-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrammar.go
More file actions
347 lines (268 loc) · 7.75 KB
/
grammar.go
File metadata and controls
347 lines (268 loc) · 7.75 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package srgs
import (
"errors"
"fmt"
"github.com/beevik/etree"
"strconv"
"strings"
)
type MatchMode int
const (
ModePrefix MatchMode = iota
ModeExact
)
var (
NoMatch = errors.New("cannot consume string with token")
PrefixOnly = errors.New("string matched is a prefix")
)
var (
InvalidGrammar = errors.New("invalid grammar document")
NoRoot = errors.New("no root present in grammar")
RootNotFound = errors.New("unable to find root rule")
UnidentifiableRule = errors.New("rules must have an id")
EmptyRuleRefUri = errors.New("rulerefs must have a non-empty uri")
)
// An expansion is any part of a grammar that can match a string
type Expansion interface {
// Set the string to match on the expansion. Match either in ModePrefix (return nil error as soon as a prefix is
// found) or ModeExact (return nil only if there is an exact match -- otherwise return PrefixOnly error)
Match(string, MatchMode)
// If there are other ways of matching the prefix (e.g. if multiple alternatives or repeats match), return the
// next version of the consumed string. Otherwise return "" and Exhausted error
Next() (string, error)
// Append this expansion to a processor. This will be enable the Processor to provide the output for a given path
Scan(Processor)
// Returns a copy of the expansion. This is needed because expansions implementations are stateful, and a single
// path through a grammar may reference the same rule multiple times. In that case, the states of each reference
// must be independent.
Copy(RuleRefs) Expansion
}
// State is the internal representation of an Expansion which matched an utterance. This is used to determine the
// path that matched when scanning the match into a processor
type State interface{}
type AlternativeState struct {
index int
state State
}
type SequenceState []State
type ItemState []State
// Grammar is a representation of an SRGS grammar.
type Grammar struct {
Root *RuleRef
Xml string
root Expansion
rules Rules
ruleRefs RuleRefs
}
// Creates a new grammar
func NewGrammar() *Grammar {
return new(Grammar)
}
// Returns whether a specific string is a prefix of the grammar. For example, a grammar that matches the string
// "i want to go to the park", will also return true for HasPrefix("i want to g")
func (g *Grammar) HasPrefix(str string) bool {
str = strings.ToLower(str)
g.Root.Match(str, ModePrefix)
str, err := g.Root.Next()
for {
if err != nil {
return false
}
if len(str) == 0 {
return true
}
str, err = g.Root.Next()
}
}
// Returns whether a specific string is an exact match for the grammar. Note that this means the string is not a prefix
// and it is also not longer than the grammar.
func (g *Grammar) HasMatch(str string) bool {
str = strings.ToLower(str)
g.Root.Match(str, ModeExact)
str, err := g.Root.Next()
for {
if err != nil {
return false
}
if len(str) == 0 {
return true
}
str, err = g.Root.Next()
}
}
// Uses a processor to find a match and scan the match into the processor for SISR
func (g *Grammar) GetMatch(str string, p Processor) error {
str = strings.ToLower(str)
g.Root.Match(str, ModeExact)
str, err := g.Root.Next()
for {
if err != nil {
return err
}
if len(str) == 0 {
break
}
str, err = g.Root.Next()
}
p.AppendTag("var scopes = [{'rules':{}}];")
g.Root.Scan(p)
p.AppendTag(fmt.Sprintf("root = scopes[0]['rules']['%s'];", g.Root.ruleId))
return nil
}
// Loads an XML document into a grammar
func (g *Grammar) LoadXml(xml string) error {
g.Xml = xml
doc := etree.NewDocument()
if err := doc.ReadFromString(xml); err != nil {
return err
}
grammar := doc.SelectElement("grammar")
if grammar == nil {
return InvalidGrammar
}
rootId := grammar.SelectAttrValue("root", "")
if rootId == "" {
return NoRoot
}
var root Expansion
g.rules = Rules{}
// holds references to a given rule id so that they can be filled in once all rules have been processed
g.ruleRefs = make(map[string][]*RuleRef)
for _, rule := range grammar.SelectElements("rule") {
id, exp, err := g.decodeRule(rule)
if err != nil {
return err
}
if id == rootId {
root = exp
}
g.rules[id] = exp
if refs, ok := g.ruleRefs[id]; ok {
for _, ref := range refs {
ref.rule = exp.Copy(g.ruleRefs)
}
delete(g.ruleRefs, id)
}
}
if root == nil {
return RootNotFound
}
if len(g.ruleRefs) > 0 {
refs := ""
for ref := range g.ruleRefs {
refs += ref + ", "
}
return errors.New("unresolved rule refs: " + strings.TrimSuffix(refs, ", "))
}
g.Root = &RuleRef{
ruleId: rootId,
rule: root,
}
return nil
}
func (g *Grammar) decodeRule(rule *etree.Element) (string, Expansion, error) {
id := rule.SelectAttrValue("id", "")
if id == "" {
return "", nil, UnidentifiableRule
}
exp, err := g.decodeElement(rule)
return id, exp, err
}
func (g *Grammar) decodeElement(element *etree.Element) (Expansion, error) {
out := new(Sequence)
for _, tok := range element.Child {
if data, ok := tok.(*etree.CharData); ok {
str := strings.ToLower(strings.TrimSpace(data.Data))
if len(str) == 0 {
continue
}
out.exps = append(out.exps, decodeCharData(str))
} else if el, ok := tok.(*etree.Element); ok {
if el.Tag == "ruleref" {
special := el.SelectAttrValue("special", "")
if special == "GARBAGE" {
tempGarbage := new(Garbage)
out.exps = append(out.exps, tempGarbage)
scan := el.SelectAttrValue("scan-match", "")
if scan == "true" {
tempGarbage.scanMatch = true
}
continue
}
ref := el.SelectAttrValue("uri", "")
if ref == "" {
return nil, EmptyRuleRefUri
}
if ref[0] != '#' {
return nil, errors.New("cannot understand ruleref uri " + ref + " because it is not local")
}
ruleRef := new(RuleRef)
ruleRef.ruleId = ref[1:]
out.exps = append(out.exps, ruleRef)
if rule, ok := g.rules[ruleRef.ruleId]; ok {
ruleRef.rule = rule.Copy(g.ruleRefs)
} else {
g.ruleRefs[ruleRef.ruleId] = append(g.ruleRefs[ruleRef.ruleId], ruleRef)
}
} else if el.Tag == "item" {
exp, err := g.decodeElement(el)
if err != nil {
return nil, err
}
out.exps = append(out.exps, exp)
} else if el.Tag == "one-of" {
alt := new(Alternative)
for _, item := range el.SelectElements("item") {
exp, err := g.decodeElement(item)
if err != nil {
return nil, err
}
alt.items = append(alt.items, exp.(*Item))
}
out.exps = append(out.exps, alt)
} else if el.Tag == "tag" {
out.exps = append(out.exps, NewTag(el.Text()))
} else if el.Tag == "example" {
// ignore
} else {
return nil, errors.New("unable to parse tag " + el.Tag + " " + el.SelectAttrValue("id", "no id"))
}
} else {
fmt.Println("Unable to process", tok, "Ignoring.")
}
}
if element.Tag == "item" {
repeat := element.SelectAttrValue("repeat", "1-1")
repeatmode := RepeatMode(element.SelectAttrValue("repeat-mode", string(RepeatModeNormal)))
minMax := strings.Split(repeat, "-")
var min, max int
var err error
if len(minMax) == 1 {
if min, err = strconv.Atoi(minMax[0]); err != nil {
return nil, err
}
max = min
} else if len(minMax) == 2 {
if minMax[0] == "" {
min = 0
} else if min, err = strconv.Atoi(minMax[0]); err != nil {
return nil, err
}
if minMax[1] == "" {
return nil, errors.New(`upper bounds of item repeats must be explicitly stated (e.g. please do not use repeat="1-")`)
} else if max, err = strconv.Atoi(minMax[1]); err != nil {
return nil, err
}
} else {
return nil, errors.New("invalid repeat")
}
return NewItem(out, repeatmode, min, max, g.ruleRefs), nil
}
return out, nil
}
func decodeCharData(data string) Expansion {
if len(data) == 0 {
return nil
}
return NewToken(strings.ToLower(data))
}