cluster.mx raw
1 package transdb
2
3 import (
4 "git.smesh.lol/iskra"
5 "git.smesh.lol/iskradb/lattice"
6 )
7
8 // Re-export cluster types from iskra.
9 type ClusterType = iskra.ClusterType
10 type Cluster = iskra.Cluster
11
12 const (
13 ClusterNPSubj = iskra.ClusterNPSubj
14 ClusterNPObj = iskra.ClusterNPObj
15 ClusterVP = iskra.ClusterVP
16 ClusterPP = iskra.ClusterPP
17 ClusterMod = iskra.ClusterMod
18 )
19
20 // jaParticleSet: JA boundary-marking particles for ParseClusters.
21 var jaParticleSet map[string]bool
22
23 // jaDefaultRole: fallback roles when Bcooccur is not yet populated.
24 var jaDefaultRole map[string]uint8
25
26 var _clusterInitDone bool
27
28 func clusterInitOnce() {
29 if _clusterInitDone {
30 return
31 }
32 _clusterInitDone = true
33
34 jaParticleSet = map[string]bool{
35 "は": true, "が": true, "を": true,
36 "に": true, "で": true, "と": true,
37 "から": true, "まで": true, "へ": true,
38 "より": true, "も": true,
39 }
40
41 jaDefaultRole = map[string]uint8{
42 "は": RoleNPSubjTopic,
43 "が": RoleNPSubjGram,
44 "を": RoleNPObjDirect,
45 "から": RolePPSource,
46 "まで": RolePPLimit,
47 "と": RolePPComitative,
48 "に": RoleNPDative,
49 "で": RolePPInstrumental,
50 "へ": RolePPLocative,
51 "より": RolePPSource,
52 "も": RoleNPSubjTopic,
53 }
54 }
55
56 // jaClusterHooks constructs the ClusterHooks for JA parsing.
57 func jaClusterHooks(tree *lattice.Tree) iskra.ClusterHooks {
58 clusterInitOnce()
59 translateInitOnce()
60 KeyInitOnce()
61 return iskra.ClusterHooks{
62 IsParticle: func(tok string) bool { return jaParticleSet[tok] },
63 LookupRole: func(tok string, npFlags uint64) uint8 {
64 return LookupParticleRole(tree, LangJA, tok, npFlags)
65 },
66 HasVerb: func(tok string) bool { return isJAVerb(tree, tok) },
67 AccumFlags: func(tokens []string) uint64 {
68 var flags uint64
69 for _, tok := range tokens {
70 for _, b := range ActiveBranches {
71 if ri := tree.LookupRecIdx(b, MakeKey(LangJA, 0, tok)); ri != lattice.NullRec {
72 if rec := tree.GetRecord(ri); rec != nil {
73 flags |= GetSemanticFromDataFile(rec)
74 }
75 break
76 }
77 }
78 }
79 return flags
80 },
81 FilterContent: func(tokens []string) []string {
82 var out []string
83 for _, tok := range tokens {
84 if !jaFunctionWord[tok] {
85 out = append(out, tok)
86 }
87 }
88 return out
89 },
90 DefaultRole: func(tok string) uint8 { return jaDefaultRole[tok] },
91 }
92 }
93
94 // ParseClusters segments a pre-tokenized JA sequence into phrase clusters.
95 func ParseClusters(tokens []string, tree *lattice.Tree, lang uint8) []*Cluster {
96 hooks := jaClusterHooks(tree)
97 return iskra.ParseClusters(tokens, hooks)
98 }
99
100 // clusterHeadLookup finds the target form and morph state for a source token.
101 func clusterHeadLookup(tree *lattice.Tree, pool []byte, tok string, srcLang uint8, coord uint64) (form string, state uint8) {
102 order := defaultBranchOrder
103 if srcLang == LangJA {
104 order = branchOrderJA(coord)
105 }
106 for _, c := range RelaxCoord(coord) {
107 key := MakeKey(srcLang, c, tok)
108 for _, b := range order {
109 ri := tree.LookupRecIdx(lattice.Branch(b), key)
110 if ri == lattice.NullRec {
111 continue
112 }
113 rec := tree.GetRecord(ri)
114 if rec == nil {
115 continue
116 }
117 if rec.Link[0] == lattice.NullRec {
118 break
119 }
120 dst := tree.GetRecord(rec.Link[0])
121 if dst == nil {
122 break
123 }
124 if f := FormFromInline(dst, pool); f != "" {
125 return f, GetMorphState(rec)
126 }
127 break
128 }
129 }
130 if srcLang == LangJA {
131 ms := inferMorphState(tok)
132 for _, stem := range verbStems(tok) {
133 sCoord := PackCoord(0, 0, 0, uint64(ms), 0, 0, 0)
134 if f, _ := clusterHeadLookup(tree, pool, stem, srcLang, sCoord); f != "" {
135 return f, ms
136 }
137 }
138 }
139 return "", 0
140 }
141
142 // TranslateCluster translates a cluster's tokens and fills c.Trans.
143 func TranslateCluster(c *Cluster, tree *lattice.Tree, pool []byte, srcLang, dstLang uint8) {
144 for _, nc := range c.Nested {
145 TranslateCluster(nc, tree, pool, srcLang, dstLang)
146 }
147
148 hooks := jaClusterHooks(tree)
149 content := hooks.FilterContent(c.Tokens)
150
151 var morphState uint8
152 if c.Kind == ClusterVP && !c.Copular && len(content) > 0 {
153 head := content[len(content)-1]
154 if srcLang == LangJA {
155 morphState = inferMorphState(head)
156 }
157 }
158 coord := PackCoord(c.Flags, 0, 0, uint64(morphState), 0, 0, 0)
159
160 headIdx := len(content) - 1
161 if srcLang == LangEN || headIdx < 0 {
162 headIdx = 0
163 }
164
165 headTrans := ""
166 headState := uint8(0)
167 if len(content) > 0 {
168 headTrans, headState = clusterHeadLookup(tree, pool, content[headIdx], srcLang, coord)
169 if headTrans == "" {
170 headTrans = content[headIdx]
171 } else if dstLang == LangEN {
172 headTrans = applyMorphEN(headTrans, headState)
173 }
174 }
175
176 modCoord := PackCoord(c.Flags, 0, 0, 0, 0, 0, 0)
177 var modTrans []string
178 for i, tok := range content {
179 if i == headIdx {
180 continue
181 }
182 t, _ := clusterHeadLookup(tree, pool, tok, srcLang, modCoord)
183 if t == "" {
184 t = tok
185 } else if dstLang == LangEN {
186 t = stripTo(t)
187 }
188 modTrans = append(modTrans, t)
189 }
190
191 rcTrans := ""
192 if len(c.Nested) > 0 {
193 var parts []string
194 for _, nc := range c.Nested {
195 if nc.Trans != "" {
196 parts = append(parts, nc.Trans)
197 }
198 }
199 rcTrans = joinWords(parts, dstLang)
200 }
201
202 var parts []string
203 if dstLang == LangEN {
204 if rcTrans != "" {
205 parts = append(parts, rcTrans)
206 }
207 parts = append(parts, modTrans...)
208 if headTrans != "" {
209 parts = append(parts, headTrans)
210 }
211 } else {
212 parts = append(parts, modTrans...)
213 if rcTrans != "" {
214 parts = append(parts, rcTrans)
215 }
216 if headTrans != "" {
217 parts = append(parts, headTrans)
218 }
219 }
220
221 c.Trans = joinWords(parts, dstLang)
222 }
223
224 // joinWords joins parts with spaces (EN) or no separator (JA).
225 func joinWords(parts []string, lang uint8) string {
226 var out []byte
227 first := true
228 for _, p := range parts {
229 if p == "" {
230 continue
231 }
232 if lang == LangEN && !first {
233 out = append(out, ' ')
234 }
235 out = append(out, []byte(p)...)
236 first = false
237 }
238 return string(out)
239 }
240
241 // ReorderClusters rearranges clusters from src to dst word order.
242 func ReorderClusters(clusters []*Cluster, srcOrder, dstOrder uint8) []*Cluster {
243 return iskra.ReorderClusters(clusters, srcOrder, dstOrder)
244 }
245
246 // InsertMarkers assembles clusters into a target-language string.
247 func InsertMarkers(clusters []*Cluster, dstDesc LangDesc, dstLang uint8) string {
248 return iskra.InsertMarkers(clusters, dstDesc, dstLang)
249 }
250