Search code examples
javanlptokenizeopennlppos-tagger

Save different POS (parts of speech) in different file using POSTagger in Java?


I am using openNLP for tagging POS (Parts of Speech).

InputStream inputStream = new 
             FileInputStream("C:/en-pos-maxent.bin"); 
          POSModel model = new POSModel(inputStream);

          POSTaggerME tagger = new POSTaggerME(model);



    String sentence = "This is not a song for the broken-hearted" + 
            " No silent prayer for the faith-departed " + 
            " I am not gonna be just a face in the crowd " + 
            " You are gonna hear my voice " +
            " When I shout it out loud";

    String simple = "[.?!-]";      
      String[] splitString = (sentence.split(simple));     


      SimpleTokenizer simpleTokenizer = SimpleTokenizer.INSTANCE;

      //String tokens[] = simpleTokenizer.tokenize();



      for(int i = 0;i<splitString.length;i++)
      {
          String tokens[] = simpleTokenizer.tokenize(splitString[i]);


          String[] tags = tagger.tag(tokens);

          //POSSample sample = new POSSample(tokens, tags);

          /*for(String token : tokens) {         
              System.out.println(token);  
           }*/



         for(int j= 0;j < tags.length;j++)
          {
              if(tags[j].equals("DT"))
              {
                  //System.out.println(tokens[j]);

                  File file = new File("DT.txt");

                  try {

                      PrintWriter output = new PrintWriter(file);
                      output.println(tokens[j]);
                      output.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }

When I use println in loop.It show me the required value.But when I am going to save this in a file name DT.txt . It just save one value in text file. Text file output Printed Output in console


Solution

  • You are creating a new file for each run of the for loop. Create the file outside the for loop.