Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
chatbot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
option3a
chatbot
Commits
fbfe2190
Commit
fbfe2190
authored
Jan 30, 2024
by
Tiravy Amaury
Browse files
Options
Downloads
Patches
Plain Diff
function_documentation
parent
a6440706
No related branches found
No related tags found
1 merge request
!6
Test amau
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
chatbot_complet.py
+66
-4
66 additions, 4 deletions
chatbot_complet.py
with
66 additions
and
4 deletions
chatbot_complet.py
+
66
−
4
View file @
fbfe2190
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment