为什么我只得到一个TF-IDF的结果?

时间:2011-03-12 08:23:03

标签: java tf-idf

// 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值。

术语频率的示例输出:

输入字是'是'


| File = abc0.txt |
是---&gt;字数= | 2 |总计数= | 150 |期限频率= | 0.0133 |

输入的单词是'is'


| File = abc1.txt |
是---&gt;字数= | 0 |总计数= | 9 |期限频率= | 0.0000 |

TF-IDF

是 - &gt;包含术语7

的文件数

是 - &gt; IDF 0.1962946357308887

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

2 个答案:

答案 0 :(得分: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。但这些对于每个文件名/单词对都是唯一的。因此,您需要不仅保存一次,而是按文件名/单词保存,或者在最终循环中再次重新保存它们。

答案 1 :(得分:0)

打印TF IDF的部分需要在遍历所有文件的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);
}

} }