2016-07-24 2 views
2

私は2つのメソッドを組み合わせたいドキュメントパーサ、frequencyCounter、parseFiles thsiコードにちょっとしたエラーがあります。このJavaコードでJavaの2つのメソッドを組み合わせて

私はすべてのfrequencyCounterがparseFiles内から実行されるべき関数でなければならず、関連情報はファイルの内容がdoSomethingに渡されて何を印刷するかを心配する必要はありません。

今私はちょうど一緒にこれらの2つの方法を置く方法でめちゃくちゃに保つよ、いくつかのアドバイスに

を与えてください、これは私のメインクラスです:

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

これら2つの方法を削除するだけです! – Nuwanda

関連する問題