File size: 7,579 Bytes
6f3ebfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package org.maltparser.ml.lib;

import java.io.Serializable;

import libsvm.svm_model;
import libsvm.svm_node;
import libsvm.svm_parameter;
import libsvm.svm_problem;


/**
 * <p>This class borrows code from libsvm.svm.java of the Java implementation of the libsvm package.
 * MaltLibsvmModel stores the model obtained from the training procedure. In addition to the original code the model is more integrated to
 * MaltParser. Instead of moving features from MaltParser's internal data structures to liblinear's data structure it uses MaltParser's data 
 * structure directly on the model. </p> 
 * 
 * @author Johan Hall
 *
 */
public class MaltLibsvmModel implements Serializable, MaltLibModel {
	private static final long serialVersionUID = 7526471155622776147L;
    public svm_parameter param;     // parameter                                                                                                                                                                                        
    public int nr_class;            // number of classes, = 2 in regression/one class svm                                                                                                                                               
	public int l;                   // total #SV                                                                                                                                                                                        
    public svm_node[][] SV; // SVs (SV[l])                                                                                                                                                                                              
    public double[][] sv_coef;      // coefficients for SVs in decision functions (sv_coef[k-1][l])                                                                                                                                     
    public double[] rho;            // constants in decision functions (rho[k*(k-1)/2])                                                                                                                                                 

    // for classification only
    public int[] label;             // label of each class (label[k])                                                                                                                                                                   
    public int[] nSV;               // number of SVs for each class (nSV[k])                                                                                                                                                            
                                // nSV[0] + nSV[1] + ... + nSV[k-1] = l   
    public int[] start;
    
    public MaltLibsvmModel(svm_model model, svm_problem problem) {
    	this.param = model.param;
    	this.nr_class = model.nr_class;
    	this.l = model.l;
    	this.SV = model.SV;
    	this.sv_coef = model.sv_coef;
    	this.rho = model.rho;
    	this.label = model.label;
    	this.nSV = model.nSV;
		start = new int[nr_class];
		start[0] = 0;
		for(int i=1;i<nr_class;i++) {
			start[i] = start[i-1]+nSV[i-1];
		}
    }
    
    public int[] predict(MaltFeatureNode[] x) { 
    	final double[] dec_values = new double[nr_class*(nr_class-1)/2];
		final double[] kvalue = new double[l];
		final int[] vote = new int[nr_class];
		int i;
		for(i=0;i<l;i++) {
			kvalue[i] = MaltLibsvmModel.k_function(x,SV[i],param);
		}
		for(i=0;i<nr_class;i++) {
			vote[i] = 0;
		}
		
		int p=0;
		for(i=0;i<nr_class;i++) {
			for(int j=i+1;j<nr_class;j++) {
				double sum = 0;
				int si = start[i];
				int sj = start[j];
				int ci = nSV[i];
				int cj = nSV[j];
			
				int k;
				double[] coef1 = sv_coef[j-1];
				double[] coef2 = sv_coef[i];
				for(k=0;k<ci;k++)
					sum += coef1[si+k] * kvalue[si+k];
				for(k=0;k<cj;k++)
					sum += coef2[sj+k] * kvalue[sj+k];
				sum -= rho[p];
				dec_values[p] = sum;					

				if(dec_values[p] > 0)
					++vote[i];
				else
					++vote[j];
				p++;
			}
		}
		
        final int[] predictionList = new int[nr_class];
        System.arraycopy(label, 0, predictionList, 0, nr_class);
		int tmp;
		int iMax;
		final int nc =  nr_class-1;
		for (i=0; i < nc; i++) {
			iMax = i;
			for (int j=i+1; j < nr_class; j++) {
				if (vote[j] > vote[iMax]) {
					iMax = j;
				}
			}
			if (iMax != i) {
				tmp = vote[iMax];
				vote[iMax] = vote[i];
				vote[i] = tmp;
				tmp = predictionList[iMax];
				predictionList[iMax] = predictionList[i];
				predictionList[i] = tmp;
			}
		}
		return predictionList;
    }
    
    
    public int predict_one(MaltFeatureNode[] x) { 
    	final double[] dec_values = new double[nr_class*(nr_class-1)/2];
		final double[] kvalue = new double[l];
		final int[] vote = new int[nr_class];
		int i;
		for(i=0;i<l;i++) {
			kvalue[i] = MaltLibsvmModel.k_function(x,SV[i],param);
		}
		for(i=0;i<nr_class;i++) {
			vote[i] = 0;
		}
		
		int p=0;
		for(i=0;i<nr_class;i++) {
			for(int j=i+1;j<nr_class;j++) {
				double sum = 0;
				int si = start[i];
				int sj = start[j];
				int ci = nSV[i];
				int cj = nSV[j];
			
				int k;
				double[] coef1 = sv_coef[j-1];
				double[] coef2 = sv_coef[i];
				for(k=0;k<ci;k++)
					sum += coef1[si+k] * kvalue[si+k];
				for(k=0;k<cj;k++)
					sum += coef2[sj+k] * kvalue[sj+k];
				sum -= rho[p];
				dec_values[p] = sum;					

				if(dec_values[p] > 0)
					++vote[i];
				else
					++vote[j];
				p++;
			}
		}
		
		
        int max = vote[0];
        int max_index = 0;
		for (i = 1; i < vote.length; i++) {
			if (vote[i] > max) {
				max = vote[i];
				max_index = i;
			}
		}

		return label[max_index];
    }
    
	static double dot(MaltFeatureNode[] x, svm_node[] y) {
		double sum = 0;
		final int xlen = x.length;
		final int ylen = y.length;
		int i = 0;
		int j = 0;
		while(i < xlen && j < ylen)
		{
			if(x[i].index == y[j].index)
				sum += x[i++].value * y[j++].value;
			else
			{
				if(x[i].index > y[j].index)
					++j;
				else
					++i;
			}
		}
		return sum;
	}
	
	static double powi(double base, int times) {
		double tmp = base, ret = 1.0;

		for(int t=times; t>0; t/=2)
		{
			if(t%2==1) ret*=tmp;
			tmp = tmp * tmp;
		}
		return ret;
	}
	
	static double k_function(MaltFeatureNode[] x, svm_node[] y, svm_parameter param) {
		switch(param.kernel_type)
		{
			case svm_parameter.LINEAR:
				return dot(x,y);
			case svm_parameter.POLY:
				return powi(param.gamma*dot(x,y)+param.coef0,param.degree);
			case svm_parameter.RBF:
			{
				double sum = 0;
				int xlen = x.length;
				int ylen = y.length;
				int i = 0;
				int j = 0;
				while(i < xlen && j < ylen)
				{
					if(x[i].index == y[j].index)
					{
						double d = x[i++].value - y[j++].value;
						sum += d*d;
					}
					else if(x[i].index > y[j].index)
					{
						sum += y[j].value * y[j].value;
						++j;
					}
					else
					{
						sum += x[i].value * x[i].value;
						++i;
					}
				}
		
				while(i < xlen)
				{
					sum += x[i].value * x[i].value;
					++i;
				}
		
				while(j < ylen)
				{
					sum += y[j].value * y[j].value;
					++j;
				}
		
				return Math.exp(-param.gamma*sum);
			}
			case svm_parameter.SIGMOID:
				return Math.tanh(param.gamma*dot(x,y)+param.coef0);
			case svm_parameter.PRECOMPUTED:
				return	x[(int)(y[0].value)].value;
			default:
				return 0;	// java
		}
	}
	
	public int[] getLabels() {
		if (label != null) {
			final int[] labels = new int[nr_class];
			for(int i=0;i<nr_class;i++) {
				labels[i] = label[i];
			}
			return labels;
		}
		return null;
	}
}