使用核心NLP将所有名词都添加到数组中

时间:2017-06-10 12:07:39

标签: java arrays loops set stanford-nlp

我是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());



    }

此代码为我提供了以下输出。

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

我需要所有

  

NN

到我的数组set nounPhrases。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

试试这个。

Pattern pat = Pattern.compile("\\(NN\\s+(\\w+)\\)");
Matcher m = pat.matcher(sent.parse().toString());
while (m.find())
    nounPhrase.add(m.group(1));
System.out.println(nounPhrase);

结果:

[python, java]