From fbfe219039eef930a00aeccfe71d3c983815adac Mon Sep 17 00:00:00 2001
From: amau345 <amaury.tiravy@ecl20.ec-lyon.fr>
Date: Tue, 30 Jan 2024 20:49:54 +0100
Subject: [PATCH] function_documentation

---
 chatbot_complet.py | 70 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 66 insertions(+), 4 deletions(-)

diff --git a/chatbot_complet.py b/chatbot_complet.py
index 489221f..04ba8a3 100644
--- a/chatbot_complet.py
+++ b/chatbot_complet.py
@@ -1,5 +1,5 @@
 import sys
-from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QTextEdit, QLineEdit, QPushButton, QSizePolicy, QListWidget, QListWidgetItem,QLabel
+from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QTextEdit, QLineEdit, QPushButton, QSizePolicy, QListWidget, QListWidgetItem, QLabel
 from PyQt5.QtCore import Qt
 from PyQt5.QtGui import QPalette, QColor, QFont, QIcon
 from sklearn.feature_extraction.text import TfidfVectorizer
@@ -9,12 +9,26 @@ from nltk.corpus import stopwords
 from nltk.tokenize import word_tokenize
 
 def read_text_file(file_path):
+    """
+    Reads the content of a text file specified by `file_path` and splits it into paragraphs based on double line breaks (`'\n\n'`).
+    Parameters:
+    - file_path (str): The path to the text file.
+    Returns:
+    - list: A list of non-empty paragraphs from the file.
+    """
     with open(file_path, 'r', encoding='utf-8') as file:
         content = file.read().split('\n\n')
         content1 = [item for item in content if item != ""]
     return content1
 
-def extract_keywords_textblob_french(sentence):
+def extract_keywords_french(sentence):
+    """
+    Tokenizes and filters a given sentence to extract keywords in French. Removes stop words and focuses on meaningful terms.
+    Parameters:
+    - sentence (str): The input sentence.
+    Returns:
+    - str: A string containing the extracted keywords.
+    """
     stop_words = set(stopwords.words('french'))
     mots_questions = ['qui', 'quoi', 'où', 'quand', 'pourquoi', 'comment', 'quel', 'quelle', 'quels', 'quelles', 'est-ce que', 'y a-t-il', 'peut-on', 'sont-ils', 'sont-elles', 'combien', 'lequel', 'laquelle', 'lesquels', 'lesquelles', 'est-ce', 'n\'est-ce pas', 'savoir', 'pouvez-vous', 'êtes-vous', 'avez-vous', 'dois-je', 'quelqu\'un', 'quelque chose']
     stop_words=stop_words.union(mots_questions)
@@ -24,6 +38,14 @@ def extract_keywords_textblob_french(sentence):
 
 
 def create_vectorial_base(text_lines, min_chars=10):
+    """
+    Creates a TF-IDF vectorial base from a list of text lines.
+    Parameters:
+    - text_lines (list): List of text lines.
+    - min_chars (int): Minimum number of characters required for a line to be included (default is 10).
+    Returns:
+    - tuple: A tuple containing the TF-IDF vectorizer, the TF-IDF matrix (vectorial base), and the feature names.
+    """
     filtered_lines = [line for line in text_lines if len(line) >= min_chars]
 
     if not filtered_lines:
@@ -37,6 +59,16 @@ def create_vectorial_base(text_lines, min_chars=10):
     return vectorizer, vectorial_base, feature_names
 
 def get_best_answers(question, text_lines, vectorizer, vectorial_base):
+    """
+    Retrieves the top 3 most similar text lines to a given question based on cosine similarity.
+    Parameters:
+    - question (str): The user's question.
+    - text_lines (list): List of text lines.
+    - vectorizer: The TF-IDF vectorizer.
+    - vectorial_base: The TF-IDF matrix (vectorial base).
+    Returns:
+    - list: A list of the top 3 most similar text lines as answers.
+    """
     question_vector = vectorizer.transform([question]).toarray()
 
     # Calculate cosine similarity between the question and each text line
@@ -50,12 +82,18 @@ def get_best_answers(question, text_lines, vectorizer, vectorial_base):
     return best_answers
 
 class WrappingLabel(QLabel):
+    """
+    Subclass of QLabel with word wrapping enabled. Used for displaying text in the GUI.
+    """
     def __init__(self, text='', parent=None):
         super(WrappingLabel, self).__init__(text, parent)
         self.setWordWrap(True)
 
 
 class StyledListWidgetItem(QListWidgetItem):
+    """
+    Subclass of QListWidgetItem with custom styling for the chat history list.
+    """
     def __init__(self, text='', parent=None):
         super(StyledListWidgetItem, self).__init__(parent)
         self.setText(text)
@@ -67,6 +105,9 @@ class StyledListWidgetItem(QListWidgetItem):
         self.setData(Qt.UserRole, palette)
 
 class StyledListWidget(QListWidget):
+    """
+    Subclass of QListWidget with custom styling for the chat history list.
+    """
     def __init__(self, parent=None):
         super(StyledListWidget, self).__init__(parent)
         self.setAlternatingRowColors(False)
@@ -79,12 +120,20 @@ class StyledListWidget(QListWidget):
         """)
 
     def addStyledItem(self, text):
+        """
+        Adds a styled item to the list widget.
+        Parameters:
+        - text (str): The text to be added to the list.
+        """
         item = StyledListWidgetItem(text)
         item.initStyle()
         self.addItem(item)
 
 
 class ChatbotInterface(QWidget):
+    """
+    Main class representing the chatbot interface. Initializes the UI and handles user interactions.
+    """
     def __init__(self):
         super().__init__()
         file_path = 'reglementdescolarite-ingegeneraliste2324-1.docx.txt' 
@@ -100,6 +149,9 @@ class ChatbotInterface(QWidget):
         self.dico2={}
 
     def init_ui(self):
+        """
+        Initializes the user interface.
+        """
         # Créer des widgets
         self.conversation_text = QTextEdit(self)
         self.conversation_text.setFont(QFont("consolas",9))
@@ -173,7 +225,9 @@ class ChatbotInterface(QWidget):
         self.history_list_widget.itemClicked.connect(self.history_item_clicked)
 
     def send_message(self):
-        
+        """
+        Handles the user's input, processes it, and displays the chatbot's response.
+        """
         user_command = self.user_input_entry.text()
         if len(user_command)>0:
             self.conversation_text.clear()
@@ -187,7 +241,7 @@ class ChatbotInterface(QWidget):
             self.conversation_text.append(chatbot_response)
 
             # Ajouter la commande à l'historique
-            user_command1=extract_keywords_textblob_french(user_command)
+            user_command1=extract_keywords_french(user_command)
             self.command_history.append(user_command1)
             self.dico2[user_command1]= user_command
             self.dico[user_command1]= chatbot_response
@@ -200,12 +254,20 @@ class ChatbotInterface(QWidget):
             pass
 
     def update_history_list(self):
+        """
+        Updates the chat history list in the UI.
+        """
         self.history_list_widget.clear()
         for command in self.command_history:
              self.history_list_widget.addStyledItem(command)
 
 
     def history_item_clicked(self, item):
+        """
+        Displays the chat history when an item is clicked.
+        Parameters:
+        - item: The clicked item.
+        """
         self.conversation_text.clear()
         # Réafficher le contenu dans la conversation_text lorsque l'élément de l'historique est cliqué
         selected_index = self.history_list_widget.row(item)
-- 
GitLab