统计各推荐组合中第几个是该组合中与预测商品相似度最高的商品

该文章介绍如何使用CommonCount3.java程序来计算每个推荐组合中,哪个商品的相似度最高,并记录其在组合中的位置。输出结果文件(simila_place.txt)与simila.txt对应,用于后续剔除高相似度商品。
摘要由CSDN通过智能技术生成

CommonCount3.java统计各推荐组合中第几个是该组合中与预测商品相似度最高的商品,输出(simila_place.txt)与simila.txt相对应(之后要将该商品与其可替代商品剔除)1表示b[1],即该行第二个产品

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 CommonCount3 {

    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 < a.length - 1; 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_place.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;
                                c[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=c[0]+" "+c[1]+" "+c[2]+" "+c[3]+" "+c[4];//a[j]similar,b[j]line_number
                            appendMethod(fileName, content);
                            break;
                        }
                        if(a[j]>0.8)
                        {
                            content=c[j]+" ";
                            appendMethod(fileName, content);
                        }


                    }
            appendMethod(fileName, "\n");

                }

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

这里写图片描述

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值