-
Notifications
You must be signed in to change notification settings - Fork 0
/
regina.py
317 lines (256 loc) · 8.62 KB
/
regina.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Author: Baris Parlan - @bparlan
# Purpose: Wiki Summarizer Voice Assistant
# Created: 22.04.2018
# regina.py
"""
tags: reasoning logical behaviour evaluation algorithmic critical thinking opensource
platforms:
1. pc
2. mobile
3. web
4. car
resources:
- http://thepeakperformancecenter.com/educational-learning/thinking/critical-thinking/
- http://thepeakperformancecenter.com/educational-learning/thinking/blooms-taxonomy/
question reference:
https://i.pinimg.com/originals/3f/b0/21/3fb021de640b63861e27db09abc84f1e.jpg
TODO: Course https://www.udemy.com/cart/subscribe/course/808422/
packaging - https://python-packaging.readthedocs.io/en/latest/minimal.html
nltk - sentiment analysis
chatbot:
rasa.stack - https://rasa.com/products/rasa-stack
bot.press - https://botpress.io/
bot framework - http://botframework.com/
project ana - https://github.com/Kitsune-tools/ProjectAna
wit.ai - https://wit.ai/
api.ai - https://dialogflow.com/
docker
source search - wikipedia
pywsd - https://github.com/alvations/pywsd
networkx - mindmap logical graph
tts & stt
brain:
wikipedia
tvtropes - pop culture wiki
data:
sql database?
postgre sql
aws
data_tvtropes 3.2gb
https://github.com/ricardojmendez/tropology
https://mega.co.nz/#!EhZxhBhK!lT38KiMhGxTbjGKD6tJuimc48Tay4ILkEt70evgeM7c
joke
gensim?
web:
django - https://www.djangoproject.com/
flask - http://flask.pocoo.org/extensions/
gunicorn - https://gunicorn.org/
Phase 1.
Perspective to "Love, Death & Robots" with Regina.
imdb api
tvtropes api
"""
import settings # settings.py holds apikey variable.
import speech_recognition as sr # https://realpython.com/python-speech-recognition/
import os
from sys import byteorder
from array import array
from struct import pack
from multiprocessing import Process # https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python#28463266
import time
import pyaudio
import wave
import wikipedia
# pip install wikipedia
import pyttsx3
THRESHOLD = 500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100
script_dir = os.path.dirname(__file__)
wav_location = script_dir + "/demo.wav"
busy = False
def is_silent(snd_data):
"Returns 'True' if below the 'silent' threshold"
return max(snd_data) < THRESHOLD
def normalize(snd_data):
"Average the volume out"
MAXIMUM = 16384
times = float(MAXIMUM)/max(abs(i) for i in snd_data)
r = array('h')
for i in snd_data:
r.append(int(i*times))
return r
def trim(snd_data):
"Trim the blank spots at the start and end"
def _trim(snd_data):
snd_started = False
r = array('h')
for i in snd_data:
if not snd_started and abs(i)>THRESHOLD:
snd_started = True
r.append(i)
elif snd_started:
r.append(i)
return r
# Trim to the left
snd_data = _trim(snd_data)
# Trim to the right
snd_data.reverse()
snd_data = _trim(snd_data)
snd_data.reverse()
return snd_data
def add_silence(snd_data, seconds):
"Add silence to the start and end of 'snd_data' of length 'seconds' (float)"
r = array('h', [0 for i in range(int(seconds*RATE))])
r.extend(snd_data)
r.extend([0 for i in range(int(seconds*RATE))])
return r
def record():
"""
Record a word or words from the microphone and
return the data as an array of signed shorts.
Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
"""
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
num_silent = 0
snd_started = False
r = array('h')
while 1:
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
snd_data.byteswap()
r.extend(snd_data)
silent = is_silent(snd_data)
if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True
if snd_started and num_silent > 30:
break
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
r = normalize(r)
#r = trim(r)
#r = add_silence(r, 0.5)
return sample_width, r
def record_to_file(path):
"Records from the microphone and outputs the resulting data to 'path'"
sample_width, data = record()
data = pack('<' + ('h'*len(data)), *data)
wf = wave.open(path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
print("Wav Recorded: " + path)
def speech_to_text():
r = sr.Recognizer()
harvard = sr.AudioFile(wav_location)
with harvard as source:
r.adjust_for_ambient_noise(source)
audio = r.record(source)
type(audio)
# wav_text = r.recognize_google(audio)
wav_text = r.recognize_google(audio, language="tr-TR")
os.remove(wav_location) # delete wav file
print("Wav's text: " + wav_text)
return wav_text
def search_in_wiki(keyword):
wikipedia.set_lang("en")
tts_text = str(wikipedia.summary(keyword, sentences=1))
return tts_text
def text_to_speech(to_be_read):
engine = pyttsx3.init()
engine.setProperty('rate',120) #120 words per minute
engine.setProperty('volume',0.9)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(to_be_read)
engine.runAndWait()
engine.stop()
def listen():
print("I'm listening...")
record_to_file(wav_location)
busy == True
query = speech_to_text()
if(query == "Regina"):
text_to_speech("Yes Master!")
print("Yes Master!")
text_to_speech("I am searching for " + query)
text_to_speech(search_in_wiki(query))
busy == False
def timer():
start = time.time()
end = time.time()
print(end - start)
if __name__ == '__main__':
while busy == False:
listen() # BAŞLATIYOR
# TODO: search_image(sentence - keyword):
# search > download & save > while playing audio - play slideshow
# TODO: while playing audio - write text to screen
# TODO: define main functions search(other functions)
# TODO: function
# TODO: Listen for "Regina", response "Yes master", and wait for commands.
# TODO: List available commands / functions.
# TODO: jsonstore offers a free and secured JSON-based cloud datastore for small projects https://www.jsonstore.io/
# TODO: Factcheck of news
"""
* Speech Recognition
[Wit.ai](https://github.com/wit-ai/pywit)
https://realpython.com/python-speech-recognition/
* [Wikipedia](https://github.com/goldsmith/Wikipedia) - Get data
* [NLTK](https://github.com/nltk/nltk) - Summarize Wiki
* Text-to-speech
https://deparkes.co.uk/2017/06/30/python-text-speech/
https://pythonprogramminglanguage.com/text-to-speech/
Sapi to play
Gtts to save
[gTTS](https://github.com/pndurette/gTTS)
RAP PART:
https://www.rappad.co/songs-about/
https://www.rhymebuster.com/rapgenerator
http://rapscript.net/
https://melobytes.com/app/melobytes
http://deepbeat.org/
https://genius.com/discussions/155749-Rap-generator
http://writerbot.com/lyrics
- Generate lyrics:
- Generate beat:
- Generate melody from lyrics: https://melobytes.com/app/melobytes
- Generate AVS milkdrop - python audio visualizer
def record_voice():
# question["voice_question"] = str(input("Whats your question? "))
# print("voice_question recorded")
def analysis_question():
question["texts"] = question["question"].split()
question["keyword"] = question["texts"][0]
print("Words analysis done.\nKeyword: %s \nOther words:" % (question["keyword"]))
question["duration"] = len(question["keyword"])
for items in question["texts"]:
print(items)
def calculate_word_count():
print("Word count of question: %s" % (question["duration"]))
question["kelime_sayisi"] = question["duration"]
def research_summary():
input = record_voice()
speech_to_text()
analysis_question()
search_keyword_on_web()
save_results()
calculate_word_count()
save_answer()
text_to_speach()
read()
# research_summary()
"""