forked from chewxy/lingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
177 lines (146 loc) · 3.47 KB
/
io.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
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
package lingo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
)
type dummyAnnotation struct {
POSTag `json:"POSTag"`
DependencyType `json:"Label"`
ID int `json:"ID"`
Head int `json:"Head"`
Value string `json:"Value"`
Lemma string `json:"Lemma"`
Stem string `json:"Stem"`
Cluster `json:"Cluster"`
Shape `json:"Shape"`
WordFlag `json:"WordFlat"`
}
// func (a *Annotation) MarshalText() ([]byte, error) {
// var buf bytes.Buffer
// if a.Head != nil {
// fmt.Fprintf(&buf, "%v(%q/%v-%d, %q/%v-%d)", a.DependencyType, a.Value, a.POSTag, a.ID, a.Head.Value, a.Head.POSTag, a.Head.ID)
// } else if a == rootAnnotation {
// fmt.Fprintf(&buf, "ROOT")
// } else {
// fmt.Fprintf(&buf, "%q/%v-%d", a.Value, a.POSTag, a.ID)
// }
// return buf.Bytes(), nil
// }
func (a *Annotation) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteRune('{')
fmt.Fprintf(&buf, "\"ID\": %d,", a.ID)
fmt.Fprintf(&buf, "\"Value\": %q,", a.Value)
fmt.Fprintf(&buf, "\"POSTag\": \"%v\",", a.POSTag)
fmt.Fprintf(&buf, "\"Label\": \"%v\"", a.DependencyType)
if a.Head != nil {
if a.Head == rootAnnotation {
fmt.Fprintf(&buf, ", \"Head\": -1000") // special signifier for root annotations
} else {
fmt.Fprintf(&buf, ", \"Head\": %d", a.HeadID())
}
}
if a.Lemma != "" {
fmt.Fprintf(&buf, ", \"Lemma\": %q", a.Lemma)
}
// Lowered is not serialized because it's a simple function call away
if a.Stem != "" {
fmt.Fprintf(&buf, ",\"Stem\": %q", a.Stem)
}
if a.Cluster > 0 {
fmt.Fprintf(&buf, ",\"Cluster\": %d", a.Cluster)
}
if a.Shape != "" {
fmt.Fprintf(&buf, ",\"Shape\": %q", a.Shape)
}
if a.WordFlag > 0 {
fmt.Fprintf(&buf, ",\"WordFlag\": %d", a.WordFlag)
}
buf.WriteRune('}')
return buf.Bytes(), nil
}
func (a *Annotation) UnmarshalJSON(b []byte) error {
if a == nil {
// error
return errors.Errorf("Cannot unmarshal json to a nul")
}
d := dummyAnnotation{}
if err := json.Unmarshal(b, &d); err != nil {
return err
}
a.Value = d.Value
a.POSTag = d.POSTag
a.DependencyType = d.DependencyType
a.ID = d.ID
a.Lemma = d.Lemma
a.Stem = d.Stem
a.Cluster = d.Cluster
a.Shape = d.Shape
a.WordFlag = d.WordFlag
return nil
}
func (as AnnotatedSentence) MarshalJSON() ([]byte, error) {
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
buf.WriteRune('[')
for i, a := range as {
if err := encoder.Encode(a); err != nil {
return nil, err
}
if i < len(as)-1 {
buf.WriteRune(',')
}
}
buf.WriteRune(']')
return buf.Bytes(), nil
}
func (as *AnnotatedSentence) UnmarshalJSON(b []byte) error {
dummies := make([]dummyAnnotation, 0)
if err := json.Unmarshal(b, &dummies); err != nil {
return err
}
asL := len(*as)
l := len(dummies)
if asL != l {
diff := l - asL
(*as) = append(*as, make(AnnotatedSentence, diff)...)
}
for i, d := range dummies {
a := (*as)[i]
if d.Value == "-ROOT-" {
(*as)[i] = rootAnnotation
continue
}
if a == nil {
a = new(Annotation)
}
a.Value = d.Value
a.POSTag = d.POSTag
a.DependencyType = d.DependencyType
a.ID = d.ID
a.Lemma = d.Lemma
a.Stem = d.Stem
a.Cluster = d.Cluster
a.Shape = d.Shape
a.WordFlag = d.WordFlag
(*as)[i] = a
}
// fix up head IDs
for i, d := range dummies {
a := (*as)[i]
head := d.Head
if head == -1000 {
a.SetHead(rootAnnotation)
} else {
a.SetHead((*as)[head])
}
}
// TODO: fix up other things
for _, a := range *as {
a.Lowered = strings.ToLower(a.Value)
}
return nil
}