Extract Noun Phrases with Stanza and CoreNLPClient

后端 未结 1 858
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 04:39

I am trying to extract noun phrases from sentences using Stanza(with Stanford CoreNLP). This can only be done with the CoreNLPClient module in Stanza.

# Imp         


        
相关标签:
1条回答
  • 2021-01-15 05:05
    from stanza.server import CoreNLPClient
    
    # get noun phrases with tregex
    def noun_phrases(_client, _text, _annotators=None):
        pattern = 'NP'
        matches = _client.tregex(_text,pattern,annotators=_annotators)
        print("\n".join(["\t"+sentence[match_id]['spanString'] for sentence in matches['sentences'] for match_id in sentence]))
    
    # English example
    with CoreNLPClient(timeout=30000, memory='16G') as client:
        englishText = "Albert Einstein was a German-born theoretical physicist. He developed the theory of relativity."
        print('---')
        print(englishText)
        noun_phrases(client,englishText,_annotators="tokenize,ssplit,pos,lemma,parse")
    
    # French example
    with CoreNLPClient(properties='french', timeout=30000, memory='16G') as client:
        frenchText = "Je suis John."
        print('---')
        print(frenchText)
        noun_phrases(client,frenchText,_annotators="tokenize,ssplit,mwt,pos,lemma,parse")
    
    0 讨论(0)
提交回复
热议问题