-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectares_recognize.py
More file actions
140 lines (93 loc) · 4.01 KB
/
projectares_recognize.py
File metadata and controls
140 lines (93 loc) · 4.01 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from pandas.core.indexes.interval import le
import speech_recognition as sr
from numpy import less_equal
from gtts import gTTS
import pandas as pd
import os
def parseSentencesIntoWords(ListOfSentences, shelterDataDF):
splitSentences = []
for sentence in ListOfSentences:
listOfWords = sentence.split()
print(listOfWords)
splitSentences.append(listOfWords)
matchWordsToKeywords(splitSentences, shelterDataDF)
def obtainKeywordList():
keywords = ["shelter", "orange", "lee", "county"]
return keywords
def convertSentencesToWavFiles(returnSentenceList):
for i in range(0, len(returnSentenceList)):
mytext = returnSentenceList[i]
print(mytext)
language = "en"
myobj = gTTS(text=mytext, lang=language, slow=False)
theFile = "response" + str(i) + ".mp3"
myobj.save(theFile)
def matchWordsToKeywords(splitSentences, shelterDataDF):
keywords = obtainKeywordList()
returnSentenceList = []
returnSentence = ""
for sentence in splitSentences:
for i in range(0, len(sentence)):
if sentence[i] in keywords:
if sentence[i] == "county":
countyName = sentence[i - 1].capitalize()
countyName = countyName + " County"
# print(countyName)
queryCounty = countyName
df = pd.DataFrame(shelterDataDF, columns=["county", "label"])
containsValues = df[df["county"].str.contains(queryCounty)]
# print(containsValues)
if len(df[df["county"] == queryCounty]["label"]) > 1:
answer = df[df["county"] == queryCounty]["label"].tolist()
answerLength = len(answer)
counter = 0
for item in answer:
counter += 1
if counter == answerLength:
returnSentence = returnSentence + item
else:
returnSentence = returnSentence + item + " and "
returnSentence = (
returnSentence.replace(".", "")
+ " are the shelters in "
+ countyName
+ ". Please make your way to one of them carefully."
)
else:
answer = df[df["county"] == queryCounty]["label"].item()
returnSentence = returnSentence + answer
returnSentence = (
returnSentence
+ " is the only shelter in "
+ countyName
+ ". Please make your way there carefully."
)
returnSentenceList.append(returnSentence)
returnSentence = ""
break
else:
returnSentence = "I'm sorry, I didn't understand that. Make sure to tell me what county you are in."
if "shelter" not in sentence:
returnSentence = "Sorry, I can only help you with shelters."
returnSentenceList.append(returnSentence)
print(returnSentenceList)
convertSentencesToWavFiles(returnSentenceList)
def main():
shelterDataDF = pd.read_csv("shelters.csv")
speechFiles = []
speechFiles.append("call0.wav")
audioConvertedToTextFiles = []
r = sr.Recognizer()
for speechFile in speechFiles:
with sr.AudioFile(speechFile) as source:
audio_data = r.record(source)
try:
text = r.recognize_google(audio_data).lower()
except:
print("Recognition Error")
text = "Error"
audioConvertedToTextFiles.append(text)
print(text)
parseSentencesIntoWords(audioConvertedToTextFiles, shelterDataDF)
if __name__ == "__main__":
main()