Python tree.Tree() Examples
The following are 12
code examples of tree.Tree().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tree
, or try the search function
.
Example #1
Source File: article.py From Greynir with GNU General Public License v3.0 | 6 votes |
def all_matches(cls, criteria, pattern, enclosing_session=None): """ Generator of SimpleTree objects (see matcher.py) from articles matching the given criteria and the pattern """ with SessionContext( commit=True, read_only=True, session=enclosing_session ) as session: # t0 = time.time() mcnt = acnt = tcnt = 0 # print("Starting article loop") for a in cls.articles(criteria, enclosing_session=session): acnt += 1 tree = Tree(url=a.url, authority=a.authority) tree.load(a.tree) for ix, simple_tree in tree.simple_trees(): tcnt += 1 for match in simple_tree.all_matches(pattern): yield (a, ix, match) mcnt += 1 # t1 = time.time() # print("{0} articles with {1} trees examined, {2} matches in {3:.2f} seconds" # .format(acnt, tcnt, mcnt, t1-t0))
Example #2
Source File: detector.py From detection with GNU General Public License v2.0 | 5 votes |
def getDefaultObjectsTree(): """List of tuples (object, [children]). """ tree = Tree() tree[Detector.FACE][Detector.EYE] tree[Detector.FACE][Detector.NOSE] return tree
Example #3
Source File: common.py From detection with GNU General Public License v2.0 | 5 votes |
def getObjectsTree(qTreeView, table, indexes, extract): """Create an object tree representation from QTreeView. """ tree = Tree() model = qTreeView.model() extracted = tree.fromQStandardItemModel(model, table, indexes, extract) return tree, extracted
Example #4
Source File: scene.py From xy with MIT License | 5 votes |
def __init__(self, shapes): self.shapes = shapes self.tree = tree.Tree(shapes)
Example #5
Source File: neurocuts_env.py From neurocuts with Apache License 2.0 | 5 votes |
def reset(self): self.num_actions = 0 self.exceeded_max_depth = [] self.tree = Tree( self.rules, self.leaf_threshold, refinements={ "node_merging": True, "rule_overlay": True, "region_compaction": False, "rule_pushup": False, "equi_dense": False, }) self.node_map = { self.tree.root.id: self.tree.root, } self.child_map = {} if self.force_partition: if self.force_partition == "cutsplit": self.tree.partition_cutsplit() elif self.force_partition == "efficuts": self.tree.partition_efficuts() else: assert False, self.force_partition for c in self.tree.root.children: self.node_map[c.id] = c self.child_map[self.tree.root.id] = [ c.id for c in self.tree.root.children ] start = self.tree.current_node return {start.id: self._encode_state(start)}
Example #6
Source File: dataset.py From TreeLSTMSentiment with MIT License | 5 votes |
def read_tree(self, line): parents = map(int,line.split()) trees = dict() root = None for i in xrange(1,len(parents)+1): #if not trees[i-1] and parents[i-1]!=-1: if i-1 not in trees.keys() and parents[i-1]!=-1: idx = i prev = None while True: parent = parents[idx-1] if parent == -1: break tree = Tree() if prev is not None: tree.add_child(prev) trees[idx-1] = tree tree.idx = idx-1 #if trees[parent-1] is not None: if parent-1 in trees.keys(): trees[parent-1].add_child(tree) break elif parent==0: root = tree break else: prev = tree idx = parent return root
Example #7
Source File: dataset.py From TreeLSTMSentiment with MIT License | 5 votes |
def read_tree(self, line, label_line): # FIXED: tree.idx, also tree dict() use base 1 as it was in dataset # parents is list base 0, keep idx-1 # labels is list base 0, keep idx-1 #parents = map(int,line.split()) # split each number and turn to int parents = list(map(int,line.split())) # split each number and turn to int trees = dict() # this is dict root = None #labels = map(self.parse_dlabel_token, label_line.split()) labels = list(map(self.parse_dlabel_token, label_line.split())) for i in range(1,len(parents)+1): #for i in range(1,len(list(parents))+1): #if not trees[i-1] and parents[i-1]!=-1: if i not in trees.keys() and parents[i-1]!=-1: idx = i prev = None while True: parent = parents[idx-1] if parent == -1: break tree = Tree() if prev is not None: tree.add_child(prev) trees[idx] = tree tree.idx = idx # -1 remove -1 here to prevent embs[tree.idx -1] = -1 while tree.idx = 0 tree.gold_label = labels[idx-1] # add node label #if trees[parent-1] is not None: if parent in trees.keys(): trees[parent].add_child(tree) break elif parent==0: root = tree break else: prev = tree idx = parent return root
Example #8
Source File: pairgen.py From Greynir with GNU General Public License v3.0 | 5 votes |
def gen_simple_trees(criteria, stats): """ Generate simplified parse trees from articles matching the criteria """ for a in Article.articles(criteria): if not a.root_domain or "raduneyti" in a.root_domain: # Skip ministry websites due to amount of chaff found there continue tree = Tree(url = a.url, authority = a.authority) # Note the parse timestamp stats["parsed"] = a.parsed tree.load(a.tree) for ix, stree in tree.simple_trees(): yield stree, tree.score(ix), tree.length(ix)
Example #9
Source File: gencorp.py From Greynir with GNU General Public License v3.0 | 5 votes |
def gen_simple_trees(criteria): """ Generate simplified parse trees from articles matching the criteria """ for a in Article.articles(criteria): # Skip articles from certain websites if ( not a.root_domain or "raduneyti" in a.root_domain or "lemurinn" in a.root_domain ): continue # Load tree from article try: tree = Tree(url=a.url, authority=a.authority) tree.load(a.tree) except Exception as e: print("Exception loading tree in {0}: {1}".format(a.url, e)) # Skip it continue # Yield simple trees for each article sentence for ix, stree in tree.simple_trees(): text = stree.text tokens = text.split() # Make sure it has enough tokens if not len(tokens) >= MIN_SENT_LENGTH: continue # Skip sentences containing something in our bag of English words wordset = set([t.lower() for t in tokens]) if wordset & ENGLISH_WORDS: continue yield stree, tree.score(ix), tree.length(ix), a.uuid, a.url, ix
Example #10
Source File: detector.py From detection with GNU General Public License v2.0 | 4 votes |
def detect(self, img, tree, equalizeHist=True, debugTable=None, autoNeighbors=None, autoNeighborsParam=0): def detectTree(tree, parentRoi, parentName, parentHash, roiTree): """Recursive function to detect objects in the tree. """ x, y, w, h = parentRoi cropped = img[y:y+h, x:x+w] for node, children in tree.iteritems(): selected, param = node.data incNeighbors = True while incNeighbors: if debugTable and not autoNeighbors and selected: col1 = '{} ({})'.format(param.classifier, param.name) col2 = 'detecting in {}x{} ({})...'.format(w, h, parentName) debugTable([(col1, 200), (col2, 300), ('', 200)]) start = time.time() rects = self.detectObject(cropped, param.classifier, param.scaleFactor, param.minNeighbors, param.minSize, cv2.CASCADE_SCALE_IMAGE) if isinstance(rects, np.ndarray): rects = rects.tolist() self.globalizeCoords(rects, parentRoi) hashs = None tracking = None if not autoNeighbors: res = self.stabilize(param, parentHash, rects) if res: rects, hashs = zip(*res[-1]) if res[-1] else ([], []) tracking = res[:-1] if debugTable and not autoNeighbors and selected: end = time.time() col = '{} found in {:.2f} s'.format(len(rects), end - start) debugTable([(col, 0)], append=True) if autoNeighbors and node == autoNeighbors and len(rects) > autoNeighborsParam: param.minNeighbors += 1 else: incNeighbors = False for i, roi in enumerate(rects): hash = hashs[i] if hashs else None roiNode = Node(param.classifier, (roi, param, tracking)) roiTree[roiNode] name = parentName + ' > ' + param.name detectTree(children, roi, name, hash, roiTree[roiNode]) img = self.preprocess(img, equalizeHist) self.preprocessed = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) h, w = img.shape[:2] roiTree = Tree() detectTree(tree, (0, 0, w, h), 'Root', None, roiTree) return roiTree
Example #11
Source File: decisionTree.py From Machine_Learning with GNU General Public License v2.0 | 4 votes |
def __init__(self, Mat, Tag, Discrete = None, Depth = None, W=None): """ @Mat: Inputed data points which represent as a matrix. Every column vector in @Mat is a feature of training set @Tag: Labels with training points. @Discrete: It's a bool vector of size @SamplesNumber Discrete[i] == 0 means the i-th feature is discrete feature, otherwise it's continuous In default, all feature is discrete """ self._Mat = numpy.array(Mat) self._Tag = numpy.array(Tag).flatten() """ @SamplesNum: how many sample points in inputed training set @SamplesDem: the demention of inputed point which **also** means how many features in inputed data. """ self.SamplesDem = self._Mat.shape[0] self.SamplesNum = self._Mat.shape[1] self.feature_dict = {} self.labels = numpy.unique(self._Tag) self.DT = Tree() if Discrete == None: self.Discrete = [True for i in range(self.SamplesDem)] else: self.Discrete = Discrete for i in range(self.SamplesDem): if self.Discrete[i] == True: self.feature_dict[i] = numpy.unique(self._Mat[i, :]) if self.SamplesNum != self._Tag.size: print "Error: Make sure that the number of tag ",\ "is same as points in inputed data" self.limitedDepth = Depth self.currentDepth = 0 assert Depth >= 1 if W == None: self.W = {} for i in range(self.SamplesNum): val = toHashableVal(self._Mat[:, i]) self.W[ val ] = 1.0/self.SamplesNum else: assert isinstance(W, dict) self.W = W
Example #12
Source File: processor.py From Greynir with GNU General Public License v3.0 | 4 votes |
def go_single(self, url): """ Single article processor that will be called by a process within a multiprocessing pool """ print("Processing article {0}".format(url)) sys.stdout.flush() # If first article within a new process, import the processor modules if self.pmodules is None: self.pmodules = [ importlib.import_module(modname) for modname in self.processors ] # Load the article with closing(self._db.session) as session: try: article = session.query(Article).filter_by(url=url).one_or_none() if article is None: print("Article not found in scraper database") else: if article.tree and article.tokens: tree = Tree(url, article.authority) tree.load(article.tree) token_container = TokenContainer( article.tokens, url, article.authority ) # Run all processors in turn for p in self.pmodules: if p.PROCESSOR_TYPE == "tree": tree.process(session, p) elif p.PROCESSOR_TYPE == "token": token_container.process(session, p) else: assert False, ( "Unknown processor type '" + p.PROCESSOR_TYPE + "' (should be 'tree' or 'token')" ) # Mark the article as being processed article.processed = datetime.utcnow() # So far, so good: commit to the database session.commit() except Exception as e: # If an exception occurred, roll back the transaction session.rollback() print( "Exception in article {0}, transaction rolled back\nException: {1}".format( url, e ) ) raise sys.stdout.flush()