简体   繁体   中英

How to extract NP (Noun phrases) and VP (Verb Phrases) by using library of python called pycorenlp

I am using the pycornlp library. This library provides a python wrapper for stanford corenlp.

I am able to get a parsetree which is given below.

(ROOT
(S
 (NP (PRP He))
 (VP (VBP drink)
  (NP
    (NP (NN tomato) (NN soup))
    (PP (IN in)
      (NP (DT the) (NN morning)))))))

Now I want to extract the first NP and VP.

(NP (PRP He))
(VP (VBP drink)

Is there any API available in pycorenlp?

Is there any other API available which I can use to extract all NP or all VP?

The output of CoreNLP is typically a JSON (which is a combination of lists and dictionaries) that you can easily sift through pulling out what you need.

The below link has a good example that shows you how to connect to the server but then shows a small line of code illustrating how to move through the output file and take what you need. The link is: http://stanza.readthedocs.io/en/latest/example.text_classification.html#annotating-using-corenlp

The example code that sifts through your output file is:

for token in annotation['sentences'][0]['tokens']: print token['word'], token['pos']

'annotation' is the output variable. This code will print a list of the words and their part of speech. You could easily modify this code to stop once it found the first instance of NP and VP.

The simple answer to your question is that you can right a for loop to sort through your output data to find the first instance of NP and VP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM