【问题标题】:Why do I only get one result of TF-IDF?为什么我只得到一个 TF-IDF 的结果?
【发布时间】:2011-07-13 23:32:15
【问题描述】:
// Calculating term frequency
    System.out.println("Please enter the required word  :");
    Scanner scan = new Scanner(System.in);
    String word = scan.nextLine();

    String[] array = word.split(" ");
    int filename = 11;
    String[] fileName = new String[filename];
    int a = 0;
    int totalCount = 0;
    int wordCount = 0;


    for (a = 0; a < filename; a++) {

        try {
            System.out.println("The word inputted is " + word);
            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 < array.length; i++) {

                totalCount = 0;
                wordCount = 0;

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

                    }

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

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

                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File is not found");

        }

    }

System.out.println("Please enter the required word  :");
    Scanner scan2 = new Scanner(System.in);
    String word2 = scan2.nextLine();
    String[] array2 = word2.split(" ");
    int numofDoc;

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

        numofDoc = 0;

        for (int i = 0; i < filename; i++) {

            try {

                BufferedReader in = new BufferedReader(new FileReader(
                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                + i + ".txt"));

                int matchedWord = 0;

                Scanner s2 = new Scanner(in);

                {

                    while (s2.hasNext()) {
                        if (s2.next().equals(array2[b]))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc++;

            } catch (IOException e) {
                System.out.println("File not found.");
            }

        }
        System.out.println(array2[b]
                + " --> This number of files that contain the term  "
                + numofDoc);
        double inverseTF = Math.log10((float) numDoc / numofDoc);
        System.out.println(array2[b] + " --> IDF " +  inverseTF );
        double TFIDF = (((double) wordCount / totalCount) * inverseTF );
        System.out.println(array2[b] + " --> TFIDF " + TFIDF);
    }
}

您好,这是我计算词频和 TF-IDF 的代码。第一个代码计算给定字符串的每个文件的词频。第二个代码应该使用上面的值计算每个文件的 TF-IDF。但我只收到一个值。它应该为每个文档提供 TF-IDF 值。

词频的示例输出:

输入的单词是'is'


|文件 = abc0.txt |
是 ---> 字数 = |2|总数 = |150|词频 = | 0.0133 |

输入的单词是'is'


|文件 = abc1.txt |
是 ---> 字数 = |0|总数 = |9|词频 = | 0.0000 |

TF-IDF

is --> 包含术语 7 的文件数

是 --> IDF 0.1962946357308887

is --> TFIDF 0.0028607962606519654

【问题讨论】:

  • 除了实际答案(霍华德给出的)之外,您应该更加注意命名。拥有名为“fileName”和“filename”的变量,其中之一是int,非常令人困惑。

标签: java tf-idf


【解决方案1】:

您假设每个文件重复的 println 语句是

double TFIDF = (((double) wordCount / totalCount) * inverseTF );
System.out.println(array2[b] + " --> TFIDF " + TFIDF);

但它包含在单个循环中

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

仅限。如果你想在每个文件中打印这一行,你必须用另一个循环包围所有文件。

由于这是作业,我不会包含最终代码,但给您另一个提示:您还在 TFIDF 的计算中包含了变量 wordCount 和 totalCount。但这些对于每个文件名/单词对都是唯一的。因此,您不仅需要保存一次,还需要按文件名/单词保存,或者在最终循环中重新计算它们。

【讨论】:

    【解决方案2】:

    打印 TDIDF 的部分需要移到循环所有文件的 for 循环中。

    即:

        System.out.println(array2[b]
                + " --> This number of files that contain the term  "
                + numofDoc);
        double inverseTF = Math.log10((float) numDoc / numofDoc);
        System.out.println(array2[b] + " --> IDF " +  inverseTF );
        double TFIDF = (((double) wordCount / totalCount) * inverseTF );
        System.out.println(array2[b] + " --> TFIDF " + TFIDF);
    }
    

    } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2017-09-17
      • 1970-01-01
      相关资源
      最近更新 更多