aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/main/java/com/yahoo/fsa/segmenter/Segments.java
blob: e3bfe956a5cda305a51fc3f264a5da4e3b4d03ef (plain) (blame)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fsa.segmenter;

import java.util.LinkedList;

/**
 * Contains the segmentation() method.
 *
 * @author  Peter Boros
 */
public class Segments extends LinkedList<Segment> {

    public final static int SEGMENTATION_WEIGHTED            =  0;
    public final static int SEGMENTATION_WEIGHTED_BIAS10     =  1;
    public final static int SEGMENTATION_WEIGHTED_BIAS20     =  2;
    public final static int SEGMENTATION_WEIGHTED_BIAS50     =  3;
    public final static int SEGMENTATION_WEIGHTED_BIAS100    =  4;
    public final static int SEGMENTATION_WEIGHTED_LEFTMOST   =  5;
    public final static int SEGMENTATION_WEIGHTED_RIGHTMOST  =  6;
    public final static int SEGMENTATION_WEIGHTED_LONGEST    =  7;
    public final static int SEGMENTATION_LEFTMOST_LONGEST    =  8;
    public final static int SEGMENTATION_LEFTMOST_WEIGHTED   =  9;
    public final static int SEGMENTATION_RIGHTMOST_LONGEST   = 10;
    public final static int SEGMENTATION_RIGHTMOST_WEIGHTED  = 11;
    public final static int SEGMENTATION_LONGEST_WEIGHTED    = 12;
    public final static int SEGMENTATION_LONGEST_LEFTMOST    = 13;
    public final static int SEGMENTATION_LONGEST_RIGHTMOST   = 14;
    public final static int SEGMENTATION_METHODS             = 15;

    private String[] _tokens;
    private int      _size;
    private int[][]  _map;

    public Segments(String[] tokens)
    {
        _tokens = tokens;
        _size = tokens.length;
        _map = new int[_size+1][_size+1];
        for(int i=0; i<=_size; i++){
            for(int j=0; j<=_size; j++){
                _map[i][j]=-1;
            }
        }
    }

    @Override
    public boolean add(Segment s)
    {
        var result = super.add(s);
        _map[s.beg()][s.end()]=super.size()-1;
        return result;
    }

    private void addMissingSingles()
    {
        for(int i=0; i<_size; i++){
            if(_map[i][i+1]==-1){
                super.add(new Segment(i,i+1,0));
                _map[i][i+1]=super.size()-1;
            }
        }
    }

    private void reMap()
    {
        for(int i=0; i<=_size; i++){
            for(int j=0; j<=_size; j++){
                _map[i][j]=-1;
            }
        }
        for(int i=0; i<super.size(); i++){
            _map[beg(i)][end(i)] = i;
        }
    }

    public String sgm(int idx)
    {
        if(idx<0 || idx>=super.size()){
            return null;
        }
        String s = new String(_tokens[super.get(idx).beg()]);
        for(int i = super.get(idx).beg() + 1; i < super.get(idx).end(); i++){
            s += " " + _tokens[i];
        }
        return s;
    }

    public int beg(int idx)
    {
        if(idx<0 || idx>=super.size()){
            return -1;
        }
        return super.get(idx).beg();
    }

    public int end(int idx)
    {
        if(idx<0 || idx>=super.size()){
            return -1;
        }
        return super.get(idx).end();
    }

    public int len(int idx)
    {
        if(idx<0 || idx>=super.size()){
            return -1;
        }
        return super.get(idx).len();
    }

    public int conn(int idx)
    {
        if(idx<0 || idx>=super.size()){
            return -1;
        }
        return super.get(idx).conn();
    }

    @SuppressWarnings("fallthrough")
    public Segments segmentation(int method)
    {
        Segments smnt = new Segments(_tokens);

        addMissingSingles();

        int maxsc, id, bestid=-1, bias=0, c, pos, bestval, temp=0, next=-1;
        int[] maxScore = new int[super.size()];
        int[] nextid   = new int[super.size()];
        for(int i=0;i<nextid.length;i++){
            nextid[i]=-1;
        }

        switch(method){
        case SEGMENTATION_WEIGHTED_BIAS100:
            bias+=50;
        case SEGMENTATION_WEIGHTED_BIAS50:
            bias+=30;
        case SEGMENTATION_WEIGHTED_BIAS20:
            bias+=10;
        case SEGMENTATION_WEIGHTED_BIAS10:
            bias+=10;
        case SEGMENTATION_WEIGHTED:
            bestid=-1;
            for(int i=_tokens.length;i>=0;i--){
                bestid=-1;maxsc=0;
                for(int j=i+1;j<=_tokens.length;j++){
                    id=_map[i][j];
                    if(id>=0 && maxScore[id]+1>maxsc) {
                        bestid=id;
                        maxsc=maxScore[id]+1;
                    }
                }
                if(maxsc>0){
                    maxsc--;
                }
                for(int j=0;j<i;j++){
                    id=_map[j][i];
                    if(id>=0){
                        nextid[id] = bestid;
                        c = conn(id);
                        if(i-j<=1){
                            maxScore[id] = maxsc;
                        }
                        else if(bias>0){
                            maxScore[id] = maxsc + ((100+(i-j-2)*bias)*c)/100;
                        }
                        else{
                            maxScore[id] = maxsc + c;
                        }
                    }
                }
            }
            id = bestid;
            while(id!=-1){
                smnt.add(super.get(id));
                id=nextid[id];
            }
            break;
        case SEGMENTATION_LEFTMOST_LONGEST:
        case SEGMENTATION_LEFTMOST_WEIGHTED:
            pos = 0;
            while(pos<_tokens.length){
                bestid = -1; bestval = -1;
                for(int i=pos+1;i<=_tokens.length;i++){
                    id = _map[pos][i];
                    if(id>=0 &&
                       (method==SEGMENTATION_LEFTMOST_LONGEST ||
                        (temp=(len(id)>1)? conn(id) :0)>bestval) ){
                        bestid = id;
                        bestval = temp;
                        next = i;
                    }
                }
                smnt.add(super.get(bestid));
                pos=next;
            }
            break;
        case SEGMENTATION_RIGHTMOST_LONGEST:
        case SEGMENTATION_RIGHTMOST_WEIGHTED:
            pos = _tokens.length;
            while(pos>0){
                bestid = -1; bestval = -1;
                for(int i=pos-1;i>=0;i--){
                    id = _map[i][pos];
                    if(id>=0 &&
                       (method==SEGMENTATION_RIGHTMOST_LONGEST ||
                        (temp=(len(id)>1)? conn(id) :0)>bestval) ){
                        bestid = id;
                        bestval = temp;
                        next = i;
                    }
                }
                smnt.addFirst(super.get(bestid));
                pos=next;
            }
            smnt.reMap();
            break;
        case SEGMENTATION_LONGEST_WEIGHTED:
        case SEGMENTATION_LONGEST_LEFTMOST:
        case SEGMENTATION_LONGEST_RIGHTMOST:
        case SEGMENTATION_WEIGHTED_LONGEST:
        case SEGMENTATION_WEIGHTED_LEFTMOST:
        case SEGMENTATION_WEIGHTED_RIGHTMOST:
            buildSegmentationRecursive(method,smnt,0,_tokens.length);
            break;
        }

        return smnt;
    }

    private void buildSegmentationRecursive(int method, Segments smnt, int b, int e)
    {
        int bestid, bestval1, bestval2, temp;

        bestid=-1;bestval1=-1;bestval2=-1;
        for(int i=0;i<super.size();i++){
            if(b<=beg(i) && e>=end(i)){
                switch(method){
                case SEGMENTATION_LONGEST_WEIGHTED:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && conn(i)>bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=conn(i);
                    }
                    break;
                case SEGMENTATION_LONGEST_LEFTMOST:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && beg(i)<bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=beg(i);
                    }
                    break;
                case SEGMENTATION_LONGEST_RIGHTMOST:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && end(i)>bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=end(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_LONGEST:
                    temp = (len(i)>1)?conn(i):0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && len(i)>bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=len(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_LEFTMOST:
                    temp = (len(i)>1)? conn(i) :0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && beg(i)<bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=beg(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_RIGHTMOST:
                    temp = len(i)>1?conn(i):0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && end(i)>bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=end(i);
                    }
                    break;
                default: // dummy defult pick first possible
                    if(bestid<0){
                        bestid=i;
                    }
                    break;
                }
            }
        }
        if(bestid<0) {
            return; // this should never happen, as all one-word segments are created
        }

        if(b<beg(bestid)){
            buildSegmentationRecursive(method,smnt,b,beg(bestid));
        }

        // add segment
        smnt.add(super.get(bestid));

        // check right side
        if(e>end(bestid)){
            buildSegmentationRecursive(method,smnt,end(bestid),e);
        }
    }

}