余弦相似度编程出错...求解决
不知道为什么答案就是出现"NaN"...我也找不到哪里错了...求各位大虾解救...package cosSimilarity;import java.util.ArrayList;
import java.util.Scanner;
public class Simiarity {
public static double calCos (String term1, String term2)
{
double calresult=0.0d;
ArrayList<String> wordspace =new ArrayList<String>();
String[] term1array =term1.split(" ");
for (int i=0; i<term1array.length; i++)
{
if (!wordspace.contains(term1array[i]))
{
wordspace.add(term1array[i]);
}
}
String[] term2array =term2.split(" ");
for (int j=0; j<term2array.length; j++)
{
if (!wordspace.contains(term2array[j]))
{
wordspace.add(term2array[j]);
}
for (int i1=0;i1<wordspace.size();i1++)
{
System.out.println(wordspace.get(i1));
}
}
//两术语词空间向量
ArrayList <Integer> vector1= new ArrayList <Integer>();
for (int i=0; i<wordspace.size(); i++)
{
vector1.add(new Integer(0));
}
for (int i=0; i<wordspace.size(); i++)
{
String word= wordspace.get(i);
for (int j=0; j<term1array.length; j++)
{
if (term1array[j].equals(word))
{
vector1.set(i, new Integer(vector1.get(i)));
}
}
}
//术语1词空间向量
ArrayList <Integer> vector2= new ArrayList <Integer>();
for (int i=0; i<wordspace.size(); i++)
{
vector2.add(new Integer(0));
}
for (int i=0; i<wordspace.size(); i++)
{
String word= wordspace.get(i);
for (int j=0; j<term2array.length; j++)
{
if (term2array[j].equals(word))
{
vector2.set(i, new Integer(vector2.get(i)));
}
}
}
//术语2词空间向量
double numerator=0.0d;
double denominator=0.0d;
double de1=0.0d;
double de2=0.0d;
for (int i=0; i<vector1.size(); i++)
{
numerator+= vector1.get(i)* vector2.get(i);
de1+= Math.pow(vector1.get(i), 2);
de2+= Math.pow(vector2.get(i), 2);
}
denominator=Math.sqrt(de1*de2);
return calresult=(numerator/denominator);
//公式计算
}
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("please input two terms:");
String term1 = s.nextLine();
String term2 = s.nextLine();
System.out.println("the Cosin Similarity is:");
System.out.println(Simiarity.calCos (term1,term2));
}
}