Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/CalvinYan/TeXTalk
Browse files Browse the repository at this point in the history
  • Loading branch information
Deoy12 committed Oct 27, 2019
2 parents c163d29 + ec3160b commit 1299087
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 32 deletions.
72 changes: 44 additions & 28 deletions Textalk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from word2number import w2n
from recorder import calculate
import csv
words_numeric = set()
words_substituted = {}
Expand All @@ -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()
Expand All @@ -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 <quantity and all>
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 <quantity and all>
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'))
22 changes: 18 additions & 4 deletions recorder.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 1299087

Please sign in to comment.