-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
82 lines (77 loc) · 2.94 KB
/
chatbot.py
File metadata and controls
82 lines (77 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
import re
import random
import string
# conda update --all
#conda install -c conda-forge textblob
from textblob import TextBlob
#pip install -U textblob-fr
from textblob_fr import PatternAnalyzer
# Les input
q_bonjour = r"salut.*|bonjour.*|coucou.*|hello.*"
q_ca_va = r"comment vas-tu.*|(ça|ca).*va.*|quoi de neuf.*|"
q_nom = r"quel est ton nom.*|tu t'appelles comment.*|.*(ton) nom\??"
q_age = r".*?ton (age|âge).*|.*?quel (age|âge).*?"
q_chanson = r"tu écoutes quoi.*|.*(musique|chanson).*(aimes?|preferes?|préférée?)"
q_habitation = r"tu (habites|vis) (où|ou).*|(.*?(ou|où) habite.*?(tu|vous).*?)|(ou|où) tu (habites|habite).*"
q_emploi = r"tu fais quoi.*|tu travailles dans quoi.*|quel ton job.*"
q_good_by = r"au revoir|quit|ciao|hasta la vista|à \+"
q_meteo = r"quel temps fait-il à .*?|.*météo à .*?"
# les output
msg_bot = ["bonjour", "salut"]
msg_ca_va = ["super, et vous ?", "bien, et vous ?", "je vais bien, merci \n et vous ?", "très bien, merci \n et vous ?"]
msg_nom = ["Cédric", "Cédric Dromzée"]
msg_age = ["40 ans"]
msg_chanson = ["ce qui passe à la radio"]
msg_habitation = ["Capbreton", "dans les Landes"]
msg_emploi = ["Développeur Data IA"]
msg_fin = ["merci pour votre visite"]
msg_inconnu = ["posez une autre question"]
msg_super = ["c'est cool !", "parfait"]
msg_positif = ["demain est un autre jour"]
msg_neutre = ["ok"]
flag = True
print("""Bienvenue sur ce bot \n Écrivez votre question : \n
Dites moi au revoir pour quitter""")
while (flag == True):
text_user = input("> ")
text_user = text_user.lower()
if (re.search(q_good_by, text_user)):
print(random.choice(msg_fin))
flag = False
elif (re.fullmatch(q_bonjour,text_user)):
print(random.choice(msg_bot))
# Comment ça va ?
elif (re.fullmatch(q_ca_va, text_user)):
print(random.choice(msg_ca_va))
# L'interlocuteur répond à la qustion (et toi ?)
text_user = input("> ")
blob = TextBlob(text_user, analyzer=PatternAnalyzer())
if (blob.sentiment[0] > 0.2):
print(random.choice(msg_super))
elif (blob.sentiment[0] < -0.2):
print(random.choice(msg_positif))
else:
print(random.choice(msg_neutre))
# quel est ton nom ?
elif (re.fullmatch(q_nom, text_user)):
print(random.choice(msg_nom))
# quel age ?
elif (re.fullmatch(q_age, text_user)):
print(random.choice(msg_age))
# chanson pref ?
elif (re.fullmatch(q_chanson, text_user)):
print(random.choice(msg_chanson))
# habitation ?
elif (re.fullmatch(q_habitation, text_user)):
print(random.choice(msg_habitation))
# job
elif (re.fullmatch(q_emploi, text_user)):
print(random.choice(msg_emploi))
# météo
elif (re.search(q_meteo, text_user)):
text_user = re.sub(f"[{string.punctuation}]", " ", text_user)
print(f"Il fait beau à {text_user.split()[-1]}")
# question inconnue
else:
print(random.choice(msg_inconnu))