2011-03-12 5 views
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 값을 제공해야합니다. 용어 주파수왜 TF-IDF 결과가 하나 뿐인가요?

출력 예 : |

단어 입력된다


'인' 파일 = abc0.txt |
은 ---> 단어 개수 = | 2 | 총 수 = | 150 | 용어 빈도 = | 0.0133 | |

입력 된 단어는


'입니다' 파일 = abc1.txt |
은 ---> 단어 수 = | 0 | 총 수 = | 9 | 용어 빈도 = | 0.0000 |

TFIDF

입니다 -> 7

이 용어가 포함 된 파일의이 수 -> IDF 0.1962946357308887

은 -> TFIDF 0.0028607962606519654 < < < 나는 가정 파일 당 하나의 값을 얻으려면 10 개의 파일이 있다는 것을 의미합니다. 각 파일마다 10 개의 다른 값을 부여한다고 가정합니다. 그러나 결과 하나만 인쇄합니다. 누군가 내 실수를 지적 할 수 있습니까?

+4

당신이 이름에 더 많은 관심을 지불해야한다. 'fileName'과 'filename'이라는 변수 중 하나는 'int'이고, 매우 혼란 스럽습니다. –

답변

0

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

} }을 제외하고 (하워드에 의해 주어진) 실제 대답에서

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 변수도 포함 시켰습니다. 그러나 이들은 각 파일 이름/단어 쌍에 고유합니다. 따라서 파일을 한 번만 저장하지 말고 파일 이름/단어별로 저장하거나 최종 루프에서 다시 저장해야합니다.

관련 문제