Search code examples
javastanford-nlpnamed-entity-recognitionpos-tagger

Can NER Stanford called in java file?


I've tried to give NER and POS tag from texts with Stanford Named Entity Recognizer (NER) and Stanford Log-linear Part-Of-Speech Tagger.

Let me give you an example of POStag

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class POSTag {
    static MaxentTagger tagger =  new MaxentTagger("english_language_pos.tagger");

    public static void main(String[] args) throws IOException{
        String text = "Sarah and I go to Samsung office in Korea."
        String tagged = tagger.tagString(text);

        System.out.println(tagged);
    }
}

The output:

Sarah_NNP and_CC I_PRP go_VBP to_TO Samsung_NNP office_NN in_IN Korea_NNP ._.  

And for NER Stanford, I have to run in command line:

java -mx500m -cp stanford-ner-3.5.2.jar edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier indonesian_language_ner.gz -textFile E:/file.txt > E:/file_ner.txt

My question is how can I call Stanford NER in file with java extension(file.java)? Is there any way to call Stanford NER besides from command line? Or any other tool or library that I can use to give NER that called from java file?

*****edit:
I need the output of NER become like this:

Sarah_PERSON and_O I_O go_O to_O Samsung_ORGANIZATION office_O in_O Korea_PLACE ._O 

*****edit

**fyi: I use Windows 8.1


Solution

  • You could do something like this:

        String model = "english.all.3class.distsim.crf.ser.gz";
        String text = "file.txt";
    
        AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier.getClassifier(model);
        classifier.classifyAndWriteAnswers(text);
    

    I hope it helps.