From 87ed353f9ecb84b8943bd0b267324819f2d3f9b9 Mon Sep 17 00:00:00 2001 From: cemal Date: Thu, 7 Dec 2023 16:17:39 +0100 Subject: [PATCH] Comparing Javascript code words with glossary words --- src/main/kotlin/main/Main.kt | 90 +++++++++++++++++++++++++++++++--- src/main/kotlin/main/parser.kt | 6 ++- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index fd29300..60c41b9 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -4,12 +4,10 @@ import androidx.compose.desktop.ui.tooling.preview.Preview import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.Check import androidx.compose.material.icons.filled.Close -import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember +import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -30,8 +28,6 @@ import java.io.FileInputStream val customRedColor = Color(0xFFB70D1B) - - @Composable fun homePage( onGlossaireClick: () -> Unit, @@ -46,6 +42,8 @@ fun homePage( ) { Text("Quali'Nomme", style = MaterialTheme.typography.h3) + var isCompareClicked by remember { mutableStateOf(false) } + Spacer(modifier = Modifier.height(16.dp)) Row( @@ -72,7 +70,10 @@ fun homePage( } Button( - onClick = { /* Action de Comparer */ }, + onClick = { + isCompareClicked = true + + }, colors = ButtonDefaults.buttonColors( backgroundColor = customRedColor, contentColor = Color.White @@ -80,16 +81,89 @@ fun homePage( ) { Text("Comparer") } + + if (isCompareClicked) { + println(mostUsedWordList.keys.toList()) + compareResults( + motsGlossaire = chargerDonneesDepuisFichier(), + motsCode = mostUsedWordList.keys.toList(), + onRetourClick = { isCompareClicked = false } + ) + } } } } + + +@Composable +fun compareResults( + motsGlossaire: List, + motsCode: List, + onRetourClick: () -> Unit, + navigationIcon: @Composable () -> Unit = { Icon(Icons.Filled.ArrowBack, contentDescription = null) } + +) { + Column( + modifier = Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Top, // Align content at the top + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(text = "Le code contient ${comparerMots(motsGlossaire, motsCode)}% des mots du glossaire\"", style = MaterialTheme.typography.h3) + } + + Column( + modifier = Modifier + .fillMaxSize() // Fills the maximum available width + .padding(16.dp), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + + ) { + Text(text = "Le code contient ${comparerMots(motsGlossaire, motsCode)}% des mots du glossaire") + } + + // Bouton retour positionné tout en bas et centré horizontalement + Column( + modifier = Modifier.fillMaxSize().padding(20.dp), + verticalArrangement = Arrangement.Bottom, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Button( + onClick = onRetourClick, + colors = ButtonDefaults.buttonColors( + backgroundColor = customRedColor, + contentColor = Color.White + ) + ) { + Text("Retour") + } + } + +} + +fun comparerMots(motsGlossaire: List, motsCode: List): Int { + var motsTrouves = 0 + motsCode.forEach { motCode -> + motsGlossaire.forEach { mot -> + if (motCode.equals(mot.nom, ignoreCase = true)) { + motsTrouves++ + } + } + } + println(motsTrouves*100/motsCode.size) + return motsTrouves*100/motsCode.size +} + +var mostUsedWordList = mutableMapOf() @Composable @Preview fun app() { MaterialTheme { val currentPage = remember { mutableStateOf("accueil") } + + when (currentPage.value) { "accueil" -> { homePage( @@ -147,7 +221,7 @@ fun app() { }, onJavaScriptClick = { selectFile(jsExtensions) { filePath -> - parser(filePath) // Change by parser functions + mostUsedWordList = parser(filePath) // Change by parser functions } } ) diff --git a/src/main/kotlin/main/parser.kt b/src/main/kotlin/main/parser.kt index 7744535..837196f 100644 --- a/src/main/kotlin/main/parser.kt +++ b/src/main/kotlin/main/parser.kt @@ -47,7 +47,7 @@ fun splitLanguages(file : String){ -fun parser(fileName : String) { +fun parser(fileName : String) : MutableMap { val delimiter1 = " " val regex = "[^a-zA-Z^é^à]".toRegex() val array = mutableListOf() @@ -61,8 +61,10 @@ fun parser(fileName : String) { } val map = jsWords(array.groupingBy { it }.eachCount()) - val sortedMap = map.toList().take(10).sortedBy { (_, value) -> value }.toMap() + var sortedMap = map.toList().take(10).sortedBy { (_, value) -> value }.toMap() sortedMap.forEach() { (t, u) -> println("$t : $u") } // affiche le nombre d'occurence de chaque mot + sortedMap = sortedMap.toMutableMap() + return sortedMap }