Lab 5, documentation.
This commit is contained in:
parent
f68dc1a8a7
commit
b00e090791
|
@ -1,11 +1,23 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
class Letter(val character: Char) {
|
/**
|
||||||
|
* A class representing a single letter in a word.
|
||||||
|
*
|
||||||
|
* @property character a character value of a Letter.
|
||||||
|
*/
|
||||||
|
class Letter(private val character: Char) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return this.character.toString()
|
return this.character.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun equals(letter: Letter, ignoreCase: Boolean): Boolean {
|
/**
|
||||||
|
* Indicates whether the two objects of class Letter are "equal".
|
||||||
|
*
|
||||||
|
* @param letter a letter to compare.
|
||||||
|
* @param ignoreCase if set to "true" will ignore the case of Letters compared.
|
||||||
|
* @return "true" if letters are equal and "false" if they aren't.
|
||||||
|
*/
|
||||||
|
fun letterEquals(letter: Letter, ignoreCase: Boolean): Boolean {
|
||||||
return this.character.toString().equals(letter.toString(), ignoreCase)
|
return this.character.toString().equals(letter.toString(), ignoreCase)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,14 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class representing a punctuation mark following a word or a sentence.
|
||||||
|
*
|
||||||
|
* @property punctuationMark a [String] value of a punctuation mark.
|
||||||
|
*/
|
||||||
class Punctuation(var punctuationMark: String) {
|
class Punctuation(var punctuationMark: String) {
|
||||||
private val punctuationMarkOptions: Regex = Regex("\\p{Punct}")
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) {
|
if (!"\\p{Punct}".toRegex().containsMatchIn(punctuationMark)) {
|
||||||
this.punctuationMark = ""
|
this.punctuationMark = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,35 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
class Sentence(sentenceString: String) {
|
/**
|
||||||
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
|
* A class representing a single sentence is a text.
|
||||||
|
*
|
||||||
|
* @property sentenceArray a [Pair] of [Array]s of [Word] and [Punctuation] objects that make up a sentence.
|
||||||
|
*
|
||||||
|
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Word] and [Punctuation] objects, a secondary one accepts a string representing the entire sentence.
|
||||||
|
*/
|
||||||
|
class Sentence(var sentenceArray: Pair<Array<Word>, Array<Punctuation>>) {
|
||||||
|
|
||||||
init {
|
constructor(
|
||||||
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
sentenceString: String
|
||||||
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
|
) : this(
|
||||||
|
Pair(
|
||||||
}
|
sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray(),
|
||||||
|
sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of all [Letter] objects in a sentence.
|
||||||
|
*/
|
||||||
fun getAllLetters(): Array<Letter> {
|
fun getAllLetters(): Array<Letter> {
|
||||||
var allLetters = arrayOf<Letter>()
|
var allLetters = arrayOf<Letter>()
|
||||||
this.sentenceArray.first.forEach { allLetters += it.letters }
|
this.sentenceArray.first.forEach { allLetters += it.letters }
|
||||||
/*for (i: Word in this.sentenceArray.first) {
|
|
||||||
println("${i} ${i::class.simpleName} ${i.letters}")
|
|
||||||
}*/
|
|
||||||
return allLetters
|
return allLetters
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
var sentence = arrayOf<String>()
|
var sentence = arrayOf<String>()
|
||||||
for (i in this.sentenceArray.first.indices) {
|
this.sentenceArray.first.indices.forEach {sentence += this.sentenceArray.first[it].toString() + this.sentenceArray.second[it].toString() }
|
||||||
sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString()
|
|
||||||
}
|
|
||||||
return sentence.joinToString(" ")
|
return sentence.joinToString(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,53 +1,74 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
class Text(textString: String) {
|
/**
|
||||||
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
|
* A class representing the entire text.
|
||||||
|
*
|
||||||
|
* @property textArray a [Pair] of [Array]s of [Sentence] and [Punctuation] objects that make up a text.
|
||||||
|
*
|
||||||
|
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Sentence] and [Punctuation] objects, a secondary one accepts a string representing the entire text.
|
||||||
|
*/
|
||||||
|
class Text(var textArray: Pair<Array<Sentence>, Array<Punctuation>>) {
|
||||||
|
|
||||||
private fun splitSentences(textString: String): Array<Sentence> {
|
constructor(
|
||||||
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
|
textString: String
|
||||||
}
|
) : this(
|
||||||
|
Pair(
|
||||||
|
textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray(),
|
||||||
|
textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
private fun splitPunctuation(textString: String): Array<Punctuation> {
|
/**
|
||||||
return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
* Returns an array of all [Letter] objects in a sentence.
|
||||||
}
|
*/
|
||||||
|
private fun getAllLetters(): Array<Letter> {
|
||||||
fun getAllLetters(): Array<Letter> {
|
|
||||||
var allLetters = arrayOf<Letter>()
|
var allLetters = arrayOf<Letter>()
|
||||||
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
||||||
return allLetters
|
return allLetters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for the longest palindromic substring in a given text.
|
||||||
|
*
|
||||||
|
* @return the longest palindromic substring found.
|
||||||
|
*/
|
||||||
fun palindromeSearch(): String {
|
fun palindromeSearch(): String {
|
||||||
var results = arrayOf<String>()
|
var result = " "
|
||||||
|
|
||||||
val letters = this.getAllLetters()
|
val letters = this.getAllLetters()
|
||||||
|
|
||||||
for (leftBoundary in letters.indices) {
|
for (leftBoundary in letters.indices) {
|
||||||
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
|
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
|
||||||
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
|
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
|
||||||
// println(subToC.joinToString(""))
|
|
||||||
|
|
||||||
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
|
if (subToC.first().letterEquals(subToC.last(), true) && this.checkReverse(subToC) && subToC.size > result.length) {
|
||||||
// println(subToC.joinToString(""))
|
result = subToC.joinToString("")
|
||||||
results += subToC.joinToString("")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results.maxBy { it.length }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given substring is a palindrome.
|
||||||
|
*
|
||||||
|
* Is not case-sensitive.
|
||||||
|
*
|
||||||
|
* @param substring an [Array] of [Letter] objects representing a substring.
|
||||||
|
*
|
||||||
|
* @return "true" is a substring given is a palindrome, "false" if it isn't.
|
||||||
|
*/
|
||||||
private fun checkReverse(substring: Array<Letter>): Boolean {
|
private fun checkReverse(substring: Array<Letter>): Boolean {
|
||||||
var leftBoundary = 0
|
var leftBoundary = 0
|
||||||
var rightBoundary = substring.lastIndex
|
var rightBoundary = substring.lastIndex
|
||||||
var result = false
|
var result = false
|
||||||
var correction = 0
|
val correction = substring.size % 2
|
||||||
|
|
||||||
if (substring.size % 2 != 0) {
|
|
||||||
correction = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
|
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
|
||||||
leftBoundary++
|
leftBoundary++
|
||||||
rightBoundary--
|
rightBoundary--
|
||||||
result = substring[leftBoundary].equals(substring[rightBoundary], true)
|
result = substring[leftBoundary].letterEquals(substring[rightBoundary], true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -56,9 +77,7 @@ class Text(textString: String) {
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
var text = arrayOf<String>()
|
var text = arrayOf<String>()
|
||||||
for (i in this.textArray.first.indices) {
|
this.textArray.first.indices.forEach { text += this.textArray.first[it].toString() + this.textArray.second[it].toString() }
|
||||||
text += this.textArray.first[i].toString() + this.textArray.second[i].toString()
|
|
||||||
}
|
|
||||||
return text.joinToString(" ")
|
return text.joinToString(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,12 +1,21 @@
|
||||||
package OOP.Java.lab_5
|
package OOP.Java.lab_5
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class representing a single word is a sentence.
|
||||||
|
*
|
||||||
|
* @property letters an array of [Letter] objects that make up a word.
|
||||||
|
*
|
||||||
|
* @constructor a primary constructor accepts an array of [Letter] objects, a secondary one accepts a string representing the entire word.
|
||||||
|
*/
|
||||||
class Word(var letters: Array<Letter>) {
|
class Word(var letters: Array<Letter>) {
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
val wordString = StringBuilder()
|
val wordString = StringBuilder()
|
||||||
this.letters.forEach { wordString.append(it) }
|
this.letters.forEach { wordString.append(it) }
|
||||||
|
|
||||||
return wordString.toString()
|
return wordString.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
word: String
|
word: String
|
||||||
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
|
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
|
||||||
|
|
Loading…
Reference in New Issue