Lab 5, rewrote for proper data structures.

This commit is contained in:
Rhinemann 2023-06-06 12:54:17 +03:00
parent d91c576ab3
commit 59f315cacc
2 changed files with 48 additions and 34 deletions

View File

@ -1,18 +1,12 @@
package OOP.Java.lab_5
class Sentence(sentenceString: String) {
var sentenceArray = arrayOf<Pair<Word, Punctuation>>()
val arrayOfWords: Array<Word> = splitWords(sentenceString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
init {
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
for (i in arrayOfWords.indices) {
sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i])
}
}
private fun splitWords(sentenceString: String): Array<Word> {
@ -23,9 +17,20 @@ class Sentence(sentenceString: String) {
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
}
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
}
override fun toString(): String {
var sentence = arrayOf<String>()
this.sentenceArray.forEach { sentence += it.first.toString() + it.second.toString() }
for (i in this.sentenceArray.first.indices) {
sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString()
}
return sentence.joinToString(" ")
}
}

View File

@ -1,23 +1,7 @@
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)
init {
val arrayOfSentences = splitSentences(textString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
for (i in arrayOfSentences.indices) {
textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i])
}
this.rawText = getRawTextString(arrayOfSentences)
}
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
private fun splitSentences(textString: String): Array<Sentence> {
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
@ -27,29 +11,54 @@ class Text(textString: String) {
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 getAllLetters(): Array<Letter> {
var allLetters = arrayOf<Letter>()
this.textArray.first.forEach { allLetters += it.getAllLetters() }
return allLetters
}
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)
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(""))
if (subToC.first().equals(subToC.last(), true) && subToC.equals(subToC.reversed(), true) && subToC.length >= 3) {
// println(subToC)
results += subToC
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
// println(subToC.joinToString(""))
results += subToC.joinToString("")
}
}
}
return results.maxBy { it.length }
}
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
}
override fun toString(): String {
var text = arrayOf<String>()
this.textArray.forEach { text += it.first.toString() + it.second.toString() }
for (i in this.textArray.first.indices) {
text += this.textArray.first[i].toString() + this.textArray.second[i].toString()
}
return text.joinToString(" ")
}
}