-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapi_easy_context.go
57 lines (52 loc) · 1.76 KB
/
api_easy_context.go
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
package fmr
// ParseWithContext returns parse trees for rule <start> at beginning
func (g *Grammar) ParseWithContext(
context, text string, starts ...string) ([]*Node, error) {
return g.extractWithContext(
func(context, text string, starts ...string) ([]*Parse, error) {
p, err := g.EarleyParseWithContext(context, text, starts...)
if err != nil {
return nil, err
}
return []*Parse{p}, nil
}, context, text, starts...)
}
// ParseAnyWithContext returns parse trees for rule <start> at any position
func (g *Grammar) ParseAnyWithContext(
context, text string, starts ...string) ([]*Node, error) {
return g.extractWithContext(
func(context, text string, starts ...string) ([]*Parse, error) {
p, err := g.EarleyParseAnyWithContext(context, text, starts...)
if err != nil {
return nil, err
}
return []*Parse{p}, nil
}, context, text, starts...)
}
// ExtractMaxAllWithContext extracts all parse trees in text for rule <start>
func (g *Grammar) ExtractMaxAllWithContext(
context, text string, starts ...string) ([]*Node, error) {
return g.extractWithContext(
g.EarleyParseMaxAllWithContext, context, text, starts...)
}
// ExtractAllWithContext extracts all parse trees in text for rule <start>
func (g *Grammar) ExtractAllWithContext(
context, text string, starts ...string) ([]*Node, error) {
return g.extractWithContext(
g.EarleyParseAllWithContext, context, text, starts...)
}
func (g *Grammar) extractWithContext(
f func(string, string, ...string) ([]*Parse, error),
context, text string, starts ...string) ([]*Node, error) {
ps, err := f(context, text, starts...)
if err != nil {
return nil, err
}
var ret []*Node
for _, p := range ps {
for _, f := range p.GetFinalStates() {
ret = append(ret, p.GetTrees(f)...)
}
}
return ret, nil
}