Lab 5, rewrote for proper data structures.
This commit is contained in:
parent
d91c576ab3
commit
59f315cacc
|
@ -1,18 +1,12 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
class Sentence(sentenceString: String) {
|
class Sentence(sentenceString: String) {
|
||||||
var sentenceArray = arrayOf<Pair<Word, Punctuation>>()
|
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
|
||||||
val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
|
||||||
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
||||||
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(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> {
|
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()
|
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 {
|
override fun toString(): String {
|
||||||
var sentence = arrayOf<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(" ")
|
return sentence.joinToString(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,23 +1,7 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
class Text(textString: String) {
|
class Text(textString: String) {
|
||||||
var textArray = arrayOf<Pair<Sentence, Punctuation>>()
|
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun splitSentences(textString: String): Array<Sentence> {
|
private fun splitSentences(textString: String): Array<Sentence> {
|
||||||
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
|
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()
|
return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getRawTextString(arrayOfSentences: Array<Sentence>): String {
|
fun getAllLetters(): Array<Letter> {
|
||||||
return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") }
|
var allLetters = arrayOf<Letter>()
|
||||||
|
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
||||||
|
return allLetters
|
||||||
}
|
}
|
||||||
|
|
||||||
fun palindromeSearch(): String {
|
fun palindromeSearch(): String {
|
||||||
var results = arrayOf<String>()
|
var results = arrayOf<String>()
|
||||||
for (leftBoundary in 0 until this.rawText.length) {
|
val letters = this.getAllLetters()
|
||||||
for (rightBoundary in this.rawText.length downTo leftBoundary + 1) {
|
for (leftBoundary in letters.indices) {
|
||||||
val subToC = this.rawText.substring(leftBoundary, rightBoundary)
|
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) {
|
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
|
||||||
// println(subToC)
|
// println(subToC.joinToString(""))
|
||||||
results += subToC
|
results += subToC.joinToString("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results.maxBy { it.length }
|
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 {
|
override fun toString(): String {
|
||||||
var text = arrayOf<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(" ")
|
return text.joinToString(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue