本文整理汇总了Python中nltk.Tree.subtrees方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.subtrees方法的具体用法?Python Tree.subtrees怎么用?Python Tree.subtrees使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.Tree
的用法示例。
在下文中一共展示了Tree.subtrees方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: chunkingList
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import subtrees [as 别名]
def chunkingList(dataS, chunkgram):
"""
This function will find the chunk
"""
#data = str(dataS)
words = word_tokenize(str(dataS)[1:])
#print words
ps = pos_tag(words)
# print ps
chunkParser = RegexpParser(chunkgram)
chunked = chunkParser.parse(ps)
#print chunked
tree = Tree('s', chunked)
docs = []
for subtree in tree.subtrees(filter=lambda t: t.label() == 'Chunk'):
# Assemble the chunk into one line and strip extra punctuations
docs.append(" ".join([a for (a,b) in subtree.leaves()]))
return docs
示例2: convert_psd_sent_2_segmentation_2
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import subtrees [as 别名]
def convert_psd_sent_2_segmentation_2(parsed_corpus):
SegCorpus=[]
for top_k_psd_of_the_sent in parsed_corpus:
segmentation=[]
best_score, best_parse_tree_str= top_k_psd_of_the_sent[0]
tree=Tree(best_parse_tree_str)
# tree=ROOT, tree[0]=S, tree[0, ] is the subtrees of S, i.e. POS tags, we can use alternative methods
# note that it is highly dependent on the format of the parser outputs!!
for subtree in tree.subtrees(lambda t: t.height()==tree.height()-2):
segmentation.append(''.join(subtree.leaves()))
SegCorpus.append(segmentation)
if not ''.join(segmentation)==''.join(tree.leaves()):
print('Error! Leaves/characters in thee segmentation != total characters in the tree (as leaves), Double check the format and/or code!')
break
return SegCorpus
示例3: makeConstNode
# 需要导入模块: from nltk import Tree [as 别名]
# 或者: from nltk.Tree import subtrees [as 别名]
def makeConstNode(i,node,constDic, rootindeces,tree):
if node["cat"] in constDic:
if node["cat"]=="V":
if node.get("mode","")=="infinitive":
ntree=Tree("(VP (V "+node["t"]+"))")
elif "V" in node["govcats"]:
j,func=node["govcats"]["V"][0]
if func.startswith("para"): # copy governor's const
gov=tree[j]
if "const" not in gov:
makeConstNode(j,gov,constDic, rootindeces,tree)
if unicode(gov["const"])[1]=="V": # governor's const is VP
ntree=Tree("(VP (V "+node["t"]+"))")
else: # governor's const is S
ntree=Tree("(S (V "+node["t"]+"))")
else: # verb depends on verb
ntree=Tree("(VP (V "+node["t"]+"))")
else:
ntree=Tree("(S (V "+node["t"]+"))")
else:
ntree=constDic[node["cat"]].copy(True)
ntree.insert(0,Tree("("+node["cat"]+" "+node["t"]+")"))
else:
if node["children"] or i in rootindeces or node["cat"] in node["govcats"]:
ntree=Tree("("+node["cat"]+"P ("+node["cat"]+" "+node["t"]+"))")
else:
ntree=Tree("("+node["cat"]+" "+node["t"]+")")
ntree.depindex=i
ntree.function=unicode(node["gov"].items()[0][1])
#ntree.t=node["t"]
node["const"]=ntree
for t in list(ntree.subtrees(lambda t: t.height() == 2)):
t.depindex=i
#t.lexid=node["lexid"]
t.lexid=node["id"]
t.t=node["t"]