mirror of
https://github.com/ZhigalskiiIvan/TextStatisticsProject.git
synced 2024-11-09 18:01:51 +03:00
deleted unnecessary classes
This commit is contained in:
parent
a8b42a7d42
commit
ea792a3d0d
@ -6,89 +6,85 @@ import org.jetbrains.letsPlot.geom.*
|
|||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
|
|
||||||
/** Gives statistics of saved texts.
|
/** 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.
|
||||||
*/
|
*/
|
||||||
class StatisticBuilder {
|
fun getStatistics(textData: TextData) {
|
||||||
|
|
||||||
/** 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) {
|
|
||||||
|
|
||||||
if (!textData.haveText()) {
|
|
||||||
println("No saved texts")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val text = textData.getTextData("Input name of a text which you want to see statistics about.")
|
|
||||||
|
|
||||||
var counter = 1
|
|
||||||
val wordsCountsMap = text.getSentencesList().map { counter++ to it.getWordsCount() }
|
|
||||||
|
|
||||||
println("Print \"console\" if you have see data in console, \"graphic\" if you have see histogram and \"both\" if you have see them together:")
|
|
||||||
|
|
||||||
val request = requestInput(listOf("console", "graphic", "both"))
|
|
||||||
request.first.exe()
|
|
||||||
|
|
||||||
when (request.second) {
|
|
||||||
"console" -> printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
|
|
||||||
"graphic" -> buildGraphic(text.getName(), wordsCountsMap.toMap())
|
|
||||||
"both" -> {
|
|
||||||
printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
|
|
||||||
buildGraphic(text.getName(), wordsCountsMap.toMap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!textData.haveText()) {
|
||||||
|
println("No saved texts")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
|
val text = textData.getTextData("Input name of a text which you want to see statistics about.")
|
||||||
* @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>) {
|
|
||||||
|
|
||||||
val data = mapOf(
|
var counter = 1
|
||||||
"words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size },
|
val wordsCountsMap = text.getSentencesList().map { counter++ to it.getWordsCount() }
|
||||||
)
|
|
||||||
|
|
||||||
val fig = ggplot(data) +
|
println("Print \"console\" if you have see data in console, \"graphic\" if you have see histogram and \"both\" if you have see them together:")
|
||||||
geomBar(
|
|
||||||
color = "white",
|
|
||||||
fill = "red"
|
|
||||||
) { x = "words count" } +
|
|
||||||
geomArea(
|
|
||||||
stat = Stat.density(),
|
|
||||||
color = "white",
|
|
||||||
fill = "pink",
|
|
||||||
alpha = 0.4
|
|
||||||
) { x = "words count" } +
|
|
||||||
ggsize(1400, 800)
|
|
||||||
|
|
||||||
println("Graphic was save in ${ggsave(fig, "$textName.png")}")
|
val request = requestInput(listOf("console", "graphic", "both"))
|
||||||
fig.show()
|
request.first.exe()
|
||||||
|
|
||||||
|
when (request.second) {
|
||||||
|
"console" -> printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
|
||||||
|
"graphic" -> buildGraphic(text.getName(), wordsCountsMap.toMap())
|
||||||
|
"both" -> {
|
||||||
|
printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
|
||||||
|
buildGraphic(text.getName(), wordsCountsMap.toMap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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>) {
|
|
||||||
|
|
||||||
val sectionTitle = "Text name: $textName"
|
/** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
|
||||||
println("-".repeat(sectionTitle.length))
|
* @param textName name of texts, which user want to see statistics about.
|
||||||
println(sectionTitle)
|
* @param mapOfSentenceNumToItsSize map of pairs of sentence numbers and their words count.
|
||||||
println("-".repeat(sectionTitle.length))
|
*/
|
||||||
|
private fun buildGraphic(textName: String, mapOfSentenceNumToItsSize: Map<Int, Int>) {
|
||||||
|
|
||||||
val totalSentencesCount = mapOfSentenceNumToItsSize.size
|
val data = mapOf(
|
||||||
var totalWordsCount = 0
|
"words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size },
|
||||||
for (sentInfo in mapOfSentenceNumToItsSize) {
|
)
|
||||||
totalWordsCount += sentInfo.value
|
|
||||||
}
|
val fig = ggplot(data) +
|
||||||
println(
|
geomBar(
|
||||||
"""
|
color = "white",
|
||||||
|
fill = "red"
|
||||||
|
) { x = "words count" } +
|
||||||
|
geomArea(
|
||||||
|
stat = Stat.density(),
|
||||||
|
color = "white",
|
||||||
|
fill = "pink",
|
||||||
|
alpha = 0.4
|
||||||
|
) { x = "words count" } +
|
||||||
|
ggsize(1400, 800)
|
||||||
|
|
||||||
|
println("Graphic was save in ${ggsave(fig, "$textName.png")}")
|
||||||
|
fig.show()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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>) {
|
||||||
|
|
||||||
|
val sectionTitle = "Text name: $textName"
|
||||||
|
println("-".repeat(sectionTitle.length))
|
||||||
|
println(sectionTitle)
|
||||||
|
println("-".repeat(sectionTitle.length))
|
||||||
|
|
||||||
|
val totalSentencesCount = mapOfSentenceNumToItsSize.size
|
||||||
|
var totalWordsCount = 0
|
||||||
|
for (sentInfo in mapOfSentenceNumToItsSize) {
|
||||||
|
totalWordsCount += sentInfo.value
|
||||||
|
}
|
||||||
|
println(
|
||||||
|
"""
|
||||||
|Statistics:
|
|Statistics:
|
||||||
|[count of sentences]:
|
|[count of sentences]:
|
||||||
|--- $totalSentencesCount
|
|--- $totalSentencesCount
|
||||||
@ -99,125 +95,119 @@ class StatisticBuilder {
|
|||||||
|[num of sentence: count of words in it]:
|
|[num of sentence: count of words in it]:
|
||||||
|--- ${mapOfSentenceNumToItsSize.toList().joinToString("; ") { "${it.first} contains ${it.second}" }}.
|
|--- ${mapOfSentenceNumToItsSize.toList().joinToString("; ") { "${it.first} contains ${it.second}" }}.
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
)
|
)
|
||||||
println("-".repeat(sectionTitle.length))
|
println("-".repeat(sectionTitle.length))
|
||||||
println("Done!\n")
|
println("Done!\n")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Used for reading text from a file or console. */
|
/** Asks the user which where they want to read the text from and
|
||||||
class TextReader {
|
* calls special for file- and console- reading methods according the answer.
|
||||||
|
*/
|
||||||
|
fun readNewText(textData: TextData) {
|
||||||
|
|
||||||
/** Asks the user which where they want to read the text from and
|
println("Where do you want to add the text: from the console or a file?")
|
||||||
* calls special for file- and console- reading methods according the answer.
|
|
||||||
*/
|
|
||||||
fun readNewText(textData: TextData) {
|
|
||||||
|
|
||||||
println("Where do you want to add the text: from the console or a file?")
|
val kindOfSource = requestInput(listOf("console", "file"))
|
||||||
|
kindOfSource.first.exe()
|
||||||
|
|
||||||
val kindOfSource = requestInput(listOf("console", "file"))
|
when (kindOfSource.second) {
|
||||||
kindOfSource.first.exe()
|
"console" -> readFromConsole(textData)
|
||||||
|
"file" -> readFromFile(textData)
|
||||||
when (kindOfSource.second) {
|
|
||||||
"console" -> readFromConsole(textData)
|
|
||||||
"file" -> readFromFile(textData)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** 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) {
|
|
||||||
|
|
||||||
println("Input name of a text:")
|
|
||||||
if (textData.haveText()) println("Unavailable(existing) names: ${textData.getTextNamesInString()}.")
|
|
||||||
val nameRequest = requestInput(unavailableInputs = textData.getTextNamesList())
|
|
||||||
nameRequest.first.exe()
|
|
||||||
|
|
||||||
val name = nameRequest.second.toString()
|
|
||||||
|
|
||||||
println("Input a text content(after input text press enter twice):")
|
|
||||||
var content = ""
|
|
||||||
|
|
||||||
var itWasNewParYet = false
|
|
||||||
readCycle@ while (true) {
|
|
||||||
val nextPart = readln()
|
|
||||||
if (nextPart.isEmpty()) {
|
|
||||||
if (itWasNewParYet) break@readCycle
|
|
||||||
else {
|
|
||||||
content += "\n"
|
|
||||||
itWasNewParYet = true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
itWasNewParYet = false
|
|
||||||
content += "$nextPart\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Was input data right?[yes, no]:")
|
|
||||||
|
|
||||||
val correctInputRequest = requestInput(listOf("yes", "no"))
|
|
||||||
correctInputRequest.first.exe()
|
|
||||||
|
|
||||||
if (correctInputRequest.second == "yes") addTextToData(textData, name, content)
|
|
||||||
else readFromConsole(textData)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 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) {
|
|
||||||
|
|
||||||
println("Input a name of a text:")
|
|
||||||
val name = readln()
|
|
||||||
|
|
||||||
println("Input path to file:")
|
|
||||||
val contentsFile: File
|
|
||||||
|
|
||||||
pathReadCycle@ while (true) {
|
|
||||||
val pathRequest = requestInput<String>()
|
|
||||||
pathRequest.first.exe()
|
|
||||||
val filePath = pathRequest.second.toString()
|
|
||||||
|
|
||||||
val testFile = File(filePath)
|
|
||||||
|
|
||||||
if (!testFile.exists()) {
|
|
||||||
println("Incorrect path. Repeat the input:")
|
|
||||||
continue@pathReadCycle
|
|
||||||
} else {
|
|
||||||
contentsFile = testFile
|
|
||||||
break@pathReadCycle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val content = contentsFile.readText()
|
|
||||||
|
|
||||||
print("Input was correct?[yes, no]: ")
|
|
||||||
val correctInputRequest = requestInput(listOf("yes", "no"))
|
|
||||||
correctInputRequest.first.exe()
|
|
||||||
|
|
||||||
when (correctInputRequest.second) {
|
|
||||||
"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) =
|
|
||||||
textData.addNewText(textName, content)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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) {
|
||||||
|
|
||||||
|
println("Input name of a text:")
|
||||||
|
if (textData.haveText()) println("Unavailable(existing) names: ${textData.getTextNamesInString()}.")
|
||||||
|
val nameRequest = requestInput(unavailableInputs = textData.getTextNamesList())
|
||||||
|
nameRequest.first.exe()
|
||||||
|
|
||||||
|
val name = nameRequest.second.toString()
|
||||||
|
|
||||||
|
println("Input a text content(after input text press enter twice):")
|
||||||
|
var content = ""
|
||||||
|
|
||||||
|
var itWasNewParYet = false
|
||||||
|
readCycle@ while (true) {
|
||||||
|
val nextPart = readln()
|
||||||
|
if (nextPart.isEmpty()) {
|
||||||
|
if (itWasNewParYet) break@readCycle
|
||||||
|
else {
|
||||||
|
content += "\n"
|
||||||
|
itWasNewParYet = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
itWasNewParYet = false
|
||||||
|
content += "$nextPart\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Was input data right?[yes, no]:")
|
||||||
|
|
||||||
|
val correctInputRequest = requestInput(listOf("yes", "no"))
|
||||||
|
correctInputRequest.first.exe()
|
||||||
|
|
||||||
|
if (correctInputRequest.second == "yes") addTextToData(textData, name, content)
|
||||||
|
else readFromConsole(textData)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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) {
|
||||||
|
|
||||||
|
println("Input a name of a text:")
|
||||||
|
val name = readln()
|
||||||
|
|
||||||
|
println("Input path to file:")
|
||||||
|
val contentsFile: File
|
||||||
|
|
||||||
|
pathReadCycle@ while (true) {
|
||||||
|
val pathRequest = requestInput<String>()
|
||||||
|
pathRequest.first.exe()
|
||||||
|
val filePath = pathRequest.second.toString()
|
||||||
|
|
||||||
|
val testFile = File(filePath)
|
||||||
|
|
||||||
|
if (!testFile.exists()) {
|
||||||
|
println("Incorrect path. Repeat the input:")
|
||||||
|
continue@pathReadCycle
|
||||||
|
} else {
|
||||||
|
contentsFile = testFile
|
||||||
|
break@pathReadCycle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val content = contentsFile.readText()
|
||||||
|
|
||||||
|
print("Input was correct?[yes, no]: ")
|
||||||
|
val correctInputRequest = requestInput(listOf("yes", "no"))
|
||||||
|
correctInputRequest.first.exe()
|
||||||
|
|
||||||
|
when (correctInputRequest.second) {
|
||||||
|
"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) =
|
||||||
|
textData.addNewText(textName, content)
|
||||||
|
|
||||||
|
|
||||||
/** Stores data about tracking texts,
|
/** Stores data about tracking texts,
|
||||||
* includes methods for work with them due the adding.
|
* includes methods for work with them due the adding.
|
||||||
*/
|
*/
|
||||||
@ -226,7 +216,6 @@ class TextData {
|
|||||||
/** list of monitored texts. */
|
/** list of monitored texts. */
|
||||||
private val textsList = mutableListOf<Text>()
|
private val textsList = mutableListOf<Text>()
|
||||||
|
|
||||||
|
|
||||||
fun haveText(): Boolean = textsList.isNotEmpty()
|
fun haveText(): Boolean = textsList.isNotEmpty()
|
||||||
|
|
||||||
/** Method for removing text from watch list. Asks for a name of a text
|
/** Method for removing text from watch list. Asks for a name of a text
|
||||||
@ -234,7 +223,6 @@ class TextData {
|
|||||||
*/
|
*/
|
||||||
fun removeText() {
|
fun removeText() {
|
||||||
|
|
||||||
|
|
||||||
if (!haveText()) {
|
if (!haveText()) {
|
||||||
println("No saved texts")
|
println("No saved texts")
|
||||||
return
|
return
|
||||||
@ -365,13 +353,11 @@ fun exit(): Nothing {
|
|||||||
*/
|
*/
|
||||||
class CommandCenter(
|
class CommandCenter(
|
||||||
private val textData: TextData,
|
private val textData: TextData,
|
||||||
private val textReader: TextReader,
|
|
||||||
private val statisticBuilder: StatisticBuilder
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val exitCommand = Command("exit", ::exit)
|
private val exitCommand = Command("exit", ::exit)
|
||||||
private val addCommand = Command("add text") { textReader.readNewText(textData) }
|
private val addCommand = Command("add text") { readNewText(textData) }
|
||||||
private val showStatisticsCommand = Command("show statistics") { statisticBuilder.getStatistics(textData) }
|
private val showStatisticsCommand = Command("show statistics") { getStatistics(textData) }
|
||||||
private val removeTextCommand = Command("remove text") { textData.removeText() }
|
private val removeTextCommand = Command("remove text") { textData.removeText() }
|
||||||
|
|
||||||
private val commandsList = listOf(exitCommand, addCommand, showStatisticsCommand, removeTextCommand)
|
private val commandsList = listOf(exitCommand, addCommand, showStatisticsCommand, removeTextCommand)
|
||||||
@ -531,13 +517,12 @@ class ReturnCommand : InputOutcomeCommand {
|
|||||||
override val exe: () -> Unit = throw ReturnException()
|
override val exe: () -> Unit = throw ReturnException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
|
||||||
val textData = TextData()
|
val textData = TextData()
|
||||||
val statisticBuilder = StatisticBuilder()
|
|
||||||
val textReader = TextReader()
|
|
||||||
|
|
||||||
val commandCenter = CommandCenter(textData, textReader, statisticBuilder)
|
val commandCenter = CommandCenter(textData)
|
||||||
|
|
||||||
workCycle(commandCenter)
|
workCycle(commandCenter)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user