-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalChat.py
70 lines (62 loc) · 1.56 KB
/
normalChat.py
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
from difflib import get_close_matches
import json
from random import choice
import datetime
class DateTime:
def currentDate(self):
now = datetime.datetime.now()
day = now.strftime('%A')
date = str(now)[8:10]
month = now.strftime('%B')
year = str(now.year)
result = f'{day}, {date} {month}, {year}'
return result
def currentTime(self):
time = datetime.datetime.now()
x = " A.M."
if time.hour>12: x = " P.M."
time = str(time)
time = time[11:16] + x
return time
def wishMe():
now = datetime.datetime.now()
hr = now.hour
if hr<12:
wish="Good Morning"
elif hr>=12 and hr<16:
wish="Good Afternoon"
else:
wish="Good Evening"
return wish
def isContain(text, lst):
for word in lst:
if word in text:
return True
return False
def chat(text):
dt = DateTime()
result = ""
if isContain(text, ['good']):
result = wishMe()
elif isContain(text, ['date','today','day','month']):
result = dt.currentDate()
elif isContain(text, ['time']):
result = "Current Time is: " + dt.currentTime()
return result
data = json.load(open('extrafiles/NormalChat.json', encoding='utf-8'))
def lang_translate(text,language):
from googletrans import Translator, LANGUAGES
if language in LANGUAGES.values():
translator = Translator()
result = translator.translate(text, src='en', dest=language)
return result
else:
return "None"
def reply(query):
if query in data:
response = data[query]
else:
query = get_close_matches(query, data.keys(), n=2, cutoff=0.6)
if len(query)==0: return "None"
return choice(data[query[0]])
return choice(response)