2017-07-31 2 views
1

Je suis nouveau dans le traitement du langage naturel (NLP), je veux faire du marquage de la parole (POS), puis trouver une structure spécifique au sein d'un texte. Je pourrais gérer le marquage des points de vente à l'aide de Stanford-PNL, mais je ne sais pas comment extraire cette structure:extraire une structure linguistique basée sur la POS tagged phrase en utilisant Stanford nlp en JAVA

NN/NNS + IN + DT + NN/NNS/NNP/NNPS

public static void main(String args[]) throws Exception{ 
    //input File 
    String contentFilePath = ""; 
    //outputFile 
    String triplesFilePath = contentFilePath.substring(0, contentFilePath.length()-4)+"_postagg.txt"; 

    //document to POS tagging 
    String content = getFileContent(contentFilePath); 

    Properties props = new Properties(); 

    props.setProperty("annotators","tokenize, ssplit, pos"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
    // Annotate the document. 
    Annotation doc = new Annotation(content); 
    pipeline.annotate(doc); 


    // Annotate the document. 
    List<CoreMap> sentences = doc.get(CoreAnnotations.SentencesAnnotation.class); 
    for (CoreMap sentence : sentences) { 
     for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) { 
      String word = token.get(CoreAnnotations.TextAnnotation.class); 
      // this is the POS tag of the token 
      String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class); 
      System.out.println(word + "/" + pos); 
     } }}} 
+0

Je viens de réaliser, que la balise POS pour un déterminant est "DT", pas "DET". J'ai corrigé ma réponse ci-dessous, ça marche maintenant. –

Répondre

1

Vous pouvez simplement itérer sur votre phrase et vérifier les balises POS . S'ils correspondent à vos besoins, vous pouvez extraire cette structure. Le code pour cela pourrait ressembler à ceci:

for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) { 
    List<CoreLabel> tokens = sentence.get(TokensAnnotation.class); 
    for(int i = 0; i < tokens.size() - 3; i++) { 
     String pos = tokens.get(i).get(PartOfSpeechAnnotation.class); 
     if(pos.equals("NN") || pos.equals("NNS")) { 
      pos = tokens.get(i + 1).getString(PartOfSpeechAnnotation.class); 
      if(pos.equals("IN")) { 
       pos = tokens.get(i + 2).getString(PartOfSpeechAnnotation.class); 
       if(pos.equals("DT")) { 
        pos = tokens.get(i + 3).getString(PartOfSpeechAnnotation.class); 
        if(pos.contains("NN")) { 
         //We have a match starting at index i and ending at index i + 3 
         String word1 = tokens.get(i).getString(TextAnnotation.class); 
         String word2 = tokens.get(i + 1).getString(TextAnnotation.class); 
         String word3 = tokens.get(i + 2).getString(TextAnnotation.class); 
         String word4 = tokens.get(i + 3).getString(TextAnnotation.class); 
         System.out.println(word1 + " " + word2 + " " + word3 + " " + word4); 
        } 
       } 
      } 
     } 
    } 
} 
+0

Pourquoi cela ne fonctionne pas avec DET? – Raha1986

+0

Les tags POS de Stanford proviennent des [Tags POS Penn Treebank] (https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html). Ils spécifient l'étiquette pour un déterminant comme "DT". Il n'y a pas de balise "DET". –