2017-06-10 2 views
0

Je suis un débutant aux outils PNL. J'ai besoin d'obtenir tous les noms du code ci-dessous à un tableau. Comment puis-je faire ceci ?Obtenir tous les noms à un ensemble de tableaux en utilisant le noyau 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()); 



    } 

Ce code me donne la sortie ci-dessous.

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

J'ai besoin tous

NN

à mon tableau mis nounPhrases. Comment puis-je faire ceci ?

+0

J'utilise la PNL bibliothèque de base simple Stanford [lien] (https://stanfordnlp.github.io/CoreNLP/simple.html) – user8048032

Répondre

0

Essayez ceci.

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

Résultat:

[python, java] 
+0

Merci Saka. Ce code fonctionne. – user8048032