nouvele base

This commit is contained in:
Qpelu 2025-04-09 15:31:52 +02:00
parent 40a222510c
commit 045e613ff2
4 changed files with 73 additions and 81 deletions

View File

@ -1,5 +1,7 @@
package Main; package Main;
import java.util.List;
public class Avis { public class Avis {
private String commentaire; private String commentaire;
private int positif; private int positif;
@ -13,24 +15,43 @@ public class Avis {
url = u; url = u;
} }
public static int Analyse(Avis a, Dictionnaire dico) { public static int Analyse(List<String> a, Dictionnaire dico) {
String[] motsCommentaire = a.getCommentaire().split("\\W+"); int moyenne = 0;
int total = 0; int sommenote = 0;
for (String m : motsCommentaire) { for (String commentaire : a) {
m = m.toLowerCase(); String[] motsCommentaire = commentaire.split("\\W+");
m = Dictionnaire.normaliserMot(m); int score = 0;
if (Dictionnaire.isStopWord(m)) continue; for (String m : motsCommentaire) {
if (!dico.getDico().containsKey(m)) continue; double a1 = 0.0;
m = m.toLowerCase();
m = Dictionnaire.normaliserMot(m);
total += dico.getDico().get(m).getSentiment(); if (Dictionnaire.isStopWord(m)) continue;
System.out.println(total); 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() { public String getCommentaire() {
return commentaire; return commentaire;

View File

@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ListeAvis { public class ListeAvis {
private List<Avis> listeAvis; private static List<Avis> listeAvis;
public ListeAvis(String csvPath) { public ListeAvis(String csvPath) {
listeAvis = new ArrayList<>(); listeAvis = new ArrayList<>();
@ -22,14 +22,13 @@ public class ListeAvis {
while ((ligne = br.readLine()) != null) { while ((ligne = br.readLine()) != null) {
String[] valeurs = ligne.split(";", 4); String[] valeurs = ligne.split(";", 4);
if (valeurs.length != 4) { if (valeurs.length != 4) {
System.err.println("Ligne ignorée (format incorrect) : " + ligne); System.err.println("Ligne ignorée (format incorrect) : " + ligne);
continue; continue;
} }
try { try {
int numeroFilm = Integer.parseInt(valeurs[0].trim()); int numeroFilm = Integer.parseInt(valeurs[1].trim());
String url = valeurs[1].trim(); String url = valeurs[1].trim();
String commentaire = valeurs[3].trim(); String commentaire = valeurs[3].trim();
int polarite = Integer.parseInt(valeurs[2].trim()); int polarite = Integer.parseInt(valeurs[2].trim());
@ -58,4 +57,19 @@ public class ListeAvis {
public List<Avis> getListeAvis() { public List<Avis> getListeAvis() {
return listeAvis; return listeAvis;
} }
public static List<String> getCommentairesParFilm(int numeroFilm) {
List<String> commentaires = new ArrayList<>();
for (Avis avis : listeAvis) {
if (avis.getFilm() == numeroFilm) {
commentaires.add(avis.getCommentaire());
}
}
return commentaires;
}
} }

View File

@ -3,24 +3,26 @@ package Main;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
/* String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\big.csv"; // String cheminFichierExcel = "C:\\Users\\Qpelu\\Downloads\\big.csv";
String cheminSauvegarde = "C:\\Users\\Qpelu\\Downloads\\dictionnaire.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.apprentissageSentiment(listeAvis.getListeAvis());
dictionnaire.sauvegarderDictionnaireCSV(cheminSauvegarde); dictionnaire.sauvegarderDictionnaireCSV(cheminSauvegarde);*/
}*/Dictionnaire d =new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv"); ///Dictionnaire d =new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv");
System.out.println(d); //System.out.println(d);
} }
} }

View File

@ -194,62 +194,15 @@ public class inter extends JFrame {
rafraichirPanel(); rafraichirPanel();
} }
/** private int calculerNoteMoyenne(int filmNumber) {
* Analyse un commentaire et calcule une note sur 5 int totalNotes= 0;
*/ Dictionnaire dico=new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv");
/*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) {
Dictionnaire dico = new Dictionnaire(); totalNotes += Avis.Analyse(ListeAvis.getCommentairesParFilm(filmNumber), dico);
List<String> 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);
System.out.println(totalNotes); 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 * 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 // Récupérer la liste des commentaires
List<String> commentaires = commentairesFilms.get(filmNumber); ListeAvis lavis = new ListeAvis("C:\\Users\\Qpelu\\Downloads\\big.csv");
commentaires.add(commentaire); Dictionnaire dico = new Dictionnaire("C:\\Users\\Qpelu\\Downloads\\dictionnaire.csv");
List<String> commentaires = ListeAvis.getCommentairesParFilm(filmNumber);
commentaires.add(c);
// Calculer la nouvelle note moyenne // Calculer la nouvelle note moyenne
int noteMoyenne = calculerNoteMoyenne(filmNumber); int noteMoyenne = Avis.Analyse(commentaires, dico);
notesFilms.put(filmNumber, noteMoyenne); notesFilms.put(filmNumber, noteMoyenne);
// Mettre à jour l'interface // Mettre à jour l'interface