| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 414 人关注过本帖
标题:余弦相似度编程出错...求解决
只看楼主 加入收藏
学java
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-19
收藏
 问题点数:0 回复次数:0 
余弦相似度编程出错...求解决
不知道为什么答案就是出现"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));
     }
      
}



搜索更多相关主题的帖子: import public package 编程 double 
2012-10-19 10:35
快速回复:余弦相似度编程出错...求解决
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026316 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved