-
Notifications
You must be signed in to change notification settings - Fork 1
/
conllx_to_tikz_dep.go
217 lines (182 loc) · 3.87 KB
/
conllx_to_tikz_dep.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2012-2013 Tetsuo Kiso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
const kInitSentLength = 256
const separator = `\^`
var specialChars = []string{"{", "}", "$", "&", "%"}
var docOption = flag.String("doc-option", "standalone", "Option of the document class")
var depOption = flag.String("dep-option", "theme = simple, edge style={<-}", "Option for the dependency environment")
var depTxtOption = flag.String("deptxt-option",
`column sep=.3em,ampersand replacement=\^`, "Option for the deptext environment")
type Token struct {
Id int
Form string
Lemma string
Cpos string
Pos string
Feat string
Head int
Deprel string
Phead string
Pdeprel string
}
// type Sentence []Token
type Sentence struct {
Tokens []Token
Length int
}
func NewToken(seq []string) *Token {
if len(seq) != 10 {
return nil
}
id, err := strconv.Atoi(seq[0])
if err != nil {
log.Printf("Invalid sequence: %s\n", seq[0])
return nil
}
head, err := strconv.Atoi(seq[6])
if err != nil {
log.Printf("Invalid sequence: %s\n", seq[6])
return nil
}
return &Token{
id,
seq[1],
seq[2],
seq[3],
seq[4],
seq[5],
head,
seq[7],
seq[8],
seq[9],
}
}
func (t *Token) IsRoot() bool { return t.Head == 0 || t.Head == -1 }
func NewSentence() *Sentence { return &Sentence{make([]Token, kInitSentLength), 0} }
func (s *Sentence) Add(t Token) {
if s.Length >= cap(s.Tokens) {
newSlice := make([]Token, cap(s.Tokens)*2)
copy(newSlice, s.Tokens)
s.Tokens = newSlice
}
s.Tokens[s.Length] = t
s.Length++
}
func (s *Sentence) Forms() []string {
buf := make([]string, s.Length)
for i := 0; i < s.Length; i++ {
buf[i] = s.Tokens[i].Form
}
return buf
}
func (s *Sentence) String() string {
res := ""
for i := 0; i < s.Length; i++ {
if i > 0 {
res += " "
}
res += s.Tokens[i].Form
}
return res
}
func tokenize(l string) (seq []string) { return strings.Split(l, " ") }
func wrapDepText(s *Sentence) string { return strings.Join(s.Forms(), fmt.Sprintf(` %s `, separator)) + " \\\\" }
func wrapDepEdge(h, m int) string { return fmt.Sprintf(`\depedge{%d}{%d}{}`, h, m) }
func printHeader() {
fmt.Printf(`\documentclass{%s}
\usepackage{tikz-dependency}
\begin{document}
`, *docOption)
}
func printFooter() { fmt.Println(`\end{document}`) }
func printDep(s *Sentence) {
fmt.Printf(`\begin{dependency}[%s]
\begin{deptext}[%s]
`, *depOption, *depTxtOption)
fmt.Println(wrapDepText(s))
fmt.Println(`\end{deptext}`)
for i := 0; i < s.Length; i++ {
if s.Tokens[i].IsRoot() {
continue
}
fmt.Println(wrapDepEdge(s.Tokens[i].Head, s.Tokens[i].Id))
}
fmt.Println(`\end{dependency}`)
}
func replaceSpecial(s string) string {
t := s
for _, c := range specialChars {
t = strings.Replace(t, c, `\` + c, -1)
}
return t
}
func read(r io.Reader) {
rd := bufio.NewReader(r)
lineNum := 1
s := NewSentence()
for {
line, err := rd.ReadString('\n')
switch {
case err == io.EOF:
return
case err != nil:
log.Fatal(err)
}
if line[0] == '\n' {
printDep(s)
s = NewSentence()
continue
}
seq := tokenize(strings.TrimRight(line, "\n"))
if len(seq) == 0 {
log.Fatalf("Error: Illegal line at %d\n", lineNum)
}
seq[1] = replaceSpecial(seq[1])
t := NewToken(seq)
if t != nil {
s.Add(*t)
}
lineNum++
}
}
func open(file string) {
if file == "-" {
read(os.Stdin)
return
}
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
read(f)
}
func main() {
flag.Parse()
var file string
switch {
case flag.NArg() == 0:
file = "-"
case flag.NArg() == 1:
file = flag.Arg(0)
case flag.NArg() > 2:
fmt.Println("Usage: ./conllx_to_tikz_dep [options] file")
flag.PrintDefaults()
os.Exit(1)
}
printHeader()
open(file)
printFooter()
}