2016-07-24 63 views
2

我想結合這兩種方法在我的文檔分析器frequencyCounter和parseFiles中只是出現了一些錯誤代碼。在Java代碼中將Java中的兩種方法結合在一起

我希望所有的frequencyCounter應該是一個函數,應該從parseFiles內執行,相關信息不用擔心文件的內容應該傳遞給doSomething,以便它知道要打印什麼。

現在我只是保持瞭如何將這兩種方法放在一起搞亂了,請給一些建議

這是我的主類:

public class Yolo { 
    public static void frodo() throws Exception { 
     int n; // number of keywords 
     Scanner sc = new Scanner(System.in); 
     System.out.println("number of keywords : "); 
     n = sc.nextInt(); 
     for (int j = 0; j <= n; j++) { 

      Scanner scan = new Scanner(System.in); 
      System.out.println("give the testword : "); 
      String testWord = scan.next(); 
      System.out.println(testWord); 

      File document = new File("path//to//doc1.txt"); 
      boolean check = true; 

      try { 
       FileInputStream fstream = new FileInputStream(document); 
       BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
       String strLine; 
       strLine = br.readLine(); 

       // Read File Line By Line 

       int count = 0; 
       while ((strLine = br.readLine()) != null) { 

        // check to see whether testWord occurs at least once in the 
        // line of text 
        check = strLine.toLowerCase().contains(testWord.toLowerCase()); 

        if (check) { 
         // get the line 
         String[] lineWords = strLine.split("\\s+"); 
         // System.out.println(strLine); 
         count++; 
        } 

       } 
       System.out.println(testWord + "frequency: " + count); 

       br.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

你的CosineSimilarity()和TfIdf()在哪裏? – Nuwanda

回答

3

下面的代碼給你這個輸出:

Professor frequency: 54 
engineering frequency: 188 
data frequency: 2 
mining frequency: 2 
research frequency: 9 

儘管這隻適用於doc1,但您必須添加一個循環來遍歷所有5個文檔。

public class yolo { 
    public static void frodo() throws Exception { 

     String[] keywords = { "Professor" , "engineering" , "data" , "mining" , "research"}; 
     for(int i=0; i< keywords.length; i++){ 

     String testWord = keywords[i]; 
     File document = new File("path//to//doc1.txt"); 
     boolean check = true; 

     try { 
      FileInputStream fstream = new FileInputStream(document); 
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
      String strLine; 
      strLine = br.readLine(); 

      // Read File Line By Line 

      int count = 0; 
      while ((strLine = br.readLine()) != null) { 

       // check to see whether testWord occurs at least once in the 
       // line of text 
       check = strLine.toLowerCase().contains(testWord.toLowerCase()); 

       if (check) { 
        // get the line 
        String[] lineWords = strLine.split("\\s+"); 
        count++; 
       } 

      } 
      System.out.println(testWord + "frequency: " + count); 

      br.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

}

希望這有助於!

+0

@alaye謝謝先生,我還將這個類添加到我的主要java類或文檔解析器java嗎? – JJKx

+0

爲你的主,添加「yolo.frodo();」 你不需要你的頻率計數器和解析器 – Nuwanda

+0

只需刪除這兩種方法! – Nuwanda

相關問題