From e89af5f144f90de18db838ae72b6027e299d7fba Mon Sep 17 00:00:00 2001 From: ZhigalskiiIvan <671342i@gmail.com> Date: Fri, 24 Mar 2023 00:16:51 +0300 Subject: [PATCH] textAnalyser methods done --- src/main/kotlin/Main.kt | 55 ++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index c9f8c4a..885288a 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -103,28 +103,51 @@ object TextReader { } -data class Text(private val name: String, private val sentencesCount: Int) { - - val sentencesList = listOf() - - fun getSentencesCount() {} - - data class Sentence(val wordsCount: Int) {} - -} - - object TextData { private val textsList = mutableListOf() - fun addNewText(textName: String, content: String) {} + fun addNewText(textName: String, content: String) { + textsList.add(TextAnalyzer.getTextObjFromContents(textName, content)) + } object TextAnalyzer { - private val DELIMITERS = listOf('.', '!', '?') + private val DELIMITERS = Regex("([!?.]|(\\.\\.\\.))\\s") + private val WHITESPACES_WITHOUT_SPACE = Regex("(?=\\s+)(?!=\\s)") + + + fun getTextObjFromContents(name: String, content: String): Text { + var sentencesCount = 0 + val listOfSentences = mutableListOf() + + val sentencesStringList = content.split(DELIMITERS).map { it.replace(WHITESPACES_WITHOUT_SPACE, " ") } + for (sentenceText in sentencesStringList) { + val wordsList = sentenceText.split(" ").toMutableList() + wordsList.removeIf { it == "" } + if (wordsList.isNotEmpty()) { + sentencesCount++ + listOfSentences.add(Text.Sentence(wordsList.size)) + } + } + println(sentencesStringList) + + return Text(name, sentencesCount, listOfSentences.toList()) - fun returnListOfSentences(text: Text): List { - TODO() } + + } + + data class Text( + private val name: String, + private val sentencesCount: Int, + private val sentencesList: List, + ) { + + fun getSentencesCount() = sentencesCount + fun getName() = name + data class Sentence(private val wordsCount: Int) { + fun getWordsCount() = wordsCount + } + } } @@ -156,8 +179,6 @@ object CommandCenter { fun main() { mainCycle@ while (true) { - (CommandCenter.readCommandFromConsole())() - } } \ No newline at end of file