File size: 3,255 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
package org.maltparser.ml.lib;

import java.io.Serializable;


import org.maltparser.core.helper.HashMap;

/**
 * The purpose of the feature map is to map MaltParser's column based features together with the symbol code from the symbol table to 
 * unique indices suitable for liblinear and libsvm.  A feature column position are combined together with the symbol code in a 
 * 64-bit key (Long), where 16 bits are reserved for the position and 48 bits are reserved for the symbol code.  
 * 
 * @author Johan Hall
 *
 */
public class FeatureMap  implements Serializable {
	private static final long serialVersionUID = 7526471155622776147L;
	private final HashMap<Long,Integer> map;
	private int featureCounter;
	
	/**
	 * Creates a feature map and sets the feature counter to 1
	 */
	public FeatureMap() {
		map = new HashMap<Long, Integer>();
		this.featureCounter = 1;
	}
	
	/**
	 * Adds a mapping from a combination of the position in the column-based feature vector and the symbol code to 
	 * an index value suitable for liblinear and libsvm.
	 * 
	 * @param featurePosition a position in the column-based feature vector
	 * @param code a symbol code
	 * @return the index value 
	 */
	public int addIndex(int featurePosition, int code) {
		final long key = ((((long)featurePosition) << 48) | (long)code);
		Integer index = map.get(key);
		if (index == null) {
			index = featureCounter++;
			map.put(key, index);
		}
		return index.intValue();
	}
	
	/**
	 * Return
	 * 
	 * @param featurePosition the position in the column-based feature vector
	 * @param code the symbol code suitable for liblinear and libsvm
	 * @return the index value if it exists, otherwise -1
	 */
	public int getIndex(int featurePosition, int code) {
		final Integer index = map.get(((((long)featurePosition) << 48) | (long)code));
		return (index == null)?-1:index;
	}
	
	
	public int addIndex(int featurePosition, int code1, int code2) {
		final long key = ((((long)featurePosition) << 48) | (((long)code1) << 24) | (long)code2);
		Integer index = map.get(key);
		if (index == null) {
			index = featureCounter++;
			map.put(key, index);
		}
		return index.intValue();
	}
	
	public int setIndex(long key, int index) {
		return map.put(key, index);
	}
	
	public int decrementIndex(Long key) {
		Integer index = map.get(key);
		if (index != null) {
			map.put(key, index - 1);
		}
		return (index != null)?index - 1 : -1;
	}
	
	public void decrementfeatureCounter() {
		featureCounter--;
	}
	
	public Integer removeIndex(long key) {
		return map.remove(key);
	}
	
	public int getIndex(int featurePosition, int code1, int code2) {
		final Integer index = map.get(((((long)featurePosition) << 48) | (((long)code1) << 24) | (long)code2));
		return (index == null)?-1:index;
	}
		
	/**
	 * @return the size of the map
	 */
	public int size() {
		return map.size();
	}
	
	
	public Long[] reverseMap() {
		Long[] reverseMap = new Long[map.size() +1];

		for (Long key : map.keySet()) {
			reverseMap[map.get(key)] = key;
		}
		return reverseMap;
	}
	
	
	
	public void setFeatureCounter(int featureCounter) {
		this.featureCounter = featureCounter;
	}

	/**
	 * @return the current value of the feature counter.
	 */
	public int getFeatureCounter() {
		return featureCounter;
	}
}