Added function for searching similar free name for text and rewrote documentation.

This commit is contained in:
ZhigalskiiIvan 2023-04-01 01:27:53 +03:00
parent 396df40998
commit 6fe3efbdca

View File

@ -215,6 +215,35 @@ class TextData {
fun getTextNamesList(): List<String> = textsList.map { it.getName() } fun getTextNamesList(): List<String> = textsList.map { it.getName() }
/**
* Receives text as input and splits it by sentence, calculate its lengths,
* create list of Sentence objects and returns Text object, created with
* this list, input field name, and count of sentences in content.
* @param name name of text.
* @param content string content of text.
* @return Text object
*/
private fun getTextObjFromContents(name: String, content: String): Text {
var sentencesCount = 1
val listOfSentences = mutableListOf<Text.Sentence>()
val sentencesStringList = content
.split(DELIMITERS)
.map { it.replace(WHITESPACES, " ") }.toMutableList()
sentencesStringList.removeIf { it.matches(WHITESPACES_OR_EMPTY) }
for (sentenceText in sentencesStringList) {
val wordsList = sentenceText.split(WHITESPACES).toMutableList()
wordsList.removeIf { it.matches(WHITESPACES_OR_EMPTY) }
sentencesCount++
listOfSentences.add(Text.Sentence(wordsList.size))
}
return Text(name, sentencesCount, listOfSentences.toList())
}
/** Uses for writing and reading Text objects in JSON. */ /** Uses for writing and reading Text objects in JSON. */
private object JSONReaderWriter { private object JSONReaderWriter {
private val gsonPretty: Gson = GsonBuilder().setPrettyPrinting().create() private val gsonPretty: Gson = GsonBuilder().setPrettyPrinting().create()
@ -284,35 +313,6 @@ class TextData {
} }
/**
* Receives text as input and splits it by sentence, calculate its lengths,
* create list of Sentence objects and returns Text object, created with
* this list, input field name, and count of sentences in content.
* @param name name of text.
* @param content string content of text.
* @return Text object
*/
private fun TextData.getTextObjFromContents(name: String, content: String): TextData.Text {
var sentencesCount = 1
val listOfSentences = mutableListOf<TextData.Text.Sentence>()
val sentencesStringList = content
.split(DELIMITERS)
.map { it.replace(WHITESPACES, " ") }.toMutableList()
sentencesStringList.removeIf { it.matches(WHITESPACES_OR_EMPTY) }
for (sentenceText in sentencesStringList) {
val wordsList = sentenceText.split(WHITESPACES).toMutableList()
wordsList.removeIf { it.matches(WHITESPACES_OR_EMPTY) }
sentencesCount++
listOfSentences.add(TextData.Text.Sentence(wordsList.size))
}
return TextData.Text(name, sentencesCount, listOfSentences.toList())
}
/** /**
* Parses list of Strings in commands and its arguments, * Parses list of Strings in commands and its arguments,
* checks the correctness of the entered data and calls related functions. * checks the correctness of the entered data and calls related functions.
@ -337,33 +337,42 @@ class CommandCenter(private val textData: TextData) {
/** Calls function of removing texts from data. */ /** Calls function of removing texts from data. */
private inner class RemoveTexts : Subcommand("remove", "Removing text from saved.") { private inner class RemoveTexts : Subcommand("remove", "Removing text from saved.") {
/** Texts names list to be removed */
private val removingList by argument( private val removingList by argument(
ArgType.String, "removing list", ArgType.String, "removing list",
"Selection of texts to delete" "Selection of texts to delete"
).vararg() ).vararg()
override fun execute() { /** @return list of available names from removingList */
if (removingList.isEmpty()) { private fun selectAvailableNamesFromInputted(): List<String> {
println("remove command needs arguments to work") val availableInputs = mutableListOf<String>()
return
}
val availableRemovingList = mutableListOf<String>()
removingList.forEach { removingList.forEach {
if (it in textData.getTextNamesList()) availableRemovingList.add(it) if (it in textData.getTextNamesList()) availableInputs.add(it)
else println("No text $it in data") else println("No text $it in data")
} }
return availableInputs
}
/** Calls removing of texts with available names from data */
private fun callRemovingFunctionForEachAvailableName() {
val availableRemovingList = selectAvailableNamesFromInputted()
availableRemovingList.forEach { textData.removeText(it) } availableRemovingList.forEach { textData.removeText(it) }
}
} }
/** Prints list of saved texts in console. */ /** Called in time from parse function of Remove text object and calls removing */
override fun execute() = callRemovingFunctionForEachAvailableName()
}
/** Calls printing list of saved texts in console. */
private inner class ShowTextsList : Subcommand("list", "Showing tracking texts.") { private inner class ShowTextsList : Subcommand("list", "Showing tracking texts.") {
/** Prints list of saved texts in string in console */
private fun printTextListInConsole() = println("Saved texts list: ${textData.getTextNamesInString(", ")}") private fun printTextListInConsole() = println("Saved texts list: ${textData.getTextNamesInString(", ")}")
/** Called by parse function and calls printing of saved texts names */
override fun execute() { override fun execute() {
printTextListInConsole() printTextListInConsole()
} }
@ -372,17 +381,20 @@ class CommandCenter(private val textData: TextData) {
/** Calls functions of graphic building or showing statistics in console. */ /** Calls functions of graphic building or showing statistics in console. */
private inner class ShowStatistics : Subcommand("stat", "Showing statistics") { private inner class ShowStatistics : Subcommand("stat", "Showing statistics") {
/** Names of texts for which you want to show statistics */
private val textNames by argument( private val textNames by argument(
ArgType.String, "text names", ArgType.String, "text names",
"Names of texts which you want to show statistics about" "Names of texts which you want to show statistics about"
).vararg() ).vararg()
/** Place where user can to see statistics */
private val outputLocation by option( private val outputLocation by option(
ArgType.Choice(listOf("graphic", "console", "both"), { it }), "output-location", "o", ArgType.Choice(listOf("graphic", "console", "both"), { it }), "output-location", "o",
"Choice where to show the statistics" "Choice where to show the statistics"
).default("console") ).default("console")
/** @return list of names from textNames which can be show statistics about */
private fun selectAvailableInputFromInputted(): List<String> { private fun selectAvailableInputFromInputted(): List<String> {
val availableInputs = mutableListOf<String>() val availableInputs = mutableListOf<String>()
@ -393,6 +405,10 @@ class CommandCenter(private val textData: TextData) {
return availableInputs return availableInputs
} }
/**
* Calls showing of statistics depend on outputLocation
* for each text from texts with textNames
*/
private fun callStatisticsShowingForEach(textNames: List<String>) { private fun callStatisticsShowingForEach(textNames: List<String>) {
when (outputLocation) { when (outputLocation) {
"graphic" -> textNames.forEach { callBuildingGraphic(it) } "graphic" -> textNames.forEach { callBuildingGraphic(it) }
@ -404,12 +420,14 @@ class CommandCenter(private val textData: TextData) {
} }
} }
/** Calls function of building graphic for text with textName */
private fun callBuildingGraphic(textName: String) { private fun callBuildingGraphic(textName: String) {
val text = textData.getTextByName(textName)!! val text = textData.getTextByName(textName)!!
val listOfSentenceSizes = text.getSentencesList().map { it.getWordsCount() } val listOfSentenceSizes = text.getSentencesList().map { it.getWordsCount() }
buildGraphic(textName, listOfSentenceSizes) buildGraphic(textName, listOfSentenceSizes)
} }
/** Calls function of printing statistics for text with textName */
private fun callConsoleStatisticsShowing(textName: String) { private fun callConsoleStatisticsShowing(textName: String) {
val text = textData.getTextByName(textName)!! val text = textData.getTextByName(textName)!!
var counter = 1 var counter = 1
@ -417,38 +435,48 @@ class CommandCenter(private val textData: TextData) {
printStatisticsInConsole(textName, mapOfSentenceNumToItsSize) printStatisticsInConsole(textName, mapOfSentenceNumToItsSize)
} }
/** Calls function of calling for all available text names */
override fun execute() { private fun callStatistics() {
val availableSelectedTextNames = selectAvailableInputFromInputted() val availableSelectedTextNames = selectAvailableInputFromInputted()
callStatisticsShowingForEach(availableSelectedTextNames) callStatisticsShowingForEach(availableSelectedTextNames)
} }
/** Calls showing statistics methods */
override fun execute() {
callStatistics()
}
} }
/** Calls functions of reading and adding text in saved. */ /** Calls functions of reading and adding text in saved. */
private inner class Add : Subcommand("add", "Adding text from source") { private inner class Add : Subcommand("add", "Adding text from source") {
/** Path to file for reading */
private val path by option( private val path by option(
ArgType.String, "source path", "s", ArgType.String, "source path", "s",
"Inputting path to file for reading or \"console\" to reading from console" "Inputting path to file for reading or \"console\" to reading from console"
).default("console") ).default("console")
/** New text name */
private val textName by argument( private val textName by argument(
ArgType.String, "name", ArgType.String, "name",
"Specifies the name of the new text" "Specifies the name of the new text"
) )
/** Calculate method of input and calls reading functions */
private fun callReading() = when { private fun callReading() = when {
path == "console" -> callReadingFromConsole() path == "console" -> callReadingFromConsole()
!File(path).exists() -> throw IncorrectPathException(path) !File(path).exists() -> throw IncorrectPathException(path)
else -> callReadingFromFile() else -> callReadingFromFile()
} }
/** Calls reading text with textName from console */
private fun callReadingFromConsole() = readFromConsoleAndAddToData(textData, textName) private fun callReadingFromConsole() = readFromConsoleAndAddToData(textData, textName)
/** Calls reading of text with textName from file with path */
private fun callReadingFromFile() = readFromFileAndAddToData(textData, textName, path) private fun callReadingFromFile() = readFromFileAndAddToData(textData, textName, path)
/** Calls reading methods */
override fun execute() = callReading() override fun execute() = callReading()
} }