2023-06-05 19:05:22 +03:00
|
|
|
package OOP.Java.lab_5
|
|
|
|
|
|
|
|
class Text(textString: String) {
|
2023-06-06 12:54:17 +03:00
|
|
|
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
|
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()
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:40 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:54:17 +03:00
|
|
|
fun getAllLetters(): Array<Letter> {
|
|
|
|
var allLetters = arrayOf<Letter>()
|
|
|
|
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
|
|
|
return allLetters
|
2023-06-05 19:05:22 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:40 +03:00
|
|
|
fun palindromeSearch(): String {
|
|
|
|
var results = arrayOf<String>()
|
2023-06-06 12:54:17 +03:00
|
|
|
val letters = this.getAllLetters()
|
|
|
|
for (leftBoundary in letters.indices) {
|
|
|
|
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
|
|
|
|
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
|
|
|
|
// println(subToC.joinToString(""))
|
2023-06-05 21:46:40 +03:00
|
|
|
|
2023-06-06 12:54:17 +03:00
|
|
|
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
|
|
|
|
// println(subToC.joinToString(""))
|
|
|
|
results += subToC.joinToString("")
|
2023-06-05 21:46:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results.maxBy { it.length }
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:54:17 +03:00
|
|
|
private fun checkReverse(substring: Array<Letter>): Boolean {
|
|
|
|
var leftBoundary = 0
|
|
|
|
var rightBoundary = substring.lastIndex
|
|
|
|
var result = false
|
|
|
|
var correction = 0
|
|
|
|
|
|
|
|
if (substring.size % 2 != 0) {
|
|
|
|
correction = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
|
|
|
|
leftBoundary++
|
|
|
|
rightBoundary--
|
|
|
|
result = substring[leftBoundary].equals(substring[rightBoundary], true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:40 +03:00
|
|
|
|
2023-06-05 19:05:22 +03:00
|
|
|
override fun toString(): String {
|
|
|
|
var text = arrayOf<String>()
|
2023-06-06 12:54:17 +03:00
|
|
|
for (i in this.textArray.first.indices) {
|
|
|
|
text += this.textArray.first[i].toString() + this.textArray.second[i].toString()
|
|
|
|
}
|
2023-06-05 19:05:22 +03:00
|
|
|
return text.joinToString(" ")
|
|
|
|
}
|
|
|
|
}
|