-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordRepresentation.java
More file actions
134 lines (115 loc) · 4.1 KB
/
WordRepresentation.java
File metadata and controls
134 lines (115 loc) · 4.1 KB
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
/*
* Spencer Caplan
*/
import java.util.*;
import java.util.Map.Entry;
public class WordRepresentation {
private String myName = "";
private Map<String, Feature> myFeatures = new HashMap<String, Feature>();
private Map<String, Feature> levelToFeatureMap = new HashMap<String, Feature>();
private Map<String, String> levelToFeatureStringNameMap = new HashMap<String, String>();
private static double lowerBlockingThreshold;
public WordRepresentation(String name) {
myName = name;
}
public String getName() {
return myName;
}
public static void setBlockingThreshold(double cutoff) {
lowerBlockingThreshold = cutoff;
}
public void printRepresentation() {
System.out.println("Representation for label " + myName + ": ");
System.out.println("----------");
for (String name : myFeatures.keySet()) {
System.out.println(name + " " + (myFeatures.get(name)).getProminence());
}
}
public void clearBlockedFeatures() throws IllegalStateException{
// iterate over the features in currRep -- if there exists more than one feature at a given level then deal with it
String basicName = "";
String subName = "";
String superName = "";
Map<String, Feature> safeCopyMyFeatures = new HashMap<String, Feature>(myFeatures);
for (String currName : safeCopyMyFeatures.keySet()) {
// check level
String currLevel = (myFeatures.get(currName)).getLevel();
List<String> toDelete = new ArrayList<String>();
if (currLevel.equals("super")) {
if (!superName.equals("")) {
toDelete = checkFeatPair(safeCopyMyFeatures.get(currName), safeCopyMyFeatures.get(superName));
} else {
superName = currName;
}
} else if (currLevel.equals("basic")) {
if (!basicName.equals("")) {
toDelete = checkFeatPair(safeCopyMyFeatures.get(currName), safeCopyMyFeatures.get(basicName));
} else {
basicName = currName;
}
} else if (currLevel.equals("subordinate")) {
if (!subName.equals("")) {
toDelete = checkFeatPair(safeCopyMyFeatures.get(currName), safeCopyMyFeatures.get(subName));
} else {
subName = currName;
}
} else {
System.out.println("Undefined Feature: " + currLevel);
throw new IllegalStateException();
}
// remove whatever is in "toDelete"
for (String featNameToRemove : toDelete) {
// System.out.println(featNameToRemove + " was blocked in " + myName);
myFeatures.remove(featNameToRemove);
}
}
}
private static List<String> checkFeatPair(Feature featOne, Feature featTwo) {
double featOneProm = featOne.getProminence();
double featTwoProm = featTwo.getProminence();
List<String> blockedFeatures = new ArrayList<String>();
if ((featOneProm >= lowerBlockingThreshold) && (featTwoProm >= lowerBlockingThreshold)) {
blockedFeatures.add(featOne.getName());
blockedFeatures.add(featTwo.getName());
} else if ((featOneProm >= lowerBlockingThreshold) && (featTwoProm < lowerBlockingThreshold)) {
blockedFeatures.add(featTwo.getName());
} else if ((featOneProm < lowerBlockingThreshold) && (featTwoProm >= lowerBlockingThreshold)) {
blockedFeatures.add(featOne.getName());
}
return blockedFeatures;
}
public void addFeature(Feature feat) {
myFeatures.put(feat.getName(), feat);
}
public Map<String, Feature> getFeatures() {
return myFeatures;
}
public void combineRep(Map<String, Feature> toAddIn) {
for (String featName : toAddIn.keySet()) {
Feature featToAdd = toAddIn.get(featName);
this.addFeature(featToAdd);
}
}
public void combineRepAdditive(Map<String, Feature> toAddIn) {
for (String featName : toAddIn.keySet()) {
Feature featToAdd = toAddIn.get(featName);
double oldProminence = 0.0;
if (myFeatures.containsKey(featName)) {
oldProminence = (myFeatures.get(featName)).getProminence();
}
double newProminence = oldProminence + (featToAdd.getProminence());
if (newProminence > 1.0) {
newProminence = 1.0;
}
featToAdd.setProminence(newProminence);
this.addFeature(featToAdd);
}
}
public double checkContains(String featName) {
if (myFeatures.containsKey(featName)) {
return (myFeatures.get(featName)).getProminence();
} else {
return 0.0;
}
}
}