-
Notifications
You must be signed in to change notification settings - Fork 15
/
api_easy.go
50 lines (45 loc) · 1.43 KB
/
api_easy.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
package fmr
// Parse returns parse trees for rule <start> at beginning
func (g *Grammar) Parse(text string, starts ...string) ([]*Node, error) {
return g.extract(func(text string, starts ...string) ([]*Parse, error) {
p, err := g.EarleyParse(text, starts...)
if err != nil {
return nil, err
}
return []*Parse{p}, nil
}, text, starts...)
}
// ParseAny returns parse trees for rule <start> at any position
func (g *Grammar) ParseAny(text string, starts ...string) ([]*Node, error) {
return g.extract(
func(text string, starts ...string) ([]*Parse, error) {
p, err := g.EarleyParseAny(text, starts...)
if err != nil {
return nil, err
}
return []*Parse{p}, nil
}, text, starts...)
}
// ExtractMaxAll extracts all parse trees in text for rule <start>
func (g *Grammar) ExtractMaxAll(
text string, starts ...string) ([]*Node, error) {
return g.extract(g.EarleyParseMaxAll, text, starts...)
}
// ExtractAll extracts all parse trees in text for rule <start>
func (g *Grammar) ExtractAll(text string, starts ...string) ([]*Node, error) {
return g.extract(g.EarleyParseAll, text, starts...)
}
func (g *Grammar) extract(f func(string, ...string) ([]*Parse, error),
text string, starts ...string) ([]*Node, error) {
ps, err := f(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
}