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-06 12:54:17 +03:00
|
|
|
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
|
2023-06-05 19:05:22 +03:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:40 +03:00
|
|
|
private fun splitWords(sentenceString: String): Array<Word> {
|
2023-06-05 19:05:22 +03:00
|
|
|
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:40 +03:00
|
|
|
private fun splitPunctuation(sentenceString: String): Array<Punctuation> {
|
2023-06-05 19:05:22 +03:00
|
|
|
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:54:17 +03:00
|
|
|
fun getAllLetters(): Array<Letter> {
|
|
|
|
var allLetters = arrayOf<Letter>()
|
|
|
|
this.sentenceArray.first.forEach { allLetters += it.letters }
|
|
|
|
/*for (i: Word in this.sentenceArray.first) {
|
|
|
|
println("${i} ${i::class.simpleName} ${i.letters}")
|
|
|
|
}*/
|
|
|
|
return allLetters
|
|
|
|
}
|
|
|
|
|
2023-06-05 19:05:22 +03:00
|
|
|
override fun toString(): String {
|
|
|
|
var sentence = arrayOf<String>()
|
2023-06-06 12:54:17 +03:00
|
|
|
for (i in this.sentenceArray.first.indices) {
|
|
|
|
sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString()
|
|
|
|
}
|
2023-06-05 19:05:22 +03:00
|
|
|
return sentence.joinToString(" ")
|
2023-06-05 17:11:49 +03:00
|
|
|
}
|
2023-06-05 16:37:37 +03:00
|
|
|
}
|