-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.py
More file actions
120 lines (94 loc) · 3.28 KB
/
AI.py
File metadata and controls
120 lines (94 loc) · 3.28 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# speech to text
import speech_recognition as sr
# search
import wikipedia
# text to speech
import pyttsx3
# Bot class
from chatterbot import ChatBot
# Trainer
from chatterbot.trainers import ChatterBotCorpusTrainer
# Translate
from googletrans import Translator
translator = Translator()
chatBot = ChatBot(name="SnowyDragon")
#...
name = chatBot.name
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatBot)
# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")
# registered speech to text
r = sr.Recognizer()
# registered text to speech
engine = pyttsx3.init()
# array / list => voices
voices = engine.getProperty('voices')
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 10)
# default english
voiceId = 2
listenLang = 'en-US'
ara = False
engine.setProperty('voice', voices[voiceId].id)
def speak(message):
engine.say('{}'.format(message))
engine.runAndWait()
def listen():
talk = "Could not get that. sorry!"
try:
with sr.Microphone() as source: # use the default microphone as the audio source
r.adjust_for_ambient_noise(source)
audio = r.listen(source) # listen for the first phrase and extract it into audio data
talk = r.recognize_google(audio, language=listenLang) # recognize speech using Google Speech Recognition
print("Raw:" + talk)
if ara:
lang = translator.detect(talk).lang
print(f"Language: {lang}")
talk = translator.translate(talk, dest="en", src="ar").text
print(f"eng: {talk}")
except sr.UnknownValueError: # speech is unintelligible
if(ara != True):
speak("Sorry!!, Could not understand you, Please try again!.")
else:
speak("المعذرة، لم استطع فهم ذلك.")
talk = listen()
return talk
selected = False
engine.setProperty('voice', voices[voiceId].id)
selLan = False
while not selLan:
speak("Hey Islam")
speak("Do you want to use Arabic?")
answer = listen().lower()
if "yes" in answer or "ok" in answer or "yup" in answer or "y" in answer or "i want to" in answer or "i would like" in answer:
ara = True
listenLang = "ar-QA"
voiceId = 1
engine.setProperty('voice', voices[voiceId].id)
selLan = True
elif "no" in answer or "nope" in answer or "nah" in answer or "n" in answer or "i do not" in answer:
selLan = True
welcome = "Welcome back Islam!"
if ara:
welcome = "مَرْحباً بِعودتكَ اِسلامْ"
speak(welcome)
stop = False
while not stop:
# raw
said = listen()
if "exit" not in said.lower() or "stop" not in said.lower():
print("You: " + said)
respond = chatBot.get_response(said)
if ara:
respond = translator.translate(f"{respond.text}", dest="ar")
print(f"{name}: {respond.text}")
speak(respond.text)
else:
stop = True
# Goodbye
respond = chatBot.get_response("Goodbye")
if ara:
respond = translator.translate(f"{respond.text}", dest="ar")
print(f"{name}: {respond.text}")
speak(respond.text)