OOP_IO-2x_2023-mirror/Java/lab_5/Sentence.kt

35 lines
1.3 KiB
Kotlin
Raw Permalink Normal View History

2023-06-05 16:37:37 +03:00
package OOP.Java.lab_5
2023-06-07 15:38:48 +03:00
/**
* 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>>) {
2023-06-05 19:05:22 +03:00
2023-06-07 15:38:48 +03:00
constructor(
sentenceString: String
) : 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()
)
)
2023-06-05 19:05:22 +03:00
2023-06-07 15:38:48 +03:00
/**
* Returns an array of all [Letter] objects in a sentence.
*/
fun getAllLetters(): Array<Letter> {
var allLetters = arrayOf<Letter>()
this.sentenceArray.first.forEach { allLetters += it.letters }
return allLetters
}
2023-06-05 19:05:22 +03:00
override fun toString(): String {
var sentence = arrayOf<String>()
2023-06-07 15:38:48 +03:00
this.sentenceArray.first.indices.forEach {sentence += this.sentenceArray.first[it].toString() + this.sentenceArray.second[it].toString() }
2023-06-05 19:05:22 +03:00
return sentence.joinToString(" ")
2023-06-05 17:11:49 +03:00
}
2023-06-05 16:37:37 +03:00
}