Compare commits
6 Commits
97b2b761e4
...
d91c576ab3
Author | SHA1 | Date |
---|---|---|
Rhinemann | d91c576ab3 | |
Rhinemann | 49dfcfe457 | |
Rhinemann | 8e430b94ae | |
Rhinemann | 46d339f31e | |
Rhinemann | 2ddeaab94f | |
Rhinemann | 2b8f88a45f |
|
@ -13,8 +13,8 @@ public class lab_3 {
|
|||
leftBoundary--;
|
||||
rightBoundary++;
|
||||
}
|
||||
// if it's length is greater than maxLength update
|
||||
// maxLength and res
|
||||
// if it's length is greater than maxStrLength update
|
||||
// maxLength and result
|
||||
if (rightBoundary - leftBoundary - 1 >= maxStrLength) {
|
||||
result = string.substring(leftBoundary + 1, rightBoundary);
|
||||
maxStrLength = rightBoundary - leftBoundary - 1;
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package OOP.Java.lab_5
|
||||
|
||||
class Letter(val character: Char) {
|
||||
override fun toString(): String {
|
||||
return this.character.toString()
|
||||
}
|
||||
|
||||
fun equals(letter: Letter, ignoreCase: Boolean): Boolean {
|
||||
return this.character.toString().equals(letter.toString(), ignoreCase)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package OOP.Java.lab_5
|
||||
|
||||
class Punctuation(var punctuationMark: String) {
|
||||
private val punctuationMarkOptions: Regex = Regex("\\p{Punct}")
|
||||
|
||||
init {
|
||||
if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) {
|
||||
this.punctuationMark = ""
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return punctuationMark
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
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)
|
||||
|
||||
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> {
|
||||
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(sentenceString: String): Array<Punctuation> {
|
||||
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
var sentence = arrayOf<String>()
|
||||
this.sentenceArray.forEach { sentence += it.first.toString() + it.second.toString() }
|
||||
return sentence.joinToString(" ")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
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)
|
||||
|
||||
|
||||
}
|
||||
|
||||
private fun splitSentences(textString: String): Array<Sentence> {
|
||||
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(textString: String): Array<Punctuation> {
|
||||
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 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 }
|
||||
}
|
||||
|
||||
|
||||
override fun toString(): String {
|
||||
var text = arrayOf<String>()
|
||||
this.textArray.forEach { text += it.first.toString() + it.second.toString() }
|
||||
return text.joinToString(" ")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package OOP.Java.lab_5
|
||||
|
||||
class Word(var letters: Array<Letter>) {
|
||||
override fun toString(): String {
|
||||
val wordString = StringBuilder()
|
||||
this.letters.forEach { wordString.append(it) }
|
||||
|
||||
return wordString.toString()
|
||||
}
|
||||
constructor(
|
||||
word: String
|
||||
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
|
||||
}
|
Loading…
Reference in New Issue