OOP_IO-2x_2023-mirror/Java/lab_5/Sentence.kt

31 lines
1.2 KiB
Kotlin
Raw Normal View History

2023-06-05 16:37:37 +03:00
package OOP.Java.lab_5
2023-06-05 17:11:49 +03:00
class Sentence(sentenceString: String) {
2023-06-05 19:05:22 +03:00
var sentenceArray = arrayOf<Pair<Word, Punctuation>>()
val arrayOfWords: Array<Word> = splitWords(sentenceString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
2023-06-05 17:11:49 +03:00
init {
2023-06-05 19:05:22 +03:00
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
for (i in arrayOfWords.indices) {
sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i])
2023-06-05 17:11:49 +03:00
}
2023-06-05 19:05:22 +03:00
}
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(" ")
2023-06-05 17:11:49 +03:00
}
2023-06-05 16:37:37 +03:00
}