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

58 lines
2.2 KiB
Kotlin
Raw Normal View History

2023-06-05 19:05:22 +03:00
package OOP.Java.lab_5
class Text(textString: String) {
var textArray = arrayOf<Pair<Sentence, Punctuation>>()
var rawText: String = ""
val arrayOfSentences = splitSentences(textString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
2023-06-05 19:05:22 +03:00
init {
val arrayOfSentences = splitSentences(textString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
for (i in arrayOfSentences.indices) {
2023-06-05 19:05:22 +03:00
textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i])
}
this.rawText = getRawTextString(arrayOfSentences)
2023-06-05 19:05:22 +03:00
}
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> {
2023-06-05 19:05:22 +03:00
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("") }
}
fun palindromeSearch(): String {
var results = arrayOf<String>()
for (leftBoundary in 0 until this.rawText.length) {
for (rightBoundary in this.rawText.length downTo leftBoundary + 1) {
val subToC = this.rawText.substring(leftBoundary, rightBoundary)
if (subToC.first().equals(subToC.last(), true) && subToC.equals(subToC.reversed(), true) && subToC.length >= 3) {
// println(subToC)
results += subToC
}
}
}
return results.maxBy { it.length }
}
fun checkStringInBoundaries(stringToCheck: String): Boolean {
return stringToCheck.equals(stringToCheck.reversed(), true)
}
2023-06-05 19:05:22 +03:00
override fun toString(): String {
var text = arrayOf<String>()
this.textArray.forEach { text += it.first.toString() + it.second.toString() }
return text.joinToString(" ")
}
}