Lab 5, progress.
This commit is contained in:
parent
46d339f31e
commit
8e430b94ae
|
@ -1,12 +1,31 @@
|
|||
package OOP.Java.lab_5
|
||||
|
||||
class Sentence(sentenceString: String) {
|
||||
var sentenceArray = arrayOf<Pair<Word, Punctuation>>()
|
||||
val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
||||
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
|
||||
|
||||
init {
|
||||
var split = sentenceString.split("(\\p{Punct}? )|(\\p{Punct})".toRegex())
|
||||
var punctuation: Array<Punctuation> = sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
||||
for (elem in split) {
|
||||
println(elem)
|
||||
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
||||
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
|
||||
|
||||
|
||||
for (i in arrayOfWords.indices) {
|
||||
sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i])
|
||||
}
|
||||
print(split)
|
||||
}
|
||||
|
||||
private fun splitWords(sentenceString: String): Array<Word>{
|
||||
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(sentenceString: String): Array<Punctuation>{
|
||||
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
var sentence = arrayOf<String>()
|
||||
this.sentenceArray.forEach { sentence += it.first.toString() + it.second.toString() }
|
||||
return sentence.joinToString(" ")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package OOP.Java.lab_5
|
||||
|
||||
class Text(textString: String) {
|
||||
var textArray = arrayOf<Pair<Sentence, Punctuation>>()
|
||||
var rawText: String = ""
|
||||
|
||||
init {
|
||||
val arrayOfSentences = splitSentences(textString)
|
||||
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
|
||||
|
||||
for (i in arrayOfSentences.indices){
|
||||
textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i])
|
||||
}
|
||||
|
||||
this.rawText = getRawTextString(arrayOfSentences)
|
||||
}
|
||||
|
||||
private fun splitSentences(textString: String): Array<Sentence> {
|
||||
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(textString: String): Array<Punctuation>{
|
||||
return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun getRawTextString(arrayOfSentences: Array<Sentence>): String {
|
||||
return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") }
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
var text = arrayOf<String>()
|
||||
this.textArray.forEach { text += it.first.toString() + it.second.toString() }
|
||||
return text.joinToString(" ")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue