2017-06-10 1 views
1

私はスタンフォードシンプルNLPを使用しています。 nounPhrases配列にすべての名詞の値を取得する必要があります。私()メソッドは以下のように私に出力を提供します:シンプルなCoreNLP - どのようにすべての名詞を配列に取得するのですか?

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))))) 

これは私がスタンフォードNLPに新たなんだので、私の条件正しいか間違っているかのかどうかわからないんだけど、私の方法

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 

     } 

    } 
} 

です。私の名詞をこの配列に手伝ってください。

URLの下にサンプルコードフォームがあり、少しカスタマイズしました。

Simple CoreNLP

答えて

0

あなたの条件は、ほぼ右です。あなたは、 "NN"を含むPOSタグを持つすべての単語、つまりすべての名詞を必要とします。すべての単語のPOSタグをチェックするには、文中のすべての単語を繰り返し処理する必要があります。あなたのコードに基づいて、次のようになります:

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()); 
     //Iterate over every word in the sentence 
     for(int i = 0; i < sent.words().size(); i++) { 
      //Condition: if the word is a noun (posTag starts with "NN") 
      if (sent.posTag(i) != null && sent.posTag(i).contains("NN")) { 
       //Put the word into the Set 
       nounPhrases.add(sent.word(i)); 
      } 
     } 
    } 
} 
関連する問題