diff --git a/src/Main/Avis.java b/src/Main/Avis.java index fa84f0d..42dd721 100644 --- a/src/Main/Avis.java +++ b/src/Main/Avis.java @@ -1,5 +1,7 @@ package Main; +import java.util.List; + public class Avis { private String commentaire; private int positif; @@ -13,24 +15,43 @@ public class Avis { url = u; } - public static int Analyse(Avis a, Dictionnaire dico) { - String[] motsCommentaire = a.getCommentaire().split("\\W+"); - int total = 0; + public static int Analyse(List a, Dictionnaire dico) { + int moyenne = 0; + int sommenote = 0; - for (String m : motsCommentaire) { - m = m.toLowerCase(); - m = Dictionnaire.normaliserMot(m); + for (String commentaire : a) { + String[] motsCommentaire = commentaire.split("\\W+"); + int score = 0; - if (Dictionnaire.isStopWord(m)) continue; - if (!dico.getDico().containsKey(m)) continue; + for (String m : motsCommentaire) { + double a1 = 0.0; + m = m.toLowerCase(); + m = Dictionnaire.normaliserMot(m); - total += dico.getDico().get(m).getSentiment(); - System.out.println(total); + if (Dictionnaire.isStopWord(m)) continue; + if (!dico.getDico().containsKey(m)) continue; + + score += dico.getDico().get(m).getSentiment(); + System.out.println(score); + + if (score >= 1000) a1 = 5; + else if (score >= 500 ) a1 = 4.0 + (score - 7) / 3.0; + else if (score >= 0) a1 = 3.0 + (score - 4) / 3.0; + else if (score <0) a1 = 2.0 + (score - 1) / 3.0; + + + sommenote += a1; + System.out.println(a+"taille"); + } } - return total; - } + // Calcul de la moyenne, en s'assurant de ne pas diviser par 0 + if (!a.isEmpty()) { + moyenne = (int) (sommenote / a.size()); + } + return moyenne; + } public String getCommentaire() { return commentaire; diff --git a/src/Main/ListeAvis.java b/src/Main/ListeAvis.java index 26843d1..0276e10 100644 --- a/src/Main/ListeAvis.java +++ b/src/Main/ListeAvis.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import java.util.List; public class ListeAvis { - private List listeAvis; + private static List listeAvis; public ListeAvis(String csvPath) { listeAvis = new ArrayList<>(); @@ -22,14 +22,13 @@ public class ListeAvis { while ((ligne = br.readLine()) != null) { String[] valeurs = ligne.split(";", 4); - if (valeurs.length != 4) { System.err.println("Ligne ignorée (format incorrect) : " + ligne); continue; } try { - int numeroFilm = Integer.parseInt(valeurs[0].trim()); + int numeroFilm = Integer.parseInt(valeurs[1].trim()); String url = valeurs[1].trim(); String commentaire = valeurs[3].trim(); int polarite = Integer.parseInt(valeurs[2].trim()); @@ -58,4 +57,19 @@ public class ListeAvis { public List getListeAvis() { return listeAvis; } + + + public static List getCommentairesParFilm(int numeroFilm) { + List commentaires = new ArrayList<>(); + + for (Avis avis : listeAvis) { + if (avis.getFilm() == numeroFilm) { + commentaires.add(avis.getCommentaire()); + } + } + + return commentaires; + } } + + diff --git a/src/Main/Main.java b/src/Main/Main.java index df0e09f..7f7c1ab 100644 --- a/src/Main/Main.java +++ b/src/Main/Main.java @@ -3,24 +3,26 @@ package Main; public class Main { public static void main(String[] args) { - /* String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\big.csv"; - String cheminSauvegarde = "C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"; + // String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\big.csv"; + // String cheminSauvegarde = "C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"; - ListeAvis listeAvis = new ListeAvis(cheminFichierExcel); + // ListeAvis listeAvis = new ListeAvis(cheminFichierExcel); - Dictionnaire dictionnaire = new Dictionnaire(); + Dictionnaire dictionnaire = new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); - Avis a = new Avis(0, "szzn", "salut je suis un gros con", 0); + Avis a = new Avis(0, "szzn", "salut j'ai adoré ce film magnifique", 0); - Avis.Analyse(a, dictionnaire); + + + /* Avis.Analyse(a, dictionnaire); dictionnaire.apprentissageSentiment(listeAvis.getListeAvis()); - dictionnaire.sauvegarderDictionnaireCSV(cheminSauvegarde); + dictionnaire.sauvegarderDictionnaireCSV(cheminSauvegarde);*/ - }*/Dictionnaire d =new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); - System.out.println(d); + ///Dictionnaire d =new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); + //System.out.println(d); } } \ No newline at end of file diff --git a/src/Main/inter.java b/src/Main/inter.java index 082df05..51dc2db 100644 --- a/src/Main/inter.java +++ b/src/Main/inter.java @@ -194,62 +194,15 @@ public class inter extends JFrame { rafraichirPanel(); } - /** - * Analyse un commentaire et calcule une note sur 5 - */ - /*private int analyserCommentaire(String commentaire) { - if (commentaire == null || commentaire.trim().isEmpty()) { - return 3; // Note moyenne par défaut - } - - StringTokenizer tokenizer = new StringTokenizer(commentaire.toLowerCase(), " ,.!?;:()\n\t\"'"); - - int totalSentiment = 0; - int nbMots = 0; - - while (tokenizer.hasMoreTokens()) { - String mot = tokenizer.nextToken(); - - Integer sentiment = dictionnaireSentiments.get(mot); - if (sentiment != null) { - totalSentiment += sentiment; - nbMots++; - } - } - - if (nbMots == 0) { - return 3; // Note moyenne par défaut - } - - // Calculer la note - double moyenneSentiment = (double) totalSentiment / nbMots; - int note = (int) Math.round(moyenneSentiment * 5 / 100); - - // Limiter entre 1 et 5 - return Math.max(1, Math.min(5, note)); - }/* - - /** - * Calcule la note moyenne d'un film basée sur tous ses commentaires - */ - private int calculerNoteMoyenne(int filmNumber) { + private int calculerNoteMoyenne(int filmNumber) { + int totalNotes= 0; + Dictionnaire dico=new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); - Dictionnaire dico = new Dictionnaire(); - List commentaires = commentairesFilms.get(filmNumber); - - if (commentaires == null || commentaires.isEmpty()) { - return 0; - } - - int totalNotes = 0; - - for (String commentaire : commentaires) { - Avis com =new Avis(filmNumber,"cd",commentaire,0); - totalNotes += Avis.Analyse(com, dico); + totalNotes += Avis.Analyse(ListeAvis.getCommentairesParFilm(filmNumber), dico); System.out.println(totalNotes); - } - return Math.round((float) totalNotes / commentaires.size()); + + return totalNotes; } /** @@ -437,13 +390,15 @@ public class inter extends JFrame { /** * Ajoute un commentaire à un film et met à jour l'interface */ - private void ajouterCommentaire(int filmNumber, String commentaire) { + private void ajouterCommentaire(int filmNumber, String c) { // Récupérer la liste des commentaires - List commentaires = commentairesFilms.get(filmNumber); - commentaires.add(commentaire); + ListeAvis lavis = new ListeAvis("C:\\Users\\Qpelu\\Downloads\\big.csv"); + Dictionnaire dico = new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); + List commentaires = ListeAvis.getCommentairesParFilm(filmNumber); + commentaires.add(c); // Calculer la nouvelle note moyenne - int noteMoyenne = calculerNoteMoyenne(filmNumber); + int noteMoyenne = Avis.Analyse(commentaires, dico); notesFilms.put(filmNumber, noteMoyenne); // Mettre à jour l'interface