我想创建一个alogritmo,让我说一棵树是否包含在另一棵树中。感谢this site,我管理了一种算法,该算法可以让我知道二叉树,但是我想将其概括化。
def isSubtree(T,S):
'''
function to say if a tree S is a subtree of another one, T
'''
# Base Case
if S is None:
return True
if T is None:
return True
# Check the tree with root as current node
if (areIdentical(T, S)):
return True
# IF the tree with root as current node doesn't match
# then try left and right subtreee one by one
return isSubtree(T.children, S) or isSubtree(T.children, S) # won't work because we have several children
def areIdentical(root1, root2):
'''
function to say if two roots are identical
'''
# Base Case
if root1 is None and root2 is None:
return True
if root1 is None or root2 is None:
return False
# Check if the data of both roots their and children are the same
return (root1.data == root2.data and
areIdentical(root1.children, root2.children)) # Here problem because it won't work for children
例如:
>>># first tree creation
>>>text = start becoming popular
>>>textSpacy = spacy_nlp(text1)
>>>treeText = nltk_spacy_tree(textSpacy)
>>>t = WordTree(treeText[0])
>>># second tree creation
>>>question = When did Beyonce start becoming popular?
>>>questionSpacy = spacy_nlp(question)
>>>treeQuestion = nltk_spacy_tree(questionSpacy)
>>>q = WordTree(treeQuestion[0])
>>># tree comparison
>>>isSubtree(t,q)
True
如果这可能有用,这是我使用的WordTree
类:
class WordTree:
'''Tree for spaCy dependency parsing array'''
def __init__(self, tree, is_subtree=False):
"""
Construct a new 'WordTree' object.
:param array: The array contening the dependency
:param parent: The parent of the array if exists
:return: returns nothing
"""
self.parent = []
self.children = []
self.data = tree.label().split('_')[0] # the first element of the tree # We are going to add the synonyms as well.
for subtree in tree:
if type(subtree) == Tree:
# Iterate through the depth of the subtree.
t = WordTree(subtree, True)
t.parent=tree.label().split('_')[0]
elif type(subtree) == str:
surface_form = subtree.split('_')[0]
self.children.append(surface_form)
它与使用Spacy词组的树木配合得很好。
question = "When did Beyonce start becoming popular?"
questionSpacy = spacy_nlp(question)
treeQuestion = nltk_spacy_tree(questionSpacy)
t = WordTree(treeQuestion[0])
答案 0 :(得分:1)
您可以遍历T
的所有子树,如果S
是T
的任何子树的子树,则S
是T
的子树False
。另外,当T
为None
时,您应该返回T
,因为这意味着您已经到了S
的叶子,并且仍然找不到def isSubtree(T,S):
if S is None:
return True
if T is None:
return False
if areIdentical(T, S):
return True
return any(isSubtree(c, S) for c in T.children)
子树:
import mp3play as mP
file = 'C:\path\to\your\*.mp3'
song = mP.load(file)
song.play()