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();
}
}
}