SelectTop200_2.java输出文件final_example1.txt,排除原先因为相似度很像,而cat不同而没有排除的商品,即找到该搭配组合中一个商品跟被预测商品很像,但是它们的cat不同
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 SelectTop200_2 {
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 String[] getStrings() {
FileInputStream fis;
InputStreamReader isr;
BufferedReader br = null;
try {
fis = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/ToCats.txt");
// fis = new FileInputStream("D://ToCats.txt");
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] strings = new String[5484];
int i=0;
String str;
try {
while ((str = br.readLine()) != null) {
strings[i] = str;
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strings;
}
public static String[] getStrings1() {
FileInputStream fis1;
InputStreamReader isr1;
BufferedReader br1 = null;
try {
fis1 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/ToCat.txt");
// fis1 = new FileInputStream("D://ToCat.txt");
isr1 = new InputStreamReader(fis1, "UTF-8");
br1 = new BufferedReader(isr1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] strings = new String[23001];
int i=0;
String str;
try {
while ((str = br1.readLine()) != null) {
strings[i] = str;
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strings;
}
public static String[] getStrings2() {
FileInputStream fis2;
InputStreamReader isr2;
BufferedReader br2 = null;
try {
fis2 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/dim_fashion_matchsets.txt");
//fis2 = new FileInputStream("D://dim_fashion_matchsets.txt");
isr2 = new InputStreamReader(fis2, "UTF-8");
br2 = new BufferedReader(isr2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] strings = new String[23001];
int i=0;
String str;
try {
while ((str = br2.readLine()) != null) {
String[] str1 = str.split(" ");
//String[] str2 = str1[1].split(";");
strings[i] = str1[1].replaceAll(";", ",");
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strings;
}
public static String[] getStrings3() {
FileInputStream fis4;
InputStreamReader isr4;
BufferedReader br4 = null;
try {
fis4 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/simila_place.txt");
isr4 = new InputStreamReader(fis4, "UTF-8");
br4 = new BufferedReader(isr4);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] strings = new String[5484];
int i=0;
String str;
try {
while ((str = br4.readLine()) != null) {
strings[i] = str;
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strings;
}
public static void main(String args[])
{
String[] strings = getStrings();//记录每行预测商品对应的类别cat
String[] strings1 = getStrings1();//记录对应match中每行对应的cat,用于甄别是否为同类产品
String[] strings2 = getStrings2();//每条对应match中的每行记录,需要去除同类产品
String[] strings3=getStrings3();//记录每行的每个最相似的位置
// for(int i=0;i<5484;i++)
// {
//strings[i]每行预测商品对应的类别cat,第一行对应399
String fileName = "/public/home/dsj/Public/sundujing/fpgrowth/final_example1.txt";
String content;
FileInputStream fis3;
InputStreamReader isr3;
BufferedReader br3 = null;
try {
fis3 = new FileInputStream("/public/home/dsj/Public/sundujing/fpgrowth/line0.6_10.txt");
//fis3 = new FileInputStream("D://line.txt");
isr3 = new InputStreamReader(fis3, "UTF-8");
br3 = new BufferedReader(isr3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String str;
int count=0;
try {
while ((str = br3.readLine()) != null)//对应的为line.txt的每行即是为每行预测商品所准备的推荐搭配的前100行数
{
//strings3[count] 1记录对应位置
String[] str1 = str.split(" ");
String[] str4=strings3[count].split(" ");//str1.length==str4.length
for(int k=0;k<str1.length;k++)
{
int t=Integer.parseInt(str1[k]);//第一行对应为第一个数5735....
//strings1[t]对应为ToCat第5735行,487 487 487 487 399 399 399 399 220 220 220 220 317 317 317 317,这条记录
String[] str2 = strings1[t-1].split(" ");
//strings2[t]对应的是dim_fashion_matchsets第5735行 :912534,932752,658698,519908,70341,3050430,300564,1071257,1590463,2240550,84524,3204531,3267304,3185197,1273818,1334655
String[] str3 = strings2[t-1].split(",");
for(int j=0;j<str2.length;j++)
{
if(strings[count].trim().equals(str2[j])==false&&str2[j].equals(str2[Integer.parseInt(str4[k])])==false)
{
// boolean a=strings[count].equals(str2[j])
// content="";
// }else{
content="";
//System.out.print(str3[j]+",");
content=str3[j]+",";
// boolean a=strings[count].trim().equals(str2[j]);
// content=a+"";
//content=str2[j];
appendMethod(fileName, content);
}
}
}
System.out.println("");
appendMethod(fileName, "\n");
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}