Stanford NER is a Java implementation of a Named Entity Recognizer. Named Entity Recognition (NER) labels sequences of words in a text which are the names of things, such as person and company names, or gene and protein names.
We can use Stanford NER in two different ways. In this article, we will discuss both the techniques of how to use 3class(PERSON,LOCATION,ORGANIZATION) ner model with examples.


The first technique is to load individual classifier, in our case 3 class model and load it. perform the following steps:

Step 1: Download  Standford ner Standford ner zip file.

Step 2: Extract Stanford bundle, add stanford-ner jar file into your project classpath.

Step 3: write below code snippets

//path of classifier we want to load
        String classierPath = "D:\\classifiers\\english.all.3class.distsim.crf.ser.gz";
        //content that we want to classify
        String fileContents = "barak Obama was born in 1961 in Honolulu, Hawaii, \n" +
                "in 1988 Obama enrolled in Harvard Law School.";
        //Load classifier , classifier should be load only one time
        AbstractSequenceClassifier classifier = CRFClassifier.getClassifierNoExceptions(classierPath);
        //classify the text
        List<List<CoreLabel>> out = classifier.classify(fileContents);
        //iterate the result and print it.
        for (List<CoreLabel> sentence : out) {
            for (CoreLabel word : sentence) {
                //unclassify label class is O , we will not print it here
                System.out.println(word.word() + " = " + word.get(CoreAnnotations.AnswerAnnotation.class) );


Loading classifier from D:\classifiers\english.all.3class.distsim.crf.ser.gz ... done [9.6 sec].
barak = PERSON
Obama = PERSON
Honolulu = LOCATION
Obama = PERSON


In this example, we will use the stanford core NLP library which contains all the features and model of NLP.

Add below maven dependency in your project


Code snippet

// creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER
        Properties props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma, ner");
        //props.put("annotators", "tokenize, ssplit, pos, lemma, parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        String text = "barak Obama was born in 1961 in Honolulu, Hawaii, \n" +
                "In 1988 Obama enrolled in Harvard Law School.";
        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);
        // run all Annotators on this text
        // these are all the sentences in this document
        // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
        List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
        for(CoreMap sentence: sentences) {
            // traversing the words in the current sentence
            // a CoreLabel is a CoreMap with additional token-specific methods
            for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                // this is the text of the token
                String word = token.get(CoreAnnotations.TextAnnotation.class);
                // this is the POS tag of the token
                String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
                // this is the NER label of the token
                String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
                System.out.println("word: " + word + " pos: " + pos + " ne:" + ne);

An output of Stanford NER live demo

Standford NER 3class model livener output

Refer CRF-NER , NER Live Demo , NER annotators for more details.

Was this post helpful?

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *