// 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 값을 제공해야합니다. 용어 주파수왜 TF-IDF 결과가 하나 뿐인가요?
출력 예 : |
단어 입력된다
'인' 파일 = abc0.txt |
은 ---> 단어 개수 = | 2 | 총 수 = | 150 | 용어 빈도 = | 0.0133 | |
는
'입니다' 파일 = abc1.txt |
은 ---> 단어 수 = | 0 | 총 수 = | 9 | 용어 빈도 = | 0.0000 |
TFIDF
입니다 -> 7
이 용어가 포함 된 파일의이 수 -> IDF 0.1962946357308887
은 -> TFIDF 0.0028607962606519654 < < < 나는 가정 파일 당 하나의 값을 얻으려면 10 개의 파일이 있다는 것을 의미합니다. 각 파일마다 10 개의 다른 값을 부여한다고 가정합니다. 그러나 결과 하나만 인쇄합니다. 누군가 내 실수를 지적 할 수 있습니까?
당신이 이름에 더 많은 관심을 지불해야한다. 'fileName'과 'filename'이라는 변수 중 하나는 'int'이고, 매우 혼란 스럽습니다. –