mirror of
https://github.com/ZhigalskiiIvan/TextStatisticsProject.git
synced 2024-12-22 21:01:52 +03:00
deleted unnecessary classes
This commit is contained in:
parent
a8b42a7d42
commit
ea792a3d0d
@ -6,16 +6,12 @@ import org.jetbrains.letsPlot.geom.*
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
|
||||
/** Gives statistics of saved texts.
|
||||
*/
|
||||
class StatisticBuilder {
|
||||
|
||||
/** Recognizes the name of the text, which
|
||||
/** Recognizes the name of the text, which
|
||||
* user want to see statistics about and type of output.
|
||||
* It calls methods of graphic building or printing data in console,
|
||||
* build required for it objects.
|
||||
*/
|
||||
fun getStatistics(textData: TextData) {
|
||||
fun getStatistics(textData: TextData) {
|
||||
|
||||
if (!textData.haveText()) {
|
||||
println("No saved texts")
|
||||
@ -41,13 +37,13 @@ class StatisticBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
|
||||
/** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
|
||||
* @param textName name of texts, which user want to see statistics about.
|
||||
* @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count.
|
||||
*/
|
||||
private fun buildGraphic(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
|
||||
private fun buildGraphic(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
|
||||
|
||||
val data = mapOf(
|
||||
"words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size },
|
||||
@ -69,13 +65,13 @@ class StatisticBuilder {
|
||||
println("Graphic was save in ${ggsave(fig, "$textName.png")}")
|
||||
fig.show()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Prints statistics according to data from mapOfSentenceNumToItsSize.
|
||||
/** Prints statistics according to data from mapOfSentenceNumToItsSize.
|
||||
* @param textName name of texts, which user want to see statistics about.
|
||||
* @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count.
|
||||
*/
|
||||
private fun printStatisticsInConsole(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
|
||||
private fun printStatisticsInConsole(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
|
||||
|
||||
val sectionTitle = "Text name: $textName"
|
||||
println("-".repeat(sectionTitle.length))
|
||||
@ -102,17 +98,13 @@ class StatisticBuilder {
|
||||
)
|
||||
println("-".repeat(sectionTitle.length))
|
||||
println("Done!\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Used for reading text from a file or console. */
|
||||
class TextReader {
|
||||
|
||||
/** Asks the user which where they want to read the text from and
|
||||
/** Asks the user which where they want to read the text from and
|
||||
* calls special for file- and console- reading methods according the answer.
|
||||
*/
|
||||
fun readNewText(textData: TextData) {
|
||||
fun readNewText(textData: TextData) {
|
||||
|
||||
println("Where do you want to add the text: from the console or a file?")
|
||||
|
||||
@ -123,14 +115,13 @@ class TextReader {
|
||||
"console" -> readFromConsole(textData)
|
||||
"file" -> readFromFile(textData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Read from console the name of text, checks that it hasn't saved yet and its contents,
|
||||
/** Read from console the name of text, checks that it hasn't saved yet and its contents,
|
||||
* asks if entered text is not correct and re-calls itself or
|
||||
* calls method of adding received text to data.
|
||||
*/
|
||||
private fun readFromConsole(textData: TextData) {
|
||||
private fun readFromConsole(textData: TextData) {
|
||||
|
||||
println("Input name of a text:")
|
||||
if (textData.haveText()) println("Unavailable(existing) names: ${textData.getTextNamesInString()}.")
|
||||
@ -164,14 +155,14 @@ class TextReader {
|
||||
|
||||
if (correctInputRequest.second == "yes") addTextToData(textData, name, content)
|
||||
else readFromConsole(textData)
|
||||
}
|
||||
}
|
||||
|
||||
/** Asks for the name of text, path to file to read text from,
|
||||
/** Asks for the name of text, path to file to read text from,
|
||||
* checks for its existing, reads contents and,
|
||||
* asks if entered names is not correct and re-calls itself or
|
||||
* calls method of adding received text to data.
|
||||
*/
|
||||
private fun readFromFile(textData: TextData) {
|
||||
private fun readFromFile(textData: TextData) {
|
||||
|
||||
println("Input a name of a text:")
|
||||
val name = readln()
|
||||
@ -205,18 +196,17 @@ class TextReader {
|
||||
"yes" -> addTextToData(textData, name, content)
|
||||
"no" -> readFromFile(textData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Calls method of TextData class to add new text in set
|
||||
* of tracked texts.
|
||||
* @param textName name of new text.
|
||||
* @param content content of new text.
|
||||
*/
|
||||
private fun addTextToData(textData: TextData, textName: String, content: String) =
|
||||
private fun addTextToData(textData: TextData, textName: String, content: String) =
|
||||
textData.addNewText(textName, content)
|
||||
|
||||
}
|
||||
|
||||
/** Stores data about tracking texts,
|
||||
* includes methods for work with them due the adding.
|
||||
@ -226,7 +216,6 @@ class TextData {
|
||||
/** list of monitored texts. */
|
||||
private val textsList = mutableListOf<Text>()
|
||||
|
||||
|
||||
fun haveText(): Boolean = textsList.isNotEmpty()
|
||||
|
||||
/** Method for removing text from watch list. Asks for a name of a text
|
||||
@ -234,7 +223,6 @@ class TextData {
|
||||
*/
|
||||
fun removeText() {
|
||||
|
||||
|
||||
if (!haveText()) {
|
||||
println("No saved texts")
|
||||
return
|
||||
@ -365,13 +353,11 @@ fun exit(): Nothing {
|
||||
*/
|
||||
class CommandCenter(
|
||||
private val textData: TextData,
|
||||
private val textReader: TextReader,
|
||||
private val statisticBuilder: StatisticBuilder
|
||||
) {
|
||||
|
||||
private val exitCommand = Command("exit", ::exit)
|
||||
private val addCommand = Command("add text") { textReader.readNewText(textData) }
|
||||
private val showStatisticsCommand = Command("show statistics") { statisticBuilder.getStatistics(textData) }
|
||||
private val addCommand = Command("add text") { readNewText(textData) }
|
||||
private val showStatisticsCommand = Command("show statistics") { getStatistics(textData) }
|
||||
private val removeTextCommand = Command("remove text") { textData.removeText() }
|
||||
|
||||
private val commandsList = listOf(exitCommand, addCommand, showStatisticsCommand, removeTextCommand)
|
||||
@ -531,13 +517,12 @@ class ReturnCommand : InputOutcomeCommand {
|
||||
override val exe: () -> Unit = throw ReturnException()
|
||||
}
|
||||
|
||||
|
||||
fun main() {
|
||||
|
||||
val textData = TextData()
|
||||
val statisticBuilder = StatisticBuilder()
|
||||
val textReader = TextReader()
|
||||
|
||||
val commandCenter = CommandCenter(textData, textReader, statisticBuilder)
|
||||
val commandCenter = CommandCenter(textData)
|
||||
|
||||
workCycle(commandCenter)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user