forked from habeanf/yap
-
Notifications
You must be signed in to change notification settings - Fork 20
/
mapping.go
73 lines (67 loc) · 1.69 KB
/
mapping.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
package mapping
import (
"yap/nlp/parser/disambig"
nlp "yap/nlp/types"
"fmt"
"io"
"os"
// "log"
)
func WriteMorph(writer io.Writer, morph *nlp.EMorpheme, curMorph, curToken int) {
writer.Write([]byte(fmt.Sprintf("%d\t%d\t", curMorph, curMorph+1)))
writer.Write([]byte(morph.Form))
writer.Write([]byte{'\t'})
if len(morph.Lemma) > 0 {
writer.Write([]byte(morph.Lemma))
} else {
writer.Write([]byte{'_'})
}
writer.Write([]byte{'\t'})
writer.Write([]byte(morph.CPOS))
writer.Write([]byte{'\t'})
writer.Write([]byte(morph.POS))
writer.Write([]byte{'\t'})
if len(morph.FeatureStr) == 0 {
writer.Write([]byte{'_'})
} else {
writer.Write([]byte(morph.FeatureStr))
}
writer.Write([]byte{'\t'})
writer.Write([]byte(fmt.Sprintf("%d\n", curToken+1)))
}
func Write(writer io.Writer, mappedSents []interface{}) {
var curMorph int
for _, mappedSent := range mappedSents {
curMorph = 0
for i, mapping := range mappedSent.(*disambig.MDConfig).Mappings {
// log.Println("At token", i, mapping.Token)
if mapping.Token == nlp.ROOT_TOKEN {
continue
}
// if mapping.Spellout != nil {
// log.Println("\t", mapping.Spellout.AsString())
// } else {
// log.Println("\t", "*No spellout")
// }
for _, morph := range mapping.Spellout {
if morph == nil {
// log.Println("\t", "Morph is nil, continuing")
continue
}
WriteMorph(writer, morph, curMorph, i)
// log.Println("\t", "At morph", j, morph.Form)
curMorph++
}
}
writer.Write([]byte{'\n'})
}
}
func WriteFile(filename string, mappedSents []interface{}) error {
file, err := os.Create(filename)
defer file.Close()
if err != nil {
return err
}
Write(file, mappedSents)
return nil
}