From 184c5f765c724fe74b373a9eee2763311d97b206 Mon Sep 17 00:00:00 2001 From: Qpelu Date: Tue, 4 Mar 2025 09:30:12 +0100 Subject: [PATCH] V1 --- .classpath | 32 ++++++++ .gitignore | 1 + .project | 17 ++++ .settings/org.eclipse.core.resources.prefs | 2 + .settings/org.eclipse.jdt.core.prefs | 11 +++ src/Main/Avis.java | 52 ++++++++++++ src/Main/Dictionnaire.java | 94 ++++++++++++++++++++++ src/Main/ListeAvis.java | 85 +++++++++++++++++++ src/Main/Main.java | 18 +++++ src/Main/Mot.java | 48 +++++++++++ 10 files changed, 360 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/Main/Avis.java create mode 100644 src/Main/Dictionnaire.java create mode 100644 src/Main/ListeAvis.java create mode 100644 src/Main/Main.java create mode 100644 src/Main/Mot.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..5d17427 --- /dev/null +++ b/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..d71b041 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + IA_TP_développement_logiciel + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..4824b80 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6831c3e --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/src/Main/Avis.java b/src/Main/Avis.java new file mode 100644 index 0000000..dd454a0 --- /dev/null +++ b/src/Main/Avis.java @@ -0,0 +1,52 @@ +package Main; + +public class Avis { + private String commentaire; + private int positif; + private int film; + + + public Avis(String com, int pos, int f) { + commentaire = com; + positif = pos; + film = f; + } + + + public String getCommentaire() { + return commentaire; + } + + + public int getPositif() { + return positif; + } + + + public int getFilm() { + return film; + } + + + public void setCommentaire(String commentaire) { + this.commentaire = commentaire; + } + + + public void setPositif(int positif) { + this.positif = positif; + } + + + public void setFilm(int film) { + this.film = film; + } + + + + public String toString() { + return "Avis{commentaire='" + commentaire + "', positif=" + positif + ", film=" + film + "}"; + } +} + + diff --git a/src/Main/Dictionnaire.java b/src/Main/Dictionnaire.java new file mode 100644 index 0000000..6ad46c1 --- /dev/null +++ b/src/Main/Dictionnaire.java @@ -0,0 +1,94 @@ +package Main; + +import java.text.Normalizer; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +public class Dictionnaire { + private Map dico; + + public Dictionnaire() { + dico = new HashMap<>(); + } + + + private static final String[] STOPWORDS = { + "je", "un", "de", "et", "la", "le", "les", "en", "à", "c'est", "pour", "que", "des", "du", "une", "il", "elle", "nous", "vous", "ils", "elles", "ce", "cela", "mais", "est", "dans" + }; + + // Méthode d'apprentissage du sentiment basée sur les avis + public void apprentissageSentiment(List listeAvis) { + for (Avis avis : listeAvis) { + int positivite = avis.getPositif(); // On récupère la positivité de l'avis + String[] motsCommentaire = avis.getCommentaire().split("\\W+"); // Séparer le commentaire en mots + + for (String mot : motsCommentaire) { + mot = mot.toLowerCase(); // Convertir en minuscule pour éviter les doublons + + // Normaliser le mot pour éliminer les accents + mot = normaliserMot(mot); + + // Ignorer les stopwords (mots inutiles) + if (isStopWord(mot)) continue; + + // Ajouter le mot au dictionnaire s'il n'existe pas déjà + if (!dico.containsKey(mot)) { + dico.put(mot, new Mot(mot, 0)); // Créer un nouveau mot avec un sentiment initial de 0 + } + + Mot motInfo = dico.get(mot); + + // Incrémenter les apparitions + motInfo.incrementerApparitionsTotal(); + if (positivite > 0) { // Si l'avis est très positif + motInfo.incrementerApparitionsPositives(); + } + } + } + + // Calcul du pourcentage de positivité pour chaque mot + for (Mot mot : dico.values()) { + double pourcentagePositivite = 0; + if (mot.getApparitionsTotal() > 0) { + pourcentagePositivite = (double) mot.getApparitionsPositives() / mot.getApparitionsTotal() * 100; + } + System.out.println(mot.getNom() + " : Positivité = " + pourcentagePositivite + "%"); + } + } + + // Vérifie si un mot est un stopword + private boolean isStopWord(String mot) { + for (String stopword : STOPWORDS) { + if (mot.equals(stopword)) { + return true; + } + } + return false; + } + + // Normaliser les mots pour enlever les accents + private String normaliserMot(String mot) { + String motSansAccents = Normalizer.normalize(mot, Normalizer.Form.NFD); + motSansAccents = motSansAccents.replaceAll("[^\\p{ASCII}]", ""); // Supprime les caractères non-ASCII + return motSansAccents; + } + + // Méthode pour afficher tous les mots analysés et leur positivité + public void afficherMotsAnalyzes() { + for (Map.Entry entry : dico.entrySet()) { + String mot = entry.getKey(); + Mot motInfo = entry.getValue(); + double pourcentagePositivite = 0; + if (motInfo.getApparitionsTotal() > 0) { + pourcentagePositivite = (double) motInfo.getApparitionsPositives() / motInfo.getApparitionsTotal() * 100; + } + System.out.println(mot + " : Positivité = " + pourcentagePositivite + "%"); + } + } + + // Récupérer la liste des mots + public Map getMots() { + return dico; + } +} diff --git a/src/Main/ListeAvis.java b/src/Main/ListeAvis.java new file mode 100644 index 0000000..2cb3878 --- /dev/null +++ b/src/Main/ListeAvis.java @@ -0,0 +1,85 @@ +package Main; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ListeAvis { + private List listeAvis; + private int colCommentaire; + private int colPositivite; + private int colFilm; + + // Constructeur qui charge le fichier Excel + public ListeAvis(String aPath) { + listeAvis = new ArrayList<>(); + colCommentaire = 2; + colPositivite = 3; + colFilm = 4; + lireExcel(aPath); + } + + // Fonction qui lit le fichier Excel et charge les avis + private void lireExcel(String filePath) { + try (FileInputStream fis = new FileInputStream(new File(filePath)); + Workbook workbook = new XSSFWorkbook(fis)) { + + Sheet sheet = workbook.getSheetAt(0); // On prend la première feuille + System.out.println("Fichier Excel chargé avec succès!"); + + for (Row row : sheet) { + if (row.getRowNum() == 0) continue; // Ignorer la première ligne (en-têtes) + + // Extraction des données + String commentaire = getCellValue(row.getCell(colCommentaire)); + int positivite = getIntCellValue(row.getCell(colPositivite)); + int film = getIntCellValue(row.getCell(colFilm)); + + // Ajout de l'avis à la liste + listeAvis.add(new Avis(commentaire, positivite, film)); + + // Affichage pour débogage + System.out.println("Commentaire: " + commentaire); + System.out.println("Positivité: " + positivite); + System.out.println("Film: " + film); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + // Fonction pour récupérer la valeur d'une cellule sous forme de String + private String getCellValue(Cell cell) { + if (cell == null) return ""; + if (cell.getCellType() == CellType.STRING) { + return cell.getStringCellValue(); + } else if (cell.getCellType() == CellType.NUMERIC) { + return String.valueOf((int) cell.getNumericCellValue()); + } + return ""; + } + + // Fonction pour récupérer la valeur d'une cellule sous forme d'entier + private int getIntCellValue(Cell cell) { + if (cell == null || cell.getCellType() != CellType.NUMERIC) return 0; + return (int) cell.getNumericCellValue(); + } + + + public List getListeAvis() { + return listeAvis; + } + + + public void afficherAvis() { + for (Avis avis : listeAvis) { + System.out.println("Commentaire: " + avis.getCommentaire()); + System.out.println("Positivité: " + avis.getPositif()); + System.out.println("Film: " + avis.getFilm()); + } + } +} diff --git a/src/Main/Main.java b/src/Main/Main.java new file mode 100644 index 0000000..d601331 --- /dev/null +++ b/src/Main/Main.java @@ -0,0 +1,18 @@ +package Main; + +public class Main { + public static void main(String[] args) { + + String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\test_avis.xlsx"; + ListeAvis listeAvis = new ListeAvis(cheminFichierExcel); + + + Dictionnaire dictionnaire = new Dictionnaire(); + + + dictionnaire.apprentissageSentiment(listeAvis.getListeAvis()); + + + dictionnaire.afficherMotsAnalyzes(); + } +} diff --git a/src/Main/Mot.java b/src/Main/Mot.java new file mode 100644 index 0000000..93f5286 --- /dev/null +++ b/src/Main/Mot.java @@ -0,0 +1,48 @@ +package Main; + +public class Mot { + private String nom; + private int sentiment; + private int apparitionsTotal; + private int apparitionsPositives; + + public Mot(String nom, int sentiment) { + this.nom = nom; + this.sentiment = sentiment; + this.apparitionsTotal = 0; + this.apparitionsPositives = 0; + } + + public String getNom() { + return nom; + } + + public int getSentiment() { + return sentiment; + } + + public void setSentiment(double sentiment) { + this.sentiment = (int) (sentiment * 100); // Enregistrer le sentiment comme un pourcentage + } + + public void incrementerApparitionsTotal() { + this.apparitionsTotal++; + } + + public void incrementerApparitionsPositives() { + this.apparitionsPositives++; + } + + public int getApparitionsTotal() { + return apparitionsTotal; + } + + public int getApparitionsPositives() { + return apparitionsPositives; + } + + @Override + public String toString() { + return "Mot{nom='" + nom + "', sentiment=" + sentiment + ", apparitionsTotal=" + apparitionsTotal + ", apparitionsPositives=" + apparitionsPositives + "}"; + } +}