def buildTree(self, dTreeCluster, elementList): bTree = BTree() #for root of tree key = 0 rootTreeNode = TreeNode(key, NodeInfo("Phrase", ccgtag=[])) bTree.put(rootTreeNode) #for the others #left tree #self.foreachChunk(bTree, rootTreeNode, dTreeCluster[0], position=0) #right tree parentChunk = dTreeCluster dtree = dTreeCluster insertNode = rootTreeNode for i in range(0, len(dTreeCluster)): self.foreachChunk(bTree, rootTreeNode, dtree, parentChunk, dTreeCluster[i], insertNode, position=0) self.assigneDependencyForBTree(bTree, elementList) self.adjustTree(bTree) return bTree
def foreachChunk(self, dtree, chunk): bTree = None bChildTree = None if hasattr(chunk[0], "form") == True: i = 0 else: i = len(chunk) - 1 while i >= 0: if hasattr(chunk[i], "form") == True: bChildTree = self.buildBranch(chunk, self.key) self.key += bChildTree.size + 1 else: bChildTree = self.foreachChunk(dtree, chunk[i]) if bTree is None: bTree = bChildTree else: tempTree = BTree() # can be difine CCG here for node treeNode = TreeNode(self.key, NodeInfo("", ccgtag=[])) self.key += 1 treeNode.parent = None tempTree.put(treeNode) tempTree.putTree(bTree, treeNode, 1) tempTree.putTree(bChildTree, treeNode, 0) bTree = tempTree bChildTree = None i -= 1 return bTree ''' #non delete
def buildTree(self, dTreeCluster, elementList): bTree = BTree() # for root of tree key = 0 bTree = None bChildTree = None # for the others # left tree # self.foreachChunk(bTree, rootTreeNode, dTreeCluster[0], position=0) # right tree dtree = dTreeCluster #i = len(dTreeCluster)-1 #while i>=0: # bTree = self.foreachChunk(bTree,bChildTree, dtree, dTreeCluster[i], position=0) # i -=1 bTree = self.foreachChunk(dtree, dTreeCluster) self.assigneDependencyForBTree(bTree, elementList) self.adjustTree(bTree) return bTree
def testBtree(): nodeInfo1 = NodeInfo("Sentence", "S") nodeInfo2 = NodeInfo("Henri", "NP") nodeInfo3 = NodeInfo("mange", "(S\\NP)/NP") nodeInfo4 = NodeInfo("des", "NP\\NP") nodeInfo5 = NodeInfo("pommes", "NP") nodeInfo6 = NodeInfo("que", "Empty") nodeInfo7 = NodeInfo("je", "NP") nodeInfo8 = NodeInfo("lui", "NP") nodeInfo9 = NodeInfo("donne", "(S\\NP)/NP") mytree = BTree() mytree.put(nodeInfo1, 0) mytree.put(nodeInfo2, 0) mytree.put(nodeInfo3, 0) mytree.put(nodeInfo4, 1) mytree.put(nodeInfo5, 0) mytree.put(nodeInfo6, 0) mytree.put(nodeInfo7, 1) mytree.put(nodeInfo8, 0) mytree.put(nodeInfo9, 1) mytree.drawTree() print(mytree)
def buildBranchWithFirstChunk(self, cluster, key): primaryElement = cluster[0] cluster = sorted(cluster, key=lambda element: int(element.id)) btreeChild = BTree() sizeCluster = len(cluster) if sizeCluster == 1: # Btree with one noeud element = cluster[0] nodeInfo = NodeInfo(element.form, element=element, ccgtag=[], dependency=element.deprel, cluster=[int(element.id), int(element.id)]) treeNode = TreeNode(key, nodeInfo) btreeChild.put(treeNode) key += 1 elif len(cluster) >= 2: for i in range(0, sizeCluster): if btreeChild.length() < 2: nodeInfo = NodeInfo("ccg", ccgtag=[]) treeNode = TreeNode(key, nodeInfo) key = key + 1 btreeChild.put(treeNode) # left: 0 root right:1 if cluster[i] is primaryElement: element = cluster[i] elementHead = self.findSmallestHeadinChunk(cluster) elementId = self.findSmallestIdinChunk(cluster) if int(element.id) == int(elementId.id) and int( element.head) == int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(element.id), int(elementHead.head) ]) elif int(element.id) != int(elementId.id) and int( element.head) == int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(elementId.id), int(elementHead.head) ]) elif int(element.id) == int(elementId.id) and int( element.head) != int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(elementId.id), int(elementHead.head) ]) else: nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel) else: nodeInfoLeaf = NodeInfo(cluster[i].form, ccgtag=[], element=cluster[i], dependency=None) treeNodeLeaf = TreeNode(key, nodeInfoLeaf, parent=treeNode) key += 1 btreeChild.put(treeNodeLeaf, position=0) else: btreeChildTemp = BTree() nodeInfo = NodeInfo("ccg", ccgtag=[]) treeNode = TreeNode(key, nodeInfo) key += 1 btreeChildTemp.put(treeNode) # left: 0 root right:1 if cluster[i] is primaryElement: element = cluster[i] elementHead = self.findSmallestHeadinChunk(cluster) elementId = self.findSmallestIdinChunk(cluster) if int(element.id) == int(elementId.id) and int( element.head) == int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(element.id), int(elementHead.head) ]) elif int(element.id) != int(elementId.id) and int( element.head) == int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(elementId.id), int(elementHead.head) ]) elif int(element.id) == int(elementId.id) and int( element.head) != int(elementHead.head): nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel, cluster=[ int(elementId.id), int(elementHead.head) ]) else: nodeInfoLeaf = NodeInfo(element.form, ccgtag=[], element=element, dependency=element.deprel) else: nodeInfoLeaf = NodeInfo(cluster[i].form, ccgtag=[], element=cluster[i], dependency=None) treeNodeLeaf = TreeNode(key, nodeInfoLeaf, parent=treeNode) key += 1 btreeChildTemp.put(treeNodeLeaf, position=0) btreeChild.putTree(btreeChildTemp, btreeChild.getExtremeRightNode(), position=1) return btreeChild