From 27e1f40741f9d07ba9dd74d219d9e60a1daf20cb Mon Sep 17 00:00:00 2001 From: Thomas BREIL Date: Wed, 6 Dec 2023 14:20:03 +0100 Subject: [PATCH] Cristiano Ronaldo SIUUU --- src/main/kotlin/main/Main.kt | 5 ++- src/main/kotlin/main/parser.kt | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/main/parser.kt diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index 75ee2ae..8268642 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -150,7 +150,7 @@ fun app() { }, onJavaScriptClick = { selectFile(jsExtensions) { filePath -> - println("JavaScript file selected: $filePath") // Change by parser functions + parser(filePath) // Change by parser functions } } ) @@ -163,8 +163,11 @@ fun app() { fun selectFile(extensions: Set, onFileSelected: (String) -> Unit) { val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) fileDialog.isMultipleMode = false // To enable selecting only one file + fileDialog.file = "*." + extensions.joinToString(";*.") fileDialog.isVisible = true + + val selectedFile = fileDialog.file val selectedDirectory = fileDialog.directory diff --git a/src/main/kotlin/main/parser.kt b/src/main/kotlin/main/parser.kt new file mode 100644 index 0000000..b8803d6 --- /dev/null +++ b/src/main/kotlin/main/parser.kt @@ -0,0 +1,68 @@ +package main + +import java.io.File + + +fun delStrings(line : String) : String { + val regex = "\".*?\"".toRegex() + return regex.replace(line, "") +} + +fun takeOnlyJs(fileName: String): String { + val extension = fileName.split(".")[1] + if ("html" == extension){ + return File(fileName).readText().split("")[0] + } + return File(fileName).readText() +} + +fun jsWords(code : Map) : Map{ + val js = File("C:\\Users\\thoma\\Desktop\\sample.txt").readText().toString().split(",") + + return code.filter { it.key !in js } +} + +fun splitLanguages(file : String){ + val extension = file.split(".")[1] + if ("html" == extension) { + val js = File(file).readText().split("")[0] + val css = File(file).readText().split("")[0] + val newHtml = File(file).readText().replace("", "") + .replace("", "") + + + if (js != "") { + File("C:\\Users\\thoma\\Desktop\\test2\\javascript.js").writeText(js) + } + + if (newHtml != File(file).readText()) { + File("C:\\Users\\thoma\\Desktop\\test2\\html.html").writeText(newHtml) + } + if (css != "") { + File("C:\\Users\\thoma\\Desktop\\test2\\css.css").writeText(css) + } + } + +} + + + +fun parser(fileName : String) { + val delimiter1 = " " + val regex = "[^a-zA-Z]".toRegex() + val array = mutableListOf() + + val line = delStrings(takeOnlyJs(fileName)).toString().replace(regex, " ").toString() + line.split(delimiter1).forEach { + + if (it != "") { + array.add(it) + } + } + val map = jsWords(array.groupingBy { it }.eachCount()) + + val sortedMap = map.toList().take(10).sortedBy { (_, value) -> value }.toMap() + sortedMap.forEach() { (t, u) -> println("$t : $u") } // affiche le nombre d'occurence de chaque mot + +} +