forked from anonfunc/talon-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
117 lines (85 loc) · 2.66 KB
/
utility.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
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
import string
from talon.voice import Str, Key
mapping = {"semicolon": ";", "new-line": "\n", "new-paragraph": "\n\n"}
punctuation = set(".,-!?")
def parse_word(word):
word = str(word).lstrip("\\").split("\\", 1)[0]
word = mapping.get(word, word)
word = word.replace("-", " ") # hate dragon hyphenation.
return word
def parse_words(m):
try:
return list(map(parse_word, m.dgndictation[0]._words))
except AttributeError:
return []
def join_words(words, sep=" "):
out = ""
for i, word in enumerate(words):
if i > 0 and word not in punctuation:
out += sep
out += word
return out
def insert(s):
Str(s)(None)
def text(m):
insert(join_words(parse_words(m)).lower())
def sentence_text(m):
text = join_words(parse_words(m)).lower()
insert(text.capitalize())
def word(m):
try:
text = join_words(list(map(parse_word, m.dgnwords[0]._words)))
insert(text.lower())
except AttributeError:
pass
def surround(by):
def func(i, word, last):
if i == 0:
word = by + word
if last:
word += by
return word
return func
def rot13(i, word, _):
out = ""
for c in word.lower():
if c in string.ascii_lowercase:
c = chr((((ord(c) - ord("a")) + 13) % 26) + ord("a"))
out += c
return out
numeral_map = dict((str(n), n) for n in range(0, 20))
for n in range(20, 101, 10):
numeral_map[str(n)] = n
for n in range(100, 1001, 100):
numeral_map[str(n)] = n
for n in range(1000, 10001, 1000):
numeral_map[str(n)] = n
numeral_map["oh"] = 0 # synonym for zero
numeral_map["and"] = None # drop me
numerals = " (" + " | ".join(sorted(numeral_map.keys())) + ")+"
optional_numerals = " (" + " | ".join(sorted(numeral_map.keys())) + ")*"
def text_to_number(words):
tmp = [str(s).lower() for s in words]
words = [parse_word(word) for word in tmp]
result = 0
factor = 1
for word in reversed(words):
print("{} {} {}".format(result, factor, word))
if word not in numerals:
raise Exception("not a number: {}".format(words))
number = numeral_map[word]
if number is None:
continue
number = int(number)
if number > 10:
result = result + number
else:
result = result + factor * number
factor = (10 ** len(str(number))) * factor
return result
def text_to_range(words, delimiter="until"):
tmp = [str(s).lower() for s in words]
split = tmp.index(delimiter)
start = text_to_number(words[:split])
end = text_to_number(words[split + 1 :])
return start, end