Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
HowToKB/src/main/java/kb/howtokb/clustering/ActivityComponentSim.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
86 lines (74 sloc)
2.44 KB
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 kb.howtokb.clustering; | |
import java.util.Map; | |
import kb.howtokb.clustering.sim.ActivityWordCategorySim.SparseSims; | |
import kb.howtokb.utils.IDMap; | |
public class ActivityComponentSim implements ISimilarity<Integer> { | |
private double threshold = 0.0; | |
private SparseSims sims; // cache. | |
private IDMap<String, Integer> ids; | |
private ISimilarity<String> word2vecSim; | |
public ActivityComponentSim(double threshold, IDMap<String, Integer> ids, ISimilarity<String> word2vecSim) { | |
this.threshold = threshold; | |
this.sims = new SparseSims((float) this.threshold); | |
this.ids = ids; | |
this.word2vecSim = word2vecSim; | |
computeAllPairsSim(); | |
} | |
private void computeAllPairsSim() { | |
//Progress p = new Progress(1); | |
Integer[] ids = this.ids.values().toArray(new Integer[0]); | |
System.out.println("\n" + ids.length + " activity components. (one dot per activity neighborhood)"); | |
for (int i = 0; i < ids.length; i++) { | |
//p.next(); | |
int e1 = ids[i]; | |
for (int j = i; j < ids.length; j++) { | |
// cache before returning the result. | |
int e2 = ids[j]; | |
sims.set(e1, e2, (float) sim(e1, e2)); | |
} | |
} | |
} | |
@Override | |
public double sim(Integer e1, Integer e2) { | |
String word1 = ids.getKeyFromValue(e1); | |
String word2 = ids.getKeyFromValue(e2); | |
return word2vecSim.sim(word1, word2); | |
} | |
@Override | |
public Map<Integer, Double> getNeighbors(Integer e) { | |
System.err.println( | |
"neighborhood for an integer " + "activity (noun or verb) not yet implemented."); | |
return null; | |
} | |
@Override | |
public boolean simThreshold(Integer e1, Integer e2, double minthreshold) throws Exception { | |
return sim(e1, e2) >= minthreshold; | |
} | |
// Use this function once the object is completely constructed. | |
/** | |
* Also checks for e2,e1 | |
* | |
* @param e1 | |
* @param e2 | |
* @return if (e1,e2) are similar, return true. otherwise returns false | |
*/ | |
public boolean simFromCache(int e1, int e2) { | |
return sims.get(e1, e2); | |
} | |
/** | |
* Also checks for e2,e1 <br> | |
* <b>NOTE: ensure that word1 and word2 are either both verbs or both noun. | |
* And modified according to the ISim func. provided to the constructor</b> | |
* | |
* @param word1 | |
* go away => v_go (we only lookup "go" in word2vec) | |
* @param word2 | |
* move => v_move | |
* @return if (e1,e2) are similar, return true. otherwise returns false | |
*/ | |
public boolean simFromCache(String word1, String word2) { | |
int e1 = ids.getValueFromKey(word1); | |
int e2 = ids.getValueFromKey(word2); | |
return sims.get(e1, e2); | |
} | |
} |