当前位置: 首页>>代码示例>>Python>>正文


Python Rule.parse方法代码示例

本文整理汇总了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
开发者ID:rupenp,项目名称:transforest,代码行数:16,代码来源:lmstate.py

示例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.')
开发者ID:ryanwilsonperkin,项目名称:calvinball,代码行数:23,代码来源:server.py

示例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)
开发者ID:dnc1994,项目名称:Shape,代码行数:5,代码来源:inferencer.py

示例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))
开发者ID:srush,项目名称:tf-fork,代码行数:104,代码来源:forest.py

示例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)
开发者ID:srush,项目名称:tf-fork,代码行数:6,代码来源:local_features.py


注:本文中的rule.Rule.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。