我必须打印 110 个能够打印的文档的相似度矩阵,输出是具有最高相似度的文档对。
现在我必须根据这些文档对形成集群。
问题是我无法存储这些文档对,因为(4,29),(29,76),(45,89)
输出必须是,(4,29,76),(45,89)
但这里我没有得到,因为只要调用 maxarray 方法并且输出与输入相同,数组列表就会被覆盖。
public class SimilarityMatrix {
public static void main(String[] args) throws IOException {
int array[][]=new int[120][120];
outputArray(array);
}
public static void outputArray(int[][] array) throws IOException {
FileOutputStream out=new FileOutputStream("similaritymatrix.txt");
PrintStream p=new PrintStream(out);
int rowSize = array.length;
int columnSize = array[0].length;
for(int i = 1; i <= 110; i++) {
System.out.print("[");
p.print("[");
for(int j = 1; j <= 109; j++) {
if(i!=j && i<j)
{
app obj=new app();
array[i][j]= obj.main(i,j);
}
else if(i>j || i==j)
{
array[i][j]=0;
}
System.out.print(" " + array[i][j]);
p.print(" " + array[i][j]);
}
System.out.println(" ]");
p.println(" ]");
}
System.out.println();
p.println();
max(array);
}
public static void max(int array[][]) throws FileNotFoundException
{
FileOutputStream out=new FileOutputStream("similaritymatrix1.txt");
PrintStream p=new PrintStream(out);
int maxValue = 1;
int i=1;
int j=1;
for ( i = 1; i <= 110; i++) {
for ( j = 1; j <= 109; j++)
{
if (array[i][j] > maxValue) {
maxValue = array[i][j];
}
}}
for ( i = 1; i <= 110; i++) {
for ( j = 1; j <= 109; j++)
{
if(maxValue==array[i][j]){
System.out.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);
p.println("\nMax values in 2D array at d"+i+"d"+j+"max value ="+maxValue);
array[i][j]=0;
ArrayList a = new ArrayList();
a.add(i);
a.add(j);
if(!a.contains(i)&& !a.contains(j))
{
a.add(i);
a.add(j);
System.out.println("the cluster is" +a);
}
else if(a.contains(i)&& !a.contains(j))
{
a.add(j);
System.out.println("the cluster is" +a);
}
else if(!a.contains(i)&& a.contains(j))
{
a.add(i);
System.out.println("the cluster is" +a);
}
}}}
for( i=1;i<=110;i++)
{
p.print("[");
System.out.print("[");
for( j=1;j<=109;j++)
{
System.out.print(" "+ array[i][j]);
p.print(" "+ array[i][j]);
}
p.println(" ]");
System.out.println("]");
}
if(maxValue>1)
max(array);
}
}
class app {
private static ArrayList<String> load(String f1) throws FileNotFoundException{
Scanner reader = new Scanner(new File(f1));
ArrayList<String> out = new ArrayList<String>();
while (reader.hasNext()){
String temp = reader.nextLine();
String[] sts = temp.split(" ");
for (int i = 0;i<sts.length;i++){
if(sts[i] != "" && sts[i] != " " && sts[i] != "\n")
out.add(sts[i]);
}
}
return out;
}
private static void write(ArrayList<String> out, String fname) throws IOException{
FileWriter writer = new FileWriter(new File(fname));
for (int i = 0;i<out.size();i++){
writer.write(out.get(i) + "\t");
}
writer.close();
}
public static int main(int a,int b) throws IOException {
int count=0;
ArrayList<String> file1;
ArrayList<String> file2;
ArrayList<String> out = new ArrayList<String>();
file1 = load(a+".txt");
file2 = load(b+".txt");
for(int i = 0;i<file1.size();i++){
String word1 = file1.get(i);
for (int z = 0; z <file2.size(); z++){
if (word1.equalsIgnoreCase(file2.get(z))){
boolean already = false;
for (int q = 0;q<out.size();q++){
if (out.get(q).equalsIgnoreCase(file1.get(i))){
already = true;
}
}
if (already==false){
out.add(file1.get(i));
count++;
}}}}
return count;
}
}
这是我到目前为止尝试过的代码,我得到了输出,但它与输入相同