Use MaxentTagger from stanford nlp - Java Natural Language Processing

Java examples for Natural Language Processing:stanford nlp

Description

Use MaxentTagger from stanford nlp

Demo Code


import edu.stanford.nlp.tagger.maxent.MaxentTagger;
import java.util.Arrays;

public class StanfordPosTagger 
{
    public static void main(String[] args) 
    {/*from  w  w  w  . j  a  v a 2 s.  co  m*/
        String sentence = "this is a test. what you believe is all junk";
        String path = "models/english-left3words-distsim.tagger";
        MaxentTagger tagger = init(path);
        int a = tag(tagger, sentence);
        System.out.println(a);
    }
    
    public static MaxentTagger init(String path)
    {
        MaxentTagger tagger = new MaxentTagger(path);
        return tagger;
    }
    
    public static int tag(MaxentTagger tagger, String sentence)
    {
        // The tagged string
        String tagged_str = tagger.tagString(sentence);
        String[] individual_word = tagged_str.split(" ");
        String[][] fully_separate = new String[individual_word.length][2];
        // Output the result
        System.out.println(tagged_str);   
        
        for (int i = 0; i < individual_word.length; i++) 
        {
            fully_separate[i] = individual_word[i].split("_");
            System.out.println(Arrays.toString(fully_separate[i]));   

            if (fully_separate[i][1].equals("WP"))
                System.out.println("key word:" + fully_separate[i][0]);   
                
        }
            
        return 1;
    }
}

Related Tutorials