统计各推荐组合中的某个商品与预测商品的相似度(最高相似度)

CommonCount2.java统计各推荐组合中的某个商品与预测商品的相似度(最高相似度)输出结果:simila.txt

package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class CommonCount2 {

    public static int count(String[] s1,String[] s2)
    {
        int count1=0;
        for(int k=0;k<s2.length;k++)
        {
            for(int j=0;j<s1.length;j++)
            {
                if(s2[k].equals(s1[j]))
                    count1++;
            }
        }
        return count1;
    }
       public static void appendMethod(String fileName, String content) 
       {
           try 
           {
               //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
               FileWriter writer = new FileWriter(fileName, true);
               writer.write(content);
               writer.close();
           } catch (IOException e)
           {
               e.printStackTrace();
           }
       }

        public static double[] bubbleSort(double[] a,int[] b,int[] c) 
        {  
            for (int i = 0; i < 100; i++)
            {  
                for (int j = i + 1; j < a.length; j++)
                {  
                    if(a[i] < a[j])
                    {  
                        double temp;
                        int temp1; 
                        int temp2;
                        temp = a[j];  
                        a[j] = a[i];  
                        a[i] = temp;  
                        temp1 = b[j];  
                        b[j] = b[i];  
                        b[i] = temp1;  
                        temp2=c[j];
                        c[j]=c[i];
                        c[i]=temp2;
                    }  
                }  
            }  
            return a;  
        }  

    public static void main(String args[])
    {


        int count =0;
        double temp;
        double a[]=new double[23105];
        int b[]=new int[23105];
        int c[]=new int[23105];
        String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/simila.txt";
        String content;
        FileInputStream fis;
        InputStreamReader isr;
        BufferedReader br = null;
        try {
            fis = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/IdToItem.txt");
            isr = new InputStreamReader(fis, "UTF-8");
            br = new BufferedReader(isr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String[] strings = new String[1];

        String str;
        try {
            while ((str = br.readLine()) != null)
            {
                for(int i=0;i<23105;i++)
                {
                    a[i]=0;
                    b[i]=i+1;
                }
                count=0;
                String[] str1 = str.split(" ");
// for(int k=0;k<str1.length;k++)
// {
                    //str1[k]
                    //读Toterms1文件,每行比较,选取相似度最高的100个,记录行号即可

                    FileInputStream fis1;
                    InputStreamReader isr1;
                    BufferedReader br1 = null;
                    try {           
                        fis1 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/ToTerms1.txt");
                        isr1 = new InputStreamReader(fis1, "UTF-8");
                        br1 = new BufferedReader(isr1);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    String str2;
                    try {
                        while ((str2 = br1.readLine()) != null)
                        {
                                temp=0;
                                a[count]=0;
                            String[] str3 = str2.split(",");//将原先的一行所有分词,换成一个一个产品的分词
                            for(int i1=0;i1<str3.length;i1++)
                            {
                                String[] str4=str3[i1].split(" ");

                                temp=(double)count(str1,str4)/str1.length;
                                if(temp>(double)a[count]/str1.length)
                                {
                                a[count]=temp;

                                }
                                c[count]=i1;
                            }


                            count++;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                }
                    //sort
                    bubbleSort(a,b,c);
// content=b[0-100];
                    for(int j=0;j<100;j++)
                    {
                        if(a[0]<=0.8)
                        {
                            content=a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4];//a[j]similar,b[j]line_number
                            appendMethod(fileName, content);
                            break;
                        }
                        if(a[j]>0.8)
                        {
                            content=a[j]+" ";
                            appendMethod(fileName, content);
                        }


                    }
            appendMethod(fileName, "\n");

                }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

统计各推荐组合中的某个商品与预测商品的相似度(最高相似度)_第1张图片

统计各推荐组合中的某个商品与预测商品的相似度(最高相似度)_第2张图片

统计各推荐组合中的某个商品与预测商品的相似度(最高相似度)_第3张图片

你可能感兴趣的:(预测)