本文整理汇总了Python中tree.Tree方法的典型用法代码示例。如果您正苦于以下问题:Python tree.Tree方法的具体用法?Python tree.Tree怎么用?Python tree.Tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree
的用法示例。
在下文中一共展示了tree.Tree方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: all_matches
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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))
示例2: getDefaultObjectsTree
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
def getDefaultObjectsTree():
"""List of tuples (object, [children]).
"""
tree = Tree()
tree[Detector.FACE][Detector.EYE]
tree[Detector.FACE][Detector.NOSE]
return tree
示例3: getObjectsTree
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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
示例4: __init__
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
def __init__(self, shapes):
self.shapes = shapes
self.tree = tree.Tree(shapes)
示例5: reset
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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)}
示例6: read_tree
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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
示例7: gen_simple_trees
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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)
示例8: gen_simple_trees
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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
示例9: detect
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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
示例10: __init__
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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
示例11: go_single
# 需要导入模块: import tree [as 别名]
# 或者: from tree import Tree [as 别名]
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()