本文整理汇总了Python中tree.Tree.process方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.process方法的具体用法?Python Tree.process怎么用?Python Tree.process使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.process方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: go_single
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import process [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)
# 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)
# 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()
示例2: __init__
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import process [as 别名]
class Query:
""" A Query is initialized by parsing a query string using QueryRoot as the
grammar root nonterminal. The Query can then be executed by processing
the best parse tree using the nonterminal handlers given above, returning a
result object if successful. """
def __init__(self, session):
self._session = session
self._error = None
self._answer = None
self._tree = None
self._qtype = None
self._key = None
self._toklist = None
@staticmethod
def _parse(toklist):
""" Parse a token list as a query """
# Parse with the nonterminal 'QueryRoot' as the grammar root
with Fast_Parser(verbose=False, root=_QUERY_ROOT) as bp:
sent_begin = 0
num_sent = 0
num_parsed_sent = 0
rdc = Reducer(bp.grammar)
trees = dict()
sent = []
for ix, t in enumerate(toklist):
if t[0] == TOK.S_BEGIN:
sent = []
sent_begin = ix
elif t[0] == TOK.S_END:
slen = len(sent)
if not slen:
continue
num_sent += 1
# Parse the accumulated sentence
num = 0
try:
# Parse the sentence
forest = bp.go(sent)
if forest is not None:
num = Fast_Parser.num_combinations(forest)
if num > 1:
# Reduce the resulting forest
forest = rdc.go(forest)
except ParseError as e:
forest = None
if num > 0:
num_parsed_sent += 1
# Obtain a text representation of the parse tree
trees[num_sent] = ParseForestDumper.dump_forest(forest)
# ParseForestPrinter.print_forest(forest)
elif t[0] == TOK.P_BEGIN:
pass
elif t[0] == TOK.P_END:
pass
else:
sent.append(t)
result = dict(num_sent=num_sent, num_parsed_sent=num_parsed_sent)
return result, trees
def parse(self, toklist, result):
""" Parse the token list as a query, returning True if valid """
self._tree = None # Erase previous tree, if any
self._error = None # Erase previous error, if any
self._qtype = None # Erase previous query type, if any
self._key = None
self._toklist = None
parse_result, trees = Query._parse(toklist)
if not trees:
# No parse at all
self.set_error("E_NO_TREES")
return False
result.update(parse_result)
if result["num_sent"] != 1:
# Queries must be one sentence
self.set_error("E_MULTIPLE_SENTENCES")
return False
if result["num_parsed_sent"] != 1:
# Unable to parse the single sentence
self.set_error("E_NO_PARSE")
return False
if 1 not in trees:
# No sentence number 1
self.set_error("E_NO_FIRST_SENTENCE")
return False
# Looks good
# Store the resulting parsed query as a tree
tree_string = "S1\n" + trees[1]
#.........这里部分代码省略.........