diff --git a/Textalk.py b/Textalk.py index 8491200..536d353 100644 --- a/Textalk.py +++ b/Textalk.py @@ -1,4 +1,5 @@ from word2number import w2n +from recorder import calculate import csv words_numeric = set() words_substituted = {} @@ -14,13 +15,15 @@ words_substituted[row[0]] = row[1] def parsestr(s): - for key in d.keys(): - if key in s: - if type(d[key]) == str: - s = s.replace(key, d[key]) - else: - s = s.replace(key, d[key](s)) - return s + # for key in d.keys(): + # if key in s: + # if type(d[key]) == str: + # s = s.replace(key, d[key]) + # else: + # s = s.replace(key, d[key](s)) + # return s + calculate(s) + return parsestr_numeric(parsestr_substitute(s)) def parsestr_numeric(s): words = s.split() @@ -35,35 +38,48 @@ def parsestr_numeric(s): return s def parsestr_substitute(s): - print(words_substituted) words = s.split() for i in range(len(words)): if words[i] in words_substituted: - print('HIT') words[i] = words_substituted[words[i]] return ' '.join(words) -# d ={'equals' : '=','equal': '=', 'plus':'+', 'minus':'-', 'over' : '/', 'divided by':'/', 'greater than' : '>','less than':'<', 'percentage':'%', 'percent':'%', \ -# 'multiplied' :'*', 'multiply':'*', 'multiplied by' :'*', 'multiply by':'*', 'sum of' : sum_of} +# Returns a lst of indices containing +def quantity(s): + lst = [] -# s = 'seventy one plus three' -# new_s = parsestr(s) -# print(new_s) + def helper(s, index_lst): + if 'quantity' in s: + # Remember to add index by 1 more for end index because second l in all will not be included \ + # in range() if not inclusive + start_index, end_index = s.find('quantity'), s.find('all') + 2 + index_lst.append([start_index, end_index]) + return helper(s[end_index:], index_lst) + return lst + return helper(s, lst) -def parsestr(s): - for key in d.keys(): - if key in s: - if type(d[key]) == str: - s = s.replace(key, d[key]) - else: - s = s.replace(key, d[key](s)) - return s +# Returns a list of the string segments containing +def quantity_checker(s, lst_of_indices): + str_lst = [] + + def checker(s, lst): + if lst: + start_index, end_index = lst[0][0], lst[0][1] + str_lst.append(s[start_index:end_index + 1]) + lst.pop(0) + return checker(s[end_index:], lst) + return str_lst + return checker(s, lst_of_indices) -# s = 'seventy one plus three' -# new_s = parsestr(s) -# print(new_s) +# Check before printing final latex string +def check(latex): + for word in l_d.keys(): + if word in latex: + latex = latex.replace(word, l_d[word]) + return latex -print(parsestr_numeric('Your total is four thousand two hundred ninety one dollars sixty seven cents and one hundred seventeen unborn fetuses')) -print(parsestr_numeric(parsestr_substitute('one plus two minus three times four equals zero'))) -print(parsestr_numeric(parsestr_substitute('I hate epsilon delta proofs'))) +# print(parsestr('Your total is four thousand two hundred ninety one dollars sixty seven cents and one hundred seventeen unborn fetuses')) +# print(parsestr('one plus two minus three times four equals zero')) +# print(parsestr('I just did three hundred and sixty five epsilon delta proofs')) +print(parsestr('The integral from zero to x times 5 of x plus x to the power of two')) diff --git a/recorder.py b/recorder.py index 3ea088e..0c2f415 100644 --- a/recorder.py +++ b/recorder.py @@ -1,11 +1,25 @@ +import requests import speech_recognition as sr -r = sr.Recognizer() +import wolframalpha + +appId = "E6A599-295PX2RKJE" +client = wolframalpha.Client(appId) +r = sr.Recognizer() mic = sr.Microphone() -with mic as source: +'''uncomment for mic functionality + with mic as source: r.adjust_for_ambient_noise(source) audio = r.listen(source) +''' +#a = r.recognize_google(audio) +#print(a) +#a= 'e^x/x^2 = 15' +def calculate(str): + res = client.query(str) + return next(res.results).text + -a = r.recognize_google(audio) -print(a) +a = 'integrate x squared' +print(calculate(a))