我有两个目标:
(1)创建名词短语列表,而不是使用’traverse()’方法打印它们.我目前使用StringIO来记录现有traverse()方法的输出.这不是一个可接受的解决方案.
(2)解析名词短语字符串,以便:'(NP Michael / NNP Jackson / NNP)成为’Michael Jackson’.在NLTK中有解除解析的方法吗?
NLTK文档建议使用traverse()来查看名词短语,但是如何在这个递归方法中捕获’t’,以便生成一个字符串名词短语列表?
from nltk.tag import pos_tagdef traverse(t): try: t.label() except AttributeError: return else: if t.label() == 'NP': print(t) # or do something else else: for child in t: traverse(child)def nounPhrase(tagged_sent): # Tag sentence for part of speech tagged_sent = pos_tag(sentence.split()) # List of tuples with [(Word,PartOfSpeech)] # define several tag patterns grammar = r""" NP: {<DT|PP$>?<JJ>*<NN>} # chunk determiner/possessive,adjectives and noun {<NNP>+} # chunk sequences of proper nouns {<NN>+} # chunk consecutive nouns """ cp = nltk.RegexpParser(grammar) # define Parser SentenceTree = cp.parse(tagged_sent) NounPhrases = traverse(SentenceTree) # collect Noun Phrase return(NounPhrases)sentence = "Michael Jackson likes to eat at McDonalds"tagged_sent = pos_tag(sentence.split()) NP = nounPhrase(tagged_sent) print(NP)
目前打印:
(NP Michael / NNP Jackson / NNP)
(NP麦当劳/ NNP)
并将’无’存储到NP
def extract_np(psent): for subtree in psent.subtrees(): if subtree.label() == 'NP': yIEld ' '.join(word for word,tag in subtree.leaves())cp = nltk.RegexpParser(grammar)parsed_sent = cp.parse(tagged_sent)for npstr in extract_np(parsed_sent): print (npstr)总结
以上是内存溢出为你收集整理的python – NLTK:如何遍历名词短语以返回字符串列表?全部内容,希望文章能够帮你解决python – NLTK:如何遍历名词短语以返回字符串列表?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)