Skip to content
Permalink
0d82ff1dc4
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
83 lines (63 sloc) 2.09 KB
package kb.howtokb.clustering.sim;
import gnu.trove.TLongHashSet;
public class ActivityWordCategorySim {
/*
public static void main(String[] args) throws Exception {
SparseSims distSparse = new SparseSims(0.5f);
// need: ids of activity frames and ActivityWordsCategory
//Test load all activity frame
ActivityWordsCategory[] arr = new ActivityWordsCategory[1292250];
String inputfile = "/var/tmp/cxchu/data-wordnet/act-frame.json";
JSONParser parser = new JSONParser();
try (BufferedReader br = new BufferedReader(new FileReader(inputfile))) {
String sCurrentLine;
int i=0;
while ((sCurrentLine = br.readLine()) != null) {
Object obj = parser.parse(sCurrentLine);
JSONObject jsonObject = (JSONObject) obj;
ActivityFrame newframe = JsonToActivityFrame.jsonToActivityFrame(jsonObject);
ActivityWordsCategory tmp = new ActivityWordsCategory(newframe.getID(),
Integer.parseInt(newframe.getActivity().getCategoryID()), newframe.getActivity().getVerb() + ";" + newframe.getActivity().getObject());
System.out.println(i);
arr[i++] = tmp;
}
}
for (int i = 0; i < arr.length; i++){
ActivityWordsCategory e1 = arr[i];
// Only store the upper triangular matrix.
for (int j = i + 1; j < arr.length; j++)
distSparse.set(i, j, (float) Word2VecRunner.simPair(e1.getActivityStrong(), arr[j].getActivityStrong()));
}
System.out.println("Done!");
}
*/
/**
* Only stores the upper triangular matrix.
*
* @author cxchu
*
*/
public static class SparseSims {
private TLongHashSet simPairs;
private float threshold;
public SparseSims(float threshold) {
simPairs = new TLongHashSet();
this.threshold = threshold;
}
public void set(int x, int y, float value) {
if (value < threshold)
return;
long key = intpairToLong(x, y);
simPairs.add(key);
}
public boolean get(int x, int y) {
long key = intpairToLong(x, y);
if (simPairs.contains(key))
return true;
return simPairs.contains(intpairToLong(y, x));
}
private long intpairToLong(int l, int r) {
return ((long) l << 32) + r;
}
}
}