com.olapdb.obase.utils.VectorUtil Maven / Gradle / Ivy
The newest version!
package com.olapdb.obase.utils;
import com.olapdb.obase.data.Tag;
import java.util.Hashtable;
import java.util.List;
public class VectorUtil {
public static double relation(double[] v1, double[] v2){
if(v1.length != v2.length)
return 0;
return dotProduct(v1, v2)/(sizeOf(v1)*sizeOf(v2));
}
public static double dotProduct(double[] v1, double[] v2)
{
double ret = 0;
for(int i=0; i a, List b){
double length_a = 0;
double length_b = 0;
double product = 0;
Hashtable words = new Hashtable();
for(Tag t:a){
length_a += t.weight*t.weight;
words.put(t.word, t.weight);
}
for(Tag t:b){
length_b += t.weight*t.weight;
product += t.weight*words.getOrDefault(t.word, 0.0);
}
if(length_a == 0 || length_b == 0||product == 0)
return 0;
// a.forEach(t->words.put(t.word, t.weight));
// double la = a.stream().map(t->t.weight*t.weight).reduce(0.0, (x,y)->x+y);
// double lb = b.stream().map(t->t.weight*t.weight).reduce(0.0, (x,y)->x+y);
// double lc = b.stream()
// .filter(t->words.containsKey(t.word))
// .map(t->t.weight*words.get(t.word))
// .reduce(0.0, (x,y)->x+y);
return product/Math.sqrt(length_a*length_b);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy