-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag-set.go
executable file
·140 lines (130 loc) · 3.45 KB
/
tag-set.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
package linguo
import (
"container/list"
"fmt"
set "gopkg.in/fatih/set.v0"
"strconv"
"strings"
)
type TagSet struct {
PAIR_SEP string
MSD_SEP string
feat, val, name, nameInv, valInv map[string]string
direct map[string]*Pair
directInv map[*set.Set]string
shtagSize map[string]*list.List
}
const (
DIRECT_TRANSLATIONS = 1 + iota
DECOMPOSITION_RULES
)
func NewTagset(ftagset string) *TagSet {
this := &TagSet{
PAIR_SEP: "=",
MSD_SEP: "|",
feat: make(map[string]string),
val: make(map[string]string),
name: make(map[string]string),
nameInv: make(map[string]string),
direct: make(map[string]*Pair),
directInv: make(map[*set.Set]string),
valInv: make(map[string]string),
shtagSize: make(map[string]*list.List),
}
cfg := NewConfigFile(false, "##")
cfg.AddSection("DirectTranslations", DIRECT_TRANSLATIONS)
cfg.AddSection("DecompositionRules", DECOMPOSITION_RULES)
if !cfg.Open(ftagset) {
CRASH("Error opening file "+ftagset, MOD_TAG_SET)
}
line := ""
for cfg.GetContentLine(&line) {
items := Split(line, " ")
switch cfg.section {
case DIRECT_TRANSLATIONS:
{
tag := items[0]
shtag := items[1]
msd := ""
if len(items) > 2 {
msd = items[2]
}
this.direct[tag] = &Pair{shtag, msd}
this.directInv[set.New(msd, this.MSD_SEP)] = tag
break
}
case DECOMPOSITION_RULES:
{
cat := items[0]
shsz := items[1]
if len(items) > 2 {
pos := items[2]
this.name[cat] = pos
this.nameInv[pos] = cat
}
this.shtagSize[cat] = list.New()
tokens := strings.Split(shsz, ",")
for _, sitem := range tokens {
item, _ := strconv.Atoi(sitem)
this.shtagSize[cat].PushBack(item)
}
//TRACE(3, fmt.Sprintf("Read short tag size for %s (%s) %s\n", cat, pos, shsz), MOD_TAG_SET)
i := 1
if len(items) > 4 {
msd := items[4]
key := cat + "#" + strconv.Itoa(i)
k := strings.Split(msd, "/")
this.feat[key] = k[0]
this.feat[cat+"#"+k[0]] = strconv.Itoa(i)
v := strings.Split(k[1], ";")
for j := 0; j < len(v); j++ {
t := strings.Split(v[j], ":")
this.val[key+"#"+strings.ToUpper(t[0])] = t[1]
this.valInv[key+"#"+t[1]] = strings.ToUpper(t[0])
}
i++
}
break
}
default:
break
}
}
TRACE(1, "Module created successfully", MOD_HMM)
return this
}
func (this TagSet) GetShortTag(tag string) string {
TRACE(3, fmt.Sprintf("get short tag for %s\n", tag), MOD_TAG_SET)
p := this.direct[tag]
if p != nil {
TRACE(5, fmt.Sprintf(" Found direct entry %s\n", p.first.(string)), MOD_TAG_SET)
return p.first.(string)
} else {
s := this.shtagSize[tag[0:1]]
if s != nil {
if s.Len() == 1 {
ln := s.Front()
TRACE(5, fmt.Sprintf(" cutting 1st position sz=%s\n", strconv.Itoa(ln.Value.(int))), MOD_TAG_SET)
if ln.Value.(int) == 0 {
return tag
} else {
return tag[0:ln.Value.(int)]
}
} else {
shtg := ""
for k := s.Front(); k != nil; k = k.Next() {
if k.Value.(int) < len(tag) {
shtg += string(tag[k.Value.(int)])
} else {
WARNING(fmt.Sprintf("Tag %s too short for requested digits. Unchanged", tag), MOD_TAG_SET)
return tag
}
}
TRACE(5, " Extracting digits "+shtg+" from "+tag, MOD_HMM)
return shtg
}
}
}
WARNING("No rule to get short version of tag '"+tag+"'.", MOD_TAG_SET)
return tag
}