This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-talking-bot.py
More file actions
87 lines (66 loc) · 2.5 KB
/
simple-talking-bot.py
File metadata and controls
87 lines (66 loc) · 2.5 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
83
84
85
86
87
## [Not Completed Yet]
## This idea is very simple; you can expand it with AI[ML, Expert System, ...]
## Name of this bot is آوآ
import random
def similarity(text, sub_text):
text = text.split(" ")
sub_text = sub_text.split(" ")
score = 0
for st in sub_text:
if st in text:
score += 1
return score / len(st)
def clean_text(text: str):
text = text.replace("@", " ")
text = text.replace("&", " ")
text = text.replace("?", " ")
text = text.replace(";", " ")
text = text.replace(",", " ")
for _ in range(5):
text = text.replace(" ", " ")
text = text.strip()
return text
class Q: # Question
def __init__(self, value: str, links: list) -> None:
self.value = value
self.links = links
class A: # Answer
def __init__(self, key: str, value: str, answers: tuple) -> None:
self.key = key
self.value = value
self.answers = answers
# TODO: Add weight to each question and answer
# TODO: Save and load this bot dictionary somewhere; he/she should learn more
# TODO: Store chain of conversations to analyse later
# TODO: Questions also should be a collection; hello, hi, Greetings, are in the same category
# TODO: What if someone says "Hello, How are you?"; should i answer to hello? and also answer to How are you? Think!
# TODO: Should i use regular expressions to find the similarity patterns?
q1a0 = A("", "well", ["I am @", "@"])
q1a1 = A("old", "7", ["I am @ days old", "I am @", "@ days"])
q1a2 = A("tall", "", ["I do not know the answer"])
q1a3 = A("happy", "", ["I do not know the answer"])
q1 = Q("how @ are you &", [q1a0, q1a1, q1a2, q1a3])
q2a1 = A("name", "AvA", ["My name is @", "I am @", "@"])
q2a2 = A("child name", "", ["I do not have a child"])
q2 = Q("what is your @", [q2a1, q2a2])
knowledge = [q1, q2]
while True:
answer = clean_text(input("Write something for me: "))
q_selected = None
q_similarity = -1
MIN_SIMILARITY_TRESH = 0.59
for q in knowledge:
key = clean_text(q.value)
sim = similarity(key, answer)
print(sim, key)
if sim > MIN_SIMILARITY_TRESH:
if q_selected is None or q_similarity < sim:
q_selected = q
q_similarity = sim
print("-----")
print(q_selected.value, q_similarity)
# FIXME: complete this section
if q_selected is not None:
for a in q_selected.links:
say = random.choice(a.answers).replace("@", a.value)
print("Possible Answers:", say)