package OOP.Java.lab_5 class Text(textString: String) { var textArray = arrayOf>() var rawText: String = "" val arrayOfSentences = splitSentences(textString) val arrayOfPunctuations: Array = splitPunctuation(textString) init { val arrayOfSentences = splitSentences(textString) val arrayOfPunctuations: Array = splitPunctuation(textString) for (i in arrayOfSentences.indices) { textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i]) } this.rawText = getRawTextString(arrayOfSentences) } private fun splitSentences(textString: String): Array { return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray() } private fun splitPunctuation(textString: String): Array { return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray() } private fun getRawTextString(arrayOfSentences: Array): String { return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") } } fun palindromeSearch(): String { var results = arrayOf() 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) } override fun toString(): String { var text = arrayOf() this.textArray.forEach { text += it.first.toString() + it.second.toString() } return text.joinToString(" ") } }