0
// 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

是 --> TFIDF 0.0028607962606519654 <<< 我想每个文件得到一个值,这意味着我有 10 个文件,它假设为每个不同的文件给我 10 个不同的值。但是,它只打印一个结果。有人可以指出我的错误吗?

4

2 回答 2

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。但这些对于每个文件名/单词对都是唯一的。因此,您不仅需要保存一次,还需要保存每个文件名/单词,或者在最终循环中重新计算它们。

于 2011-03-12T08:40:42.960 回答
0

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

IE:

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

} }

于 2011-03-12T08:46:59.473 回答