Lab 5, progress.

This commit is contained in:
Rhinemann 2023-06-05 19:05:22 +03:00
parent 46d339f31e
commit 8e430b94ae
2 changed files with 59 additions and 5 deletions

View File

@ -1,12 +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 {
var split = sentenceString.split("(\\p{Punct}? )|(\\p{Punct})".toRegex())
var punctuation: Array<Punctuation> = sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
for (elem in split) {
println(elem)
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
for (i in arrayOfWords.indices) {
sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i])
}
print(split)
}
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(" ")
}
}

35
Java/lab_5/Text.kt Normal file
View File

@ -0,0 +1,35 @@
package OOP.Java.lab_5
class Text(textString: String) {
var textArray = arrayOf<Pair<Sentence, Punctuation>>()
var rawText: String = ""
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("") }
}
override fun toString(): String {
var text = arrayOf<String>()
this.textArray.forEach { text += it.first.toString() + it.second.toString() }
return text.joinToString(" ")
}
}