diff --git a/Java/lab_5/Sentence.kt b/Java/lab_5/Sentence.kt index ba3da6a..3a6b597 100644 --- a/Java/lab_5/Sentence.kt +++ b/Java/lab_5/Sentence.kt @@ -1,12 +1,31 @@ package OOP.Java.lab_5 class Sentence(sentenceString: String) { + var sentenceArray = arrayOf>() + val arrayOfWords: Array = splitWords(sentenceString) + val arrayOfPunctuations: Array = splitPunctuation(sentenceString) + init { - var split = sentenceString.split("(\\p{Punct}? )|(\\p{Punct})".toRegex()) - var punctuation: Array = sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray() - for (elem in split) { - println(elem) +// val arrayOfWords: Array = splitWords(sentenceString) +// val arrayOfPunctuations: Array = splitPunctuation(sentenceString) + + + for (i in arrayOfWords.indices) { + sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i]) } - print(split) + } + + private fun splitWords(sentenceString: String): Array{ + return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray() + } + + private fun splitPunctuation(sentenceString: String): Array{ + return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray() + } + + override fun toString(): String { + var sentence = arrayOf() + this.sentenceArray.forEach { sentence += it.first.toString() + it.second.toString() } + return sentence.joinToString(" ") } } \ No newline at end of file diff --git a/Java/lab_5/Text.kt b/Java/lab_5/Text.kt new file mode 100644 index 0000000..6777cde --- /dev/null +++ b/Java/lab_5/Text.kt @@ -0,0 +1,35 @@ +package OOP.Java.lab_5 + +class Text(textString: String) { + var textArray = arrayOf>() + var rawText: String = "" + + init { + val arrayOfSentences = splitSentences(textString) + val arrayOfPunctuations: Array = splitPunctuation(textString) + + for (i in arrayOfSentences.indices){ + textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i]) + } + + this.rawText = getRawTextString(arrayOfSentences) + } + + private fun splitSentences(textString: String): Array { + return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray() + } + + private fun splitPunctuation(textString: String): Array{ + return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray() + } + + private fun getRawTextString(arrayOfSentences: Array): String { + return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") } + } + + override fun toString(): String { + var text = arrayOf() + this.textArray.forEach { text += it.first.toString() + it.second.toString() } + return text.joinToString(" ") + } +} \ No newline at end of file