Created
March 16, 2014 01:15
-
-
Save brendano/9576940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package d; | |
import java.util.*; | |
import java.util.Map.Entry; | |
public class TermVector { | |
public Map<String,Double> map; | |
double totalCount = 0; | |
TermVector() { | |
map = new HashMap<>(); | |
} | |
public void increment(String term, double value) { | |
ensure0(term); | |
map.put(term, map.get(term) + value); | |
totalCount += value; | |
} | |
public void increment(String term) { | |
increment(term, 1.0); | |
} | |
public void addInPlace(TermVector other) { | |
for (String k : other.map.keySet()) { | |
ensure0(k); | |
increment(k, other.value(k)); | |
} | |
} | |
public double value(String term) { | |
if (!map.containsKey(term)) return 0; | |
return map.get(term); | |
} | |
public Set<String> support() { | |
// tricky. it would be safer to check for zero-ness | |
return map.keySet(); | |
} | |
public TermVector copy() { | |
TermVector ret = new TermVector(); | |
ret.map = new HashMap<>(this.map); | |
ret.totalCount = this.totalCount; | |
return ret; | |
} | |
// static public TermVector sum(TermVector u, TermVector v) { | |
// TermVector ret = u.copy(); | |
// ret.addInPlace(v); | |
// return ret; | |
// } | |
/** helper: ensure that 'term' exists in the map */ | |
void ensure0(String term) { | |
if (!map.containsKey(term)) { | |
map.put(term, 0.0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment