0
我有一個包含字符串文件像解析NLTK塊串,形成樹
Tree('S', [Tree('NP', [('criminal', 'JJ'), ('lawyer', 'NN')]), Tree('NP',
[('new', 'JJ'), ('york', 'NN')])])
是否有解析字符串再次產生樹結構的Python函數?我嘗試了Tree.fromstring函數,但它不解析。
我產生這些字符串等預先低於
>>> import nltk
>>> from nltk import pos_tag
>>> pattern = """NP: {<DT>?<JJ>*<NN>}
... VBD: {<VBD>}
... IN: {<IN>}"""
>>> NPChunker = nltk.RegexpParser(pattern)
>>> sentence = 'criminal lawyer new york'.split()
>>> pos_tag(sentence)
[('criminal', 'JJ'), ('lawyer', 'NN'), ('new', 'JJ'), ('york', 'NN')]
>>> result = NPChunker.parse(pos_tag(sentence))
>>> result
Tree('S', [Tree('NP', [('criminal', 'JJ'), ('lawyer', 'NN')]), Tree('NP',
[('new', 'JJ'), ('york', 'NN')])])
感謝。
Thanks @BoarGules – user2745862