forked from dwks/kaldi-grammar-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.py
154 lines (122 loc) · 4.94 KB
/
words.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# module for dictating words and basic sentences
#
# (based on the multiedit module from dragonfly-modules project)
# (heavily modified)
# (the original copyright notice is reproduced below)
#
# (c) Copyright 2008 by Christo Butcher
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
try:
from aenea import (Key, Text, Mimic, CompoundRule, Dictation, Pause)
except:
from dragonfly import (Key, Text, Mimic, CompoundRule, Dictation, Pause)
import tformat
lastFormatRuleLength = 0
lastFormatRuleWords = []
def handle_word(text):
words = str(text).split()
print('word (', words, ')')
if len(words) > 0:
Text(words[0]).execute()
global lastFormatRuleWords
global lastFormatRuleLength
lastFormatRuleWords = words[0:1]
lastFormatRuleLength = len(words[0])
if len(words) > 1:
Mimic(' '.join(words[1:])).execute()
class NopeFormatRule(CompoundRule):
spec = ('nope')
def value(self, node):
global lastFormatRuleLength
print("erasing previous format of length", lastFormatRuleLength)
return Key('backspace:' + str(lastFormatRuleLength))
class ReFormatRule(CompoundRule):
spec = ('that was [upper | natural] ( proper | camel | rel-path | abs-path | score | sentence | '
'scope-resolve | jumble | dotword | dashword | natword | snakeword | brooding-narrative)')
def value(self, node):
global lastFormatRuleWords
words = lastFormatRuleWords
words = node.words()[2:] + lastFormatRuleWords
print(words)
uppercase = words[0] == 'upper'
lowercase = words[0] != 'natural'
if lowercase:
words = [word.lower() for word in words]
if uppercase:
words = [word.upper() for word in words]
words = [word.split('\\', 1)[0].replace('-', '') for word in words]
if words[0].lower() in ('upper', 'natural'):
del words[0]
function = getattr(tformat, 'format_%s' % words[0].lower())
formatted = function(words[1:])
global lastFormatRuleLength
lastFormatRuleLength = len(formatted)
return Text(formatted)
class FormatRule(CompoundRule):
spec = ('[upper | natural] ( proper | camel | rel-path | abs-path | score | sentence | '
'scope-resolve | jumble | dotword | dashword | natword | snakeword | brooding-narrative) [<dictation>] [bomb]')
extras = [Dictation(name='dictation')]
exported = False
def value(self, node):
words = node.words()
print("format rule:", words)
uppercase = words[0] == 'upper'
lowercase = words[0] != 'natural'
if lowercase:
words = [word.lower() for word in words]
if uppercase:
words = [word.upper() for word in words]
words = [word.split('\\', 1)[0].replace('-', '') for word in words]
if words[0].lower() in ('upper', 'natural'):
del words[0]
bomb = None
if 'bomb' in words:
bomb_point = words.index('bomb')
if bomb_point+1 < len(words):
bomb = words[bomb_point+1 : ]
words = words[ : bomb_point]
function = getattr(tformat, 'format_%s' % words[0].lower())
formatted = function(words[1:])
global lastFormatRuleWords
lastFormatRuleWords = words[1:]
global lastFormatRuleLength
lastFormatRuleLength = len(formatted)
# empty formatted causes problems here
print(" ->", formatted)
if bomb != None:
return Text(formatted) + Mimic(' '.join(bomb))
else:
return Text(formatted)
class PhraseFormatRule(CompoundRule):
spec = ('[start] [new] phrase [<dictation>]')
extras = [Dictation(name='dictation')]
def value(self, node):
words = node.words()
print("format rule:", words)
leading = (words[0] != 'start')
trailing = False #(words[0] != 'end' and words[0] != 'isolated')
if words[0] == 'start' or words[0] == 'isolated': words = words[1:]
capitalize = (words[0] == 'new')
if words[0] == 'new': words = words[1:]
words = words[1:] # delete "phrase"
if len(words) == 0: return Pause("0")
formatted = ''
addedWords = False
for word in words:
special = word.split('\\', 1)
if(len(special) > 1 and special[1] != 'pronoun' and special[1] != 'determiner'):
formatted += special[0]
else:
if(addedWords): formatted += ' '
formatted += special[0]
addedWords = True
if capitalize: formatted = formatted[:1].capitalize() + formatted[1:]
if leading: formatted = ' ' + formatted
if trailing: formatted += ' '
global lastFormatRuleWords
lastFormatRuleWords = words
global lastFormatRuleLength
lastFormatRuleLength = len(formatted)
print(" ->", formatted)
return Text(formatted)