-
Notifications
You must be signed in to change notification settings - Fork 0
/
time2int.py
62 lines (47 loc) · 2.56 KB
/
time2int.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
'''
Simple script for VoxCommando users.
For context, see forum thread http://voxcommando.com/forum/index.php?topic=1948
--INFO--
This time2int function is specifically scripted to allow you to dictate "QuickAdd" events to
your Google Calendar feeds from VoxCommando. There are two problems we're trying to address:
1) MS SAPI's dictation feature interprets dictated numbers as words, not digits;
2) Google Calendar requires event times to be provided as *digits*.
This script translates event times into digits that Google can understand.
-----------------------------------------------------------------
NOTE: The script expects a 12-hour clock ("7:30 pm" not "19:30").
-----------------------------------------------------------------
'''
def time2int(text_with_num):
everything=""
units = {
"zero":"0", "oh":"0","one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8",
"nine":"9", "ten":"10", "eleven":"11","twelve":"12","fifteen":"15","twenty":"20","thirty":"30","forty five":"45",
"forty":"40","fifty":"50"}
lastWordWasNum = False
for word in text_with_num.split():#evaluate each word of voice command (string)
if word not in units:
everything = everything+" "+word
lastWordWasNum = False
#if this word is a number & last word was not, then:
elif not lastWordWasNum:
everything = everything + " " + units[word]
lastWordWasNum = True
lastNum = int(units[word])
else:#evaluating and reconstructing string if this word is a number & last word was a number.
if word in units:
if (lastWordWasNum & 0 < lastNum <=12):
everything = everything+":"+units[word]
lastWordWasNum = True
lastNum = int(units[word])
elif (0 <= lastNum < 13):
everything = everything+units[word]
lastWordWasNum = True
lastNum = int(units[word])
elif (lastNum >12 & int(units[word]) >19):
everything = everything+units[word]
lastWordWasNum = True
else:
everything = everything[:-1]
everything = everything + units[word]
lastWordWasNum = True
return everything.lstrip()