[英]How can I access the contents of a node in a NLTK tree?
我正在嘗試使用NLTK樹基於單詞搜索POS標簽。
我想在樹中找到一個詞(這里:不同)(該詞肯定存在於樹中),並檢查包含該詞的節點的任何子代是否具有給定標簽(這里:NN)。
from nltk.tree import Tree
input_string = '(ROOT (SBARQ (WHADVP (WRB How)) (SQ (VBZ is) (NP (PRP it)) (ADJP (JJ different) (PP (IN from) (NP (DT the) (JJ dishonest) (NNS businessmen))))) (. ?)))'
for t in Tree.fromstring(input_string, read_node=lambda s: '<%s>' % s, read_leaf=lambda s: '"%s"' % s):
print (t)
我試圖瀏覽文檔,但是我無法獲得更多的信息。
我想做的是:
if t.leaves() in ["different"]:
if content_of_t (I don't know how to access that) in ["NN"]:
return "yes"
您可以遍歷樹的所有子樹。
tree = Tree.fromstring(
input_string,
read_node=lambda s: '<%s>' % s,
read_leaf=lambda s: '%s' % s)
for sub_tree in tree.subtrees():
if sub_tree.label() == '<JJ>' and 'different' in set(sub_tree.leaves()):
print('yes')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.