-
Notifications
You must be signed in to change notification settings - Fork 15
/
node_methods.go
129 lines (116 loc) · 2.65 KB
/
node_methods.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
package fmr
import (
"fmt"
"strconv"
"strings"
"zliu.org/goutil"
)
// Pos returns the corresponding pos of Node n in original text
func (n *Node) Pos() *Pos {
return n.p.Boundary(n.Value)
}
// Term returns the root Term of tree node
func (n *Node) Term() *Term {
if n.Value == nil { //|| n.Value.Rb == nil || len(n.Value.Rb.Terms) < 1 {
return nil
}
if n.Value.Term.Value == GammaRule {
return n.Value.Rb.Terms[0]
}
return n.Value.Term
}
// F returns the FMR signature of node
func (n *Node) F() *FMR {
if n.Value == nil || n.Value.Rb == nil || len(n.Value.Rb.Terms) < 1 {
return nil
}
if n.Value.Term.Value == GammaRule {
return n.Children[0].Value.Rb.F
}
return n.Value.Rb.F
}
// OriginalText returns the original text of Node n
func (n *Node) OriginalText() string {
pos := n.Pos()
return n.p.text[pos.StartByte:pos.EndByte]
}
// NL returns the normalized text of Node n
func (n *Node) NL() string {
var s []string
for i := n.Value.Start + 1; i <= n.Value.End; i++ {
s = append(s, n.p.columns[i].token.Text)
}
return goutil.Join(s)
}
// Tree returns the parsed tree of Node n
func (n *Node) Tree() map[string]interface{} {
if n.Value.Term.Value == GammaRule {
return n.Children[0].Tree()
}
if n.p == nil {
return nil
}
ret := map[string]interface{}{
"type": n.Term().Value,
"text": n.OriginalText(),
"pos": n.Pos(),
//"fmr": n.F(),
}
if n.Value.Rb == nil || n.Value.Rb.F == nil {
return ret
}
allTerminal := true
for _, node := range n.Children {
if node.Value.Term.Type == Terminal {
continue
}
s := node.Value.Term.Value
if strings.HasPrefix(s, "g_t_") || strings.HasPrefix(s, "l_t_") {
continue
}
allTerminal = false
break
}
if allTerminal {
return ret
}
subnodes := []interface{}{}
for _, node := range n.Children {
subnodes = append(subnodes, node.Tree())
}
ret["nodes"] = subnodes
return ret
}
// Bracketed returns the labeled bracket notation of Node
func (n *Node) Bracketed() string {
if n.Value.Term.Value == GammaRule {
return n.Children[0].Bracketed()
}
if n.p == nil {
return ""
}
var b strings.Builder
fmt.Fprintf(&b, "[%s ", n.Term().Value)
allTerminal := true
for _, node := range n.Children {
if node.Value.Term.Type == Terminal {
continue
}
s := node.Value.Term.Value
if strings.HasPrefix(s, "g_t_") || strings.HasPrefix(s, "l_t_") {
continue
}
allTerminal = false
break
}
if allTerminal {
fmt.Fprintf(&b, "%s]", strconv.Quote(n.OriginalText()))
return b.String()
}
subnodes := []string{}
for _, node := range n.Children {
subnodes = append(subnodes, node.Bracketed())
}
fmt.Fprintf(&b, "%s]", strings.Join(subnodes, " "))
return b.String()
}