-
Notifications
You must be signed in to change notification settings - Fork 1
/
SMSParser.py
69 lines (62 loc) · 2.48 KB
/
SMSParser.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
from MathSolver import MathSolver
from Wikipedia import Wikipedia
from Translator import Translate
from Grammar import Grammar
from inspect import cleandoc
import time
class SMSParser():
def __init__(self):
self.wiki = Wikipedia()
self.translate = Translate()
self.grammar = Grammar()
self.math_solver = MathSolver()
def commands(self):
commands = '''List of commands:
1. commands
2. wiki <search/summary/content> <topic>
3. translate <dest lang abbrev.> <text>
4. grammar <text>
5. mathpix <img> (image in new sms)
6. duesoon
'''
return cleandoc(commands)
def parse(self, text):
# add some placeholders for error handling
sections = text.strip().split() + [''] * 2
features = ['commands', 'wiki', 'translate', 'grammar', 'mathpix', 'duesoon']
try:
feature = sections[0]
if feature not in features:
return 'Feature not found'
elif feature == 'commands':
return self.commands()
elif feature == 'wiki':
action = sections[1]
wiki_actions = ['search', 'summary', 'content']
if action not in wiki_actions:
return 'Action not found'
text = ' '.join(sections[2:])
print(text)
print(self.wiki.summary(text))
return getattr(self.wiki, action)(text)
elif feature == 'translate':
dest_lang = sections[1]
text = ' '.join(sections[2:])
return self.translate.translate(text, dest=dest_lang)
elif feature == 'mathpix':
equation = self.math_solver.read_math('test1.jpg')
return self.math_solver.solve_math2("find zeroes for:" + equation)
elif feature == 'grammar':
text = ' '.join(sections[1:])
return self.grammar.check(text)
elif feature == "duesoon":
time.sleep(4)
return('Upcoming due dates:\n\n14.4 Problem Set - Due in 2 day(s)\n\nHemingway Reading Questions - Due in 1 day(s)')
except Exception as e:
print(e)
return 'Something went wrong. Please double-check your command!'
if __name__ == "__main__":
sms_parser = SMSParser()
command = 'translate en me gusta mucho comer las manzanas'
command = 'mathpix 2+3'
print(sms_parser.parse(command))