Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple CoreNLP - how to get all the nouns to an array?

I'm using Stanford Simple NLP. I need to get all the noun values to nounPhrases array. me() method gives me output as below:

The parse of the sentence 'I like java and python' is (ROOT (S (NP (PRP I)) (VP (VBP like) (NP (NN java) (CC and) (NN python)))))

This is my method

public String s = "I like java and python";

public static Set<String> nounPhrases = new HashSet<>();

public void me() {

    Document doc = new Document(" " + s);
    for (Sentence sent : doc.sentences()) {

        System.out.println("The parse of the sentence '" + sent + "' is " + sent.parse());

        if (sent.parse().equals("NN") || sent.parse().equals("NNS") || sent.parse().equals("NNP")
                || sent.parse().equals("NNPS")) {

            // I need to assign all nouns to the array nounPhrases

        }

    }
}

I'm not sure whether my if condition right or wrong since I'm new to Stanford NLP. Please help me to get my nouns to this array.

I got the sample code form below URL and I customized it little bit.

Simple CoreNLP

like image 792
user8048032 Avatar asked Jan 23 '26 14:01

user8048032


1 Answers

If anyone needs a complete and recent version of this solution, here it is:

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import edu.stanford.nlp.pipeline.CoreDocument;
import edu.stanford.nlp.pipeline.CoreSentence;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;


public class BasicPipelineExample4 {

  public static String text = "Joe Smith was born in California. "+
  "Study studying studied. " +
  "In 2017, he went to Paris, France in the summer. " +
  "His flight left at 3:00pm on July 10th, 2017. " +
  "After eating some escargot for the first time, Joe said, \"That was delicious!\" " +
  "He sent a postcard to his sister Jane Smith. " +
  "He is ok. " +
  "Simple, right? Remove removed removing was were is are element at given gave give index, insert it at desired index. Let's see if it works for the second test case."+
  "He is ok to go now. " +
  "After hearing about Joe's trip, Jane decided she might go to France one day.";

public static void main(String[] args) {
    Properties props = new Properties();
    // set the list of annotators to run
    props.setProperty("annotators", "tokenize,ssplit,pos,parse");
    // set a property for an annotator, in this case the coref annotator is being
    // set to use the neural algorithm
    props.setProperty("coref.algorithm", "neural");
    // build pipeline
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    // create a document object
    CoreDocument doc = new CoreDocument(text);
    // annnotate the document
    pipeline.annotate(doc);

    Set<String> nounPhrases = new HashSet<>();

    for (CoreSentence sent : doc.sentences()) {

        System.out.println("The parse of the sentence '" + sent + "' is " + sent.constituencyParse());
        // Iterate over every word in the sentence
        for (int i = 0; i < sent.tokens().size(); i++) {
            // Condition: if the word is a noun (posTag starts with "NN")
            if (sent.posTags() != null && sent.posTags().get(i) != null && sent.posTags().get(i).contains("NN")) {
                // Put the word into the Set
                nounPhrases.add(sent.tokens().get(i).originalText());
            }
        }
    }

    System.out.println("Nouns: " + nounPhrases);

}

}
like image 170
Digao Avatar answered Jan 26 '26 04:01

Digao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!