我使用NLTK ne_chunk从文本中提取命名实体:
my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."
nltk.ne_chunk(my_sent, binary=True)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何将这些实体保存到列表中?例如 -
print Entity_list
('WASHINGTON', 'New York', 'Loretta', 'Brooklyn', 'African')
Run Code Online (Sandbox Code Playgroud)
谢谢.
我使用nltk的Tree数据结构来处理parsetree字符串.
from nltk.tree import Tree
parsed = Tree('(ROOT (S (NP (PRP It)) (VP (VBZ is) (ADJP (RB so) (JJ nice))) (. .)))')
Run Code Online (Sandbox Code Playgroud)
但是,数据结构似乎有限.是否可以通过它的字符串值获取节点然后导航到顶部或底部?
例如,假设您想要获取字符串值为"nice"的节点,然后查看其父节点,子节点等是什么.是否可以通过nltk的树实现?
当我从nltk执行stanford解析器时,我得到以下结果.
(S (VP (VB get) (NP (PRP me)) (ADVP (RB now))))
Run Code Online (Sandbox Code Playgroud)
但我需要它的形式
S -> VP
VP -> VB NP ADVP
VB -> get
PRP -> me
RB -> now
Run Code Online (Sandbox Code Playgroud)
如何使用递归函数获得此结果.有内置功能吗?