有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java排序从程序计算的分数

int queryVector = 1;
    double similarity = 0.0;
    int wordPower;
    String[][] arrays = new String[filename][2];
    int row;
    int col;


    for (a = 0; a < filename; a++) {
        int totalwordPower = 0;
        int totalWords = 0;
        try {
            System.out
                    .println(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  ");
            System.out.println("\n");
            System.out.println("The word inputted : " + word2);
            File file = new File(
                    "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + a
                            + ".txt");
            System.out.println(" _________________");

            System.out.print("| File = abc" + a + ".txt | \t\t \n");

            for (int i = 0; i < array2.length; i++) {

                totalCount = 0;
                wordCount = 0;

                Scanner s = new Scanner(file);
                {
                    while (s.hasNext()) {
                        totalCount++;
                        if (s.next().equals(array2[i]))
                            wordCount++;

                    }

                    System.out.print(array2[i] + " --> Word count =  "
                            + "\t " + "|" + wordCount + "|");
                    System.out.print("  Total count = " + "\t " + "|"
                            + totalCount + "|");
                    System.out.printf("  Term Frequency =  | %8.4f |",
                            (double) wordCount / totalCount);

                    System.out.println("\t ");

                    double inverseTF = Math.log10((float) numDoc
                            / (numofDoc[i]));
                    System.out.println("    --> IDF = " + inverseTF);

                    double TFIDF = (((double) wordCount / totalCount) * inverseTF);
                    System.out.println("    --> TF/IDF = " + TFIDF + "\n");

                    totalWords += wordCount;

                    wordPower = (int) Math.pow(wordCount, 2);

                    totalwordPower += wordPower;

                    System.out.println("Document Vector : " + wordPower);

                    similarity = (totalWords * queryVector)
                            / ((Math.sqrt((totalwordPower)) * (Math
                                    .sqrt(((queryVector * 3))))));



                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File is not found");
        }
        System.out.println("The total query frequency for this file is "
                + totalWords);
        System.out.println("The total document vector : " + totalwordPower);

        System.out.println("The similarity is " + similarity);
    }
}

}

嗨,我想对根据上述代码计算的相似性分数进行排序。这是两个文本文件的输出示例。我总共有10个文本文件

输入的单词:你好吗


| File=abc0。txt |
如何-->;字数=| 0 |总数=| 1289 |词频=| 0.0000 |
--&燃气轮机;IDF=1.041392685158251 --&燃气轮机;TF/IDF=0.0

文档向量:0 是-->;字数=| 0 |总数=| 1289 |词频=| 0.0000 |
--&燃气轮机;IDF=0.43932693383026263 --&燃气轮机;TF/IDF=0.0

文档向量:0 你-->;字数=| 0 |总数=| 1289 |词频=| 0.0000 |
--&燃气轮机;IDF=0.1962946357308887 --&燃气轮机;TF/IDF=0.0

文档向量:0 此文件的总查询频率为0 总文档向量:0 相似性是南


输入的单词:你好吗


| File=abc1。txt |
如何-->;字数=| 0 |总字数=| 426 |词频=| 0.0000 |
--&燃气轮机;IDF=1.041392685158251 --&燃气轮机;TF/IDF=0.0

文档向量:0 是-->;字数=| 0 |总字数=| 426 |词频=| 0.0000 |
--&燃气轮机;IDF=0.43932693383026263 --&燃气轮机;TF/IDF=0.0

文档向量:0 你-->;字数=| 3 |总字数=| 426 |词频=| 0.0070 |
--&燃气轮机;IDF=0.1962946357308887 --&燃气轮机;TF/IDF=0.0013823565896541458

文件载体:9 此文件的总查询频率为3 总文档向量:9 相似性为0.5773502691896257

注意:这是两个文本文件的运行示例。我总共有10个文本文件

如何将相似性得分从最高到最低排序?有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    将相似性分数添加到列表中,并使用库方法进行排序。它按升序排序,你可以从头到尾读

    ArrayList<Double> arrayList = new ArrayList<Double>();
    Collections.sort(arrayList);
    

    或者,您可以声明一个比较器,并像下面那样使用它

    ArrayList<Double> arrayList = new ArrayList<Double>();
    Comparator<Double> comparator = Collections.reverseOrder();
    Collections.sort(arrayList,comparator);