Lab 5, technically working prototype. It's horrid, though.

This commit is contained in:
Rhinemann 2023-06-05 21:46:40 +03:00
parent 8e430b94ae
commit 49dfcfe457
3 changed files with 28 additions and 5 deletions

View File

@ -4,7 +4,7 @@ class Punctuation(var punctuationMark: String) {
private val punctuationMarkOptions: Regex = Regex("\\p{Punct}") private val punctuationMarkOptions: Regex = Regex("\\p{Punct}")
init { init {
if (!punctuationMarkOptions.containsMatchIn(punctuationMark)){ if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) {
this.punctuationMark = "" this.punctuationMark = ""
} }
} }

View File

@ -15,11 +15,11 @@ class Sentence(sentenceString: String) {
} }
} }
private fun splitWords(sentenceString: String): Array<Word>{ private fun splitWords(sentenceString: String): Array<Word> {
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray() return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
} }
private fun splitPunctuation(sentenceString: String): Array<Punctuation>{ private fun splitPunctuation(sentenceString: String): Array<Punctuation> {
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray() return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
} }

View File

@ -3,23 +3,27 @@ package OOP.Java.lab_5
class Text(textString: String) { class Text(textString: String) {
var textArray = arrayOf<Pair<Sentence, Punctuation>>() var textArray = arrayOf<Pair<Sentence, Punctuation>>()
var rawText: String = "" var rawText: String = ""
val arrayOfSentences = splitSentences(textString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
init { init {
val arrayOfSentences = splitSentences(textString) val arrayOfSentences = splitSentences(textString)
val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString) val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(textString)
for (i in arrayOfSentences.indices){ for (i in arrayOfSentences.indices) {
textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i]) textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i])
} }
this.rawText = getRawTextString(arrayOfSentences) 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()
} }
private fun splitPunctuation(textString: String): Array<Punctuation>{ private fun splitPunctuation(textString: String): Array<Punctuation> {
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()
} }
@ -27,6 +31,25 @@ class Text(textString: String) {
return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") } 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)
}
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() } this.textArray.forEach { text += it.first.toString() + it.second.toString() }