2016-11-06 25 views
-1

我有以下格式的文檔術語列表(在txt文件中)。文檔總數爲1400.例如:如何在Java中實現TF?

doc 1:你好,我,你好,費用,每天(這裏doc 1是一個文本文件) doc 2:費用,你好(這裏doc 2是一個文本文件) .........

現在我該如何在java中實現TF? TF是多少計時器術語出現在文檔中的術語/總數

到目前爲止的代碼,它計算的一個術語多少次出現在文檔中

public class termdoc { 
 
    public static void main(String[]args) throws FileNotFoundException, UnsupportedEncodingException{ 
 
     File file2 = new File("D:\\logs\\termdoc.txt"); 
 
     PrintWriter tdr = new PrintWriter(file2, "UTF-8"); 
 
     Map<String, Integer> m = new HashMap<>(); 
 
     Map<Integer,Map>m1=new HashMap<>(); 
 
     String wrd; 
 
     int unqwrd=0; 
 
     
 
     for(int i=1;i<=1400;i++){ 
 
      Scanner tdsc=new Scanner(new File("D:\\logs\\AfterStem"+i+".txt")); 
 
      while(tdsc.hasNext()){ 
 
       Integer docid=i; 
 
       wrd=tdsc.next(); 
 
       Integer freq=m.get(wrd); 
 
       m.put(wrd, (freq == null) ? 1 : freq + 1); 
 
       m1.put(docid,m); 
 
     
 
      } 
 
     tdr.println(m1); 
 
     m.clear(); 
 
     m1.clear(); 
 
     tdsc.close(); 
 
     
 
    } 
 
     //System.out.println(m.size() + " distinct words"); 
 
     tdr.close(); 
 
     
 
     
 
} 
 
    
 
}

+0

選擇更好的變量名稱,縮進代碼,遵守Java命名約定,並告訴TF的含義,你期望你的代碼做什麼,以及它做了什麼。 –

+0

請解釋TF是什麼。 – mtyurt

+0

我認爲根據上下文TF意味着文本過濾?正如問題中提到的那樣,文檔中出現了多少次文本...! –

回答

0

你有每個詞在文檔中出現的次數。

總和所有這些值,並且您擁有文檔中的術語總數。

然後將給定詞在文檔中出現的次數除以計算出的總和,然後得到頻率。