本文整理匯總了Python中rule.Rule.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python Rule.parse方法的具體用法?Python Rule.parse怎麽用?Python Rule.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rule.Rule
的用法示例。
在下文中一共展示了Rule.parse方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: start_state
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import parse [as 別名]
def start_state(root):
''' None -> <s>^{g-1} . TOP </s>^{g-1} '''
## LMState.cache = {}
lmstr = LMState.lm.raw_startsyms()
lhsstr = lmstr + [root] + LMState.lm.raw_stopsyms()
edge = Hyperedge(None, [root], Vector(), lhsstr)
edge.lmlhsstr = LMState.lm.startsyms() + [root] + LMState.lm.stopsyms()
edge.rule = Rule.parse("ROOT(TOP) -> x0 ### ")
sc = root.bestres[0] if FLAGS.futurecost else 0
return LMState(None, [DottedRule(edge, dot=len(lmstr))], LMState.lm.startsyms(),
step=0, score=sc) # future cost
示例2: POST
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import parse [as 別名]
def POST(self):
"""Remove a rule from the database."""
i = web.input()
web.header('Content-Type', 'application/json')
try:
rule = Rule.parse(i.rule_string)
INSTANCE['language'].validate_rule(rule)
INSTANCE['game'].remove_rule(rule)
except ValueError:
LOGGER.error('syntax error in rule "%s"', i.rule_string)
return error_response(SEMANTIC_ERROR,
'RULE is MODAL VERB PREPOSITION OBJECT')
except InvalidRuleException:
LOGGER.error('invalid token in rule "%s"', i.rule_string)
return error_response(SYNTAX_ERROR, 'invalid token in rule')
except NonexistentRuleException:
LOGGER.info('attempt to remove nonexistent rule "%s"', rule)
return error_response(LOGICAL_ERROR, 'rule does not exist')
else:
LOGGER.info('successfully removed "%s"', rule)
return success_response('Rule removed.')
示例3: add_rule
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import parse [as 別名]
def add_rule(self, rule):
rule = Rule.parse(rule)
self.rules.append(rule)
示例4: load
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import parse [as 別名]
#.........這裏部分代碼省略.........
tailnodes = []
lhsstr = [] # 123 "thank" 456
lmstr = []
lmscore = 0
lmlhsstr = []
for x in tails:
if x[0]=='"': # word
word = desymbol(x[1:-1])
lhsstr.append(word) ## desymbol here and only here; ump will call quoteattr
if lm is not None:
this = lm.word2index(word)
lmscore += lm.ngram.wordprob(this, lmstr)
lmlhsstr.append(this)
lmstr += [this,]
else: # variable
assert x in forest.nodes, "BAD TOPOL ORDER: node #%s is referred to " % x + \
"(in a hyperedge of node #%s) before being defined" % iden
tail = forest.nodes[x]
tailnodes.append(tail)
lhsstr.append(tail)
if lm is not None:
lmstr = [] # "..." "..." x0 "..."
lmlhsstr.append(tail) # sync with lhsstr
fvector = Vector(fields)
if lm is not None:
fvector["lm1"] = lmscore # hack
edge = Hyperedge(node, tailnodes, fvector, lhsstr)
edge.lmlhsstr = lmlhsstr
## new
x = rule.split()
edge.ruleid = int(x[0])
if len(x) > 1:
edge.rule = Rule.parse(" ".join(x[1:]) + " ### " + fields)
forest.rules[edge.ruleid] = edge.rule #" ".join(x[1:]) #, None)
else:
edge.rule = forest.rules[edge.ruleid] # cahced rule
node.add_edge(edge)
if is_oracle:
node.oracle_edge = edge
if node.sp_terminal():
node.word = node.edges[0].subs[0].word
## splitted nodes 12-3-4 => (12, 3, 4)
tmp = sorted([(map(int, x.iden.split("-")), x) for x in forest.nodeorder])
forest.nodeorder = [x for (_, x) in tmp]
forest.rehash()
sentid += 1
## print >> logs, "sent #%d %s, %d words, %d nodes, %d edges, loaded in %.2lf secs" \
## % (sentid, forest.tag, forest.len, num, forest.num_edges, time.time() - basetime)
forest.root = node
node.set_root(True)
line = file.readline()
if line is not None and line.strip() != "":
if line[0] == "(":
forest.goldtree = Tree.parse(line.strip(), trunc=True, lower=False)
line = file.readline()
else:
line = None
forest.number_nodes()
#print forest.root.position_id
total_time += time.time() - start_time
if num_sents % 100 == 0:
print >> logs, "... %d sents loaded (%.2lf secs per sent) ..." \
% (num_sents, total_time/num_sents)
forest.subtree() #compute the subtree string for each node
yield forest
if first is not None and num_sents >= first:
break
# better check here instead of zero-division exception
if num_sents == 0:
print >> logs, "NO FORESTS FOUND!!! (empty input file?)"
sys.exit(1)
# yield None # new: don't halt -- WHY?
Forest.load_time = total_time
print >> logs, "%d forests loaded in %.2lf secs (avg %.2lf per sent)" \
% (num_sents, total_time, total_time/(num_sents+0.001))
示例5: test_produce_context
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import parse [as 別名]
def test_produce_context():
"For testing, produce a static context"
rule = Rule.parse(test_rule)
return LocalContext(test_node, test_edge, rule, test_sent)