Compare commits

...

3 Commits

Author SHA1 Message Date
a8b42a7d42 documentation corrected 2023-03-30 16:44:20 +03:00
3ace636b9d documentation corrected 2023-03-30 16:31:52 +03:00
39451d99ab deleted lib, realized graphic building 2023-03-30 16:15:22 +03:00
30 changed files with 83 additions and 72 deletions

View File

@ -8,11 +8,17 @@ version = '1.0-SNAPSHOT'
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
implementation("org.jetbrains.lets-plot:lets-plot-kotlin:4.0.0") implementation("org.jetbrains.lets-plot:lets-plot-kotlin:4.0.0")
implementation("org.jetbrains.lets-plot:lets-plot-batik:3.1.0")
implementation("org.jetbrains.lets-plot:lets-plot-jfx:3.1.0")
implementation("org.slf4j:slf4j-nop:1.7.29")
implementation("org.jetbrains.lets-plot:lets-plot-image-export:3.1.0")
testImplementation 'org.jetbrains.kotlin:kotlin-test' testImplementation 'org.jetbrains.kotlin:kotlin-test'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
} }
test { test {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,12 +1,12 @@
import java.io.File import java.io.File
import kotlin.system.exitProcess import kotlin.system.exitProcess
import org.jetbrains.letsPlot.* import org.jetbrains.letsPlot.*
import org.jetbrains.letsPlot.geom.geomBar import org.jetbrains.letsPlot.export.ggsave
import org.jetbrains.letsPlot.intern.Plot import org.jetbrains.letsPlot.geom.*
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
/** Singleton object which gives statistics of saved texts. /** Gives statistics of saved texts.
*/ */
class StatisticBuilder { class StatisticBuilder {
@ -36,32 +36,44 @@ class StatisticBuilder {
"console" -> printStatisticsInConsole(text.getName(), wordsCountsMap.toMap()) "console" -> printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
"graphic" -> buildGraphic(text.getName(), wordsCountsMap.toMap()) "graphic" -> buildGraphic(text.getName(), wordsCountsMap.toMap())
"both" -> { "both" -> {
buildGraphic(text.getName(), wordsCountsMap.toMap())
printStatisticsInConsole(text.getName(), wordsCountsMap.toMap()) printStatisticsInConsole(text.getName(), wordsCountsMap.toMap())
buildGraphic(text.getName(), wordsCountsMap.toMap())
} }
} }
} }
/** It builds bar chart with data of mapOfSentenceNumToItsSize. /** Builds bar chart with data of mapOfSentenceNumToItsSize and saves image in file.
* textName: name of texts, which user want to see statistics about. * @param textName name of texts, which user want to see statistics about.
* mapOfSentenceNumToItsSize: map of pairs of sentence numbers and their words count. * @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 plot: Plot = val data = mapOf(
ggplot(mapOfSentenceNumToItsSize) + ggsize(1000, 600) + geomBar { x = "sentence number"; y = "words count" } "words count" to mapOfSentenceNumToItsSize.map { it.value.toFloat() / mapOfSentenceNumToItsSize.size },
)
TODO("вил би сун") val fig = ggplot(data) +
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.
* 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.
* textName: name of texts, which user want to see statistics about.
* 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>) {
@ -94,12 +106,11 @@ class StatisticBuilder {
} }
/** Singleton object used for reading text from a file or console. */ /** Used for reading text from a file or console. */
class TextReader { 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.
* calls special for file- and console- reading methods according the answer.
*/ */
fun readNewText(textData: TextData) { fun readNewText(textData: TextData) {
@ -116,8 +127,8 @@ class TextReader {
/** 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 * asks if entered text is not correct and re-calls itself or
* calls method of adding received text to data. * calls method of adding received text to data.
*/ */
private fun readFromConsole(textData: TextData) { private fun readFromConsole(textData: TextData) {
@ -155,11 +166,10 @@ class TextReader {
else readFromConsole(textData) 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,
* checks for its existing, reads contents and, * asks if entered names is not correct and re-calls itself or
* asks if entered names is not correct and re-calls itself or * calls method of adding received text to data.
* calls method of adding received text to data.
*/ */
private fun readFromFile(textData: TextData) { private fun readFromFile(textData: TextData) {
@ -208,8 +218,8 @@ class TextReader {
} }
/** Object used for storing data about tracking texts, /** Stores data about tracking texts,
* includes methods for work with them. * includes methods for work with them due the adding.
*/ */
class TextData { class TextData {
@ -236,11 +246,10 @@ class TextData {
println("Text ${removingText.getName()} removed.") println("Text ${removingText.getName()} removed.")
} }
/** /** Returns Text object whose name matches searchingName or null
* Returns Text object whose name matches searchingName or null * if there is no text with same name in the textsList.
* if there is no text with same name in the textsList. * @param searchingName name of the text to be found.
* searchingName: name of the text to be found. * @return the text with searchingName name or null.
* return: the text with searchingName name or null.
*/ */
private fun getTextByName(searchingName: String): Text? { private fun getTextByName(searchingName: String): Text? {
@ -272,7 +281,7 @@ class TextData {
* Reads name of the text to be found and returns it text if * Reads name of the text to be found and returns it text if
* it exists. * it exists.
* @param message message which prints with calling this method. * @param message message which prints with calling this method.
* @return text * @return Text type object
*/ */
fun getTextData(message: String = "Input name of a text"): Text { fun getTextData(message: String = "Input name of a text"): Text {
@ -351,28 +360,24 @@ fun exit(): Nothing {
} }
/** Singleton used for reading commands from console and storing data about /** Reads commands from the console and storing data about
* the functions they should execute. * the functions they should execute.
*/ */
class CommandCenter( class CommandCenter(
private val textData: TextData, private val textData: TextData,
private val textReader: TextReader, private val textReader: TextReader,
private val statisticBuilder: StatisticBuilder 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") { textReader.readNewText(textData) }
private val showStatisticsCommand = Command("show statistics") { statisticBuilder.getStatistics(textData) } private val showStatisticsCommand = Command("show statistics") { statisticBuilder.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)
val commandsNames = commandsList.map { it.name } private val commandsNames = commandsList.map { it.name }
/** Enumerating of available commands names and functions conjugated /** Stores command and its name */
* with them.
*/
class Command(val name: String, val executingFun: () -> Unit) class Command(val name: String, val executingFun: () -> Unit)
/** Prints list of names of available commands, requests the name and /** Prints list of names of available commands, requests the name and
@ -392,32 +397,6 @@ class CommandCenter(
} }
/** Interface used to existing commands objects, whose exe value
* stores function which calls after request due the program.
*/
interface InputOutcomeCommand {
/** Function returned in a Main.requestInput method for returned to
* main menu of application or continuation of the process.
*/
val exe: () -> Unit
}
/** If called exe of this object, program continue executing
* without changes.
*/
class ContinueCommand : InputOutcomeCommand {
override val exe: () -> Unit = {}
}
/**
* If called exe of this object, a custom ReturnException is thrown
* and process of executing some called command interrupted. Exception
* catches in mainCycle and program command request is repeated.
*/
class ReturnCommand : InputOutcomeCommand {
override val exe: () -> Unit = throw ReturnException()
}
/** Custom exception being thrown if user want to return to the menu. */ /** Custom exception being thrown if user want to return to the menu. */
class ReturnException : Exception() class ReturnException : Exception()
@ -433,7 +412,7 @@ class InvalidInputTypeException : Exception()
class InvalidElemInInputException : Exception() class InvalidElemInInputException : Exception()
/** Method repeating the process of calling functions returned by /** Function repeating the process of calling functions returned by
* CommandCenter. If ReturnException was thrown, it is caught here, * CommandCenter. If ReturnException was thrown, it is caught here,
* last iteration breaks and new one is called. * last iteration breaks and new one is called.
*/ */
@ -449,7 +428,7 @@ fun workCycle(commandCenter: CommandCenter) {
} }
/** Method reads from console input, check if it isn't available, repeat the request from the console /** Function reads from console input, check if it isn't available, repeat the request from the console
* in this case and convert input in type T if possible, else throws an exception and repeat the * in this case and convert input in type T if possible, else throws an exception and repeat the
* request. * request.
* *
@ -472,7 +451,6 @@ inline fun <reified T> requestInput(
if (input == "return") return Pair(ReturnCommand(), "") if (input == "return") return Pair(ReturnCommand(), "")
try { try {
// inputT = input to typeOf<T>()
inputT = when (typeOf<T>()) { inputT = when (typeOf<T>()) {
typeOf<Int>() -> input.toInt() typeOf<Int>() -> input.toInt()
typeOf<Double>() -> input.toDouble() typeOf<Double>() -> input.toDouble()
@ -523,9 +501,36 @@ inline fun <reified T> requestInput(
continue@readingAndChangingTypeCycle continue@readingAndChangingTypeCycle
} }
} }
} }
/** Interface used to existing commands objects, whose exe value
* stores function which calls after requesting from console in
* requestInput function.
*/
interface InputOutcomeCommand {
/** Function returned in a Main.requestInput method for returned to
* main menu of application or continuation of the process.
*/
val exe: () -> Unit
}
/** If called exe of this object, program continue executing
* without changes.
*/
class ContinueCommand : InputOutcomeCommand {
override val exe: () -> Unit = {}
}
/** If called exe of this object, a custom ReturnException is thrown
* and process of executing some called command interrupted. Exception
* catches in mainCycle and program command request is repeated.
*/
class ReturnCommand : InputOutcomeCommand {
override val exe: () -> Unit = throw ReturnException()
}
fun main() { fun main() {
val textData = TextData() val textData = TextData()