-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathframe_api.go
148 lines (137 loc) · 3.54 KB
/
frame_api.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
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
package fmr
import (
"fmt"
)
// FrameFMR parses NL text to FMR
func (g *Grammar) FrameFMR(text string) ([]string, error) {
return g.FrameFMRWithContext("", text)
}
// FrameFMRWithContext parses NL text to FMR
func (g *Grammar) FrameFMRWithContext(context, text string) ([]string, error) {
frames, err := g.MatchFramesWithContext(context, text)
if err != nil {
return nil, err
}
var ret []string
for k, v := range frames {
f := g.Frames[k.RuleName].Body[k.BodyID].F
terms := g.Frames[k.RuleName].Body[k.BodyID].Terms
var children []*Node
for _, term := range terms {
slots := v.Slots[term.Key()]
if slots == nil || len(slots) == 0 || len(slots[0].Trees) == 0 {
children = append(children, nil)
continue
}
children = append(children, slots[0].Trees[0])
}
n := &Node{}
str, err := n.fmrStr(f, children, "")
if err != nil {
return nil, err
}
ret = append(ret, str)
}
return ret, nil
}
// MatchFrames returns the matched frames for NL text
func (g *Grammar) MatchFrames(text string) (map[RbKey]*Frame, error) {
return g.MatchFramesWithContext("", text)
}
// MatchFramesWithContext returns the matched frames for NL text
func (g *Grammar) MatchFramesWithContext(
context, text string) (map[RbKey]*Frame, error) {
frames, starts, err := g.getCandidates(text)
if err != nil {
return nil, err
}
ps, err := g.EarleyParseAllWithContext(context, text, starts...)
if err != nil {
return nil, err
}
for _, p := range ps {
for _, finalState := range p.finalStates {
tag := p.Tag(finalState)
pos := p.Boundary(finalState)
trees := p.GetTrees(finalState)
if tag == "" || pos == nil {
return nil, fmt.Errorf("invalid parse")
}
slot := &Slot{*pos, trees}
ret := g.ruleIndex[tag]
if ret == nil {
continue
}
for rbKey := range ret.Frames {
if frames[rbKey] == nil {
frames[rbKey] = &Frame{make(map[uint64][]*Slot), false}
}
t := Term{Value: tag, Type: Nonterminal}
frames[rbKey].Slots[t.Key()] = append(frames[rbKey].Slots[t.Key()], slot)
if len(frames[rbKey].Slots) >=
len(g.Frames[rbKey.RuleName].Body[rbKey.BodyID].Terms) {
frames[rbKey].Complete = true
}
}
}
}
return frames, nil
}
func (g *Grammar) getCandidates(text string) (
map[RbKey]*Frame, []string, error) {
matches, err := g.trie.MultiMatch(text)
if err != nil {
return nil, nil, err
}
frames := map[RbKey]*Frame{}
rules := map[string]bool{}
for word, hits := range matches {
v := g.index[word]
if v == nil {
return nil, nil, fmt.Errorf("%s in trie but not in index", word)
}
for rbKey := range v.Frames {
if frames[rbKey] == nil {
frames[rbKey] = &Frame{make(map[uint64][]*Slot), false}
}
t := Term{Value: word, Type: Terminal}
for _, hit := range hits {
frames[rbKey].Slots[t.Key()] = append(frames[rbKey].Slots[t.Key()],
&Slot{Pos{hit.StartByte, hit.EndByte}, nil})
}
if len(frames[rbKey].Slots) >=
len(g.Frames[rbKey.RuleName].Body[rbKey.BodyID].Terms) {
frames[rbKey].Complete = true
}
}
for rbKey := range v.Rules {
rules[rbKey.RuleName] = true
}
}
var ruleList []string
for k := range rules {
ruleList = append(ruleList, k)
}
for {
if len(ruleList) == 0 {
break
}
r := ruleList[0]
ruleList = ruleList[1:]
ret := g.ruleIndex[r]
if ret == nil {
continue
}
for rbKey := range ret.Rules {
if !rules[rbKey.RuleName] {
ruleList = append(ruleList, rbKey.RuleName)
rules[rbKey.RuleName] = true
}
}
}
var starts []string
for k := range rules {
starts = append(starts, k)
}
return frames, starts, nil
}