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 standford 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.

Example:1

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

Step 1 : Download  Standford ner Standford ner zip file.

Step 2:Extract Stanford bundle , add standfor-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
                if(word.getString(CoreAnnotations.AnswerAnnotation.class).equals("O"))
                    continue;
                System.out.println(word.word() + " = " + word.get(CoreAnnotations.AnswerAnnotation.class) );
            }

        }
    }

Output:

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

Example:2

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

Add below maven dependency in your projet

<dependency>
 <groupId>edu.stanford.nlp</groupId>
 <artifactId>stanford-corenlp</artifactId>
 <version>3.8.0</version>
</dependency>
<dependency>
 <groupId>edu.stanford.nlp</groupId>
 <artifactId>stanford-corenlp</artifactId>
 <version>3.8.0</version>
 <classifier>models</classifier>
</dependency>
<dependency>
 <groupId>edu.stanford.nlp</groupId>
 <artifactId>stanford-parser</artifactId>
 <version>3.8.0</version>
</dependency>
<dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>14.0.1</version>
</dependency>
<dependency>
 <groupId>com.sun.xml.bind</groupId>
 <artifactId>jaxb-impl</artifactId>
 <version>2.1.2</version>
</dependency>

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
        pipeline.annotate(document);

        // 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);
            }
        }

Output of Standford NER live demo

Standford NER 3class model livener output

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

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags:

Leave a Reply

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