嗨,这是我计算词频的代码。以表格形式显示
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;
for (a = 0; a < filename; a++) {
try {
File file = new File(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + a
+ ".txt");
System.out.println("File = abc" + a + ".txt");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
int totalCount = 0;
int wordCount = 0;
int numDoc2 = 0;
Scanner s = new Scanner(file);
{
while (s.hasNext()) {
totalCount++;
if (s.next().equals(array[i]))
wordCount++;
}
System.out.println("Word count: " + wordCount);
System.out.println("Total count: " + totalCount);
System.out.printf("Term Frequency: %8.4f",
(double) wordCount/totalCount);
System.out.println("\n");
}
}
} catch (FileNotFoundException e) {
System.out.println("File is not found");
}
}
到目前为止的代码显示
请输入所需的字: 约
文件= abc0.txt
约
字数:0
总数:1706
词频:0.0000
文件= abc1.txt
约
字数:0
总计数:9819
词频:0.0000
我如何创建这样的数据表格:
OUTPUT:
文件名字总术语
abc0.txt 0.1 0.2 0.3
abc1.txt 0.4 0.5 0.6