當前位置: 首頁>>代碼示例>>Python>>正文


Python Rule.last方法代碼示例

本文整理匯總了Python中rule.Rule.last方法的典型用法代碼示例。如果您正苦於以下問題:Python Rule.last方法的具體用法?Python Rule.last怎麽用?Python Rule.last使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rule.Rule的用法示例。


在下文中一共展示了Rule.last方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import last [as 別名]
    def __init__(self):
        firstRule = Rule()

        i = 0
        # reset number of rules and hashtable
        Rule.numRules = 0
        #symbol.theDigrams.clear()
        # loop until done
        done = False
        input_string = "abracadabraarbadacarba"
        for i in input_string:
            #print "before: " + str(Rule.numRules)
            firstRule.last().insertAfter(Terminal(i))
            #print "middle: " + str(Rule.numRules)
            firstRule.last().p.check()
            #print "after: " + str(Rule.numRules)
        print firstRule.getRules()
        print firstRule.theGuard.n
        print firstRule.theGuard.n.value
        print firstRule.theGuard.p.value
開發者ID:greatmazinger,項目名稱:parallel-sequitur,代碼行數:22,代碼來源:sequitur.py

示例2: Grammar

# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import last [as 別名]
class Grammar(object):
    """docstring for Grammar"""
    def __init__(self):
        super(Grammar, self).__init__()
        self.digram_index = {}
        self.root_production = Rule(self)

    def train_string(self, string):
        """docstring for train_string"""
        input_sequence = [c for c in string]
        if (0 < len(input_sequence)):
            self.root_production.last().insert_after(Symbol.factory(self, input_sequence.pop(0)))
        while (0 < len(input_sequence)):
            self.root_production.last().insert_after(Symbol.factory(self, input_sequence.pop(0)))
            match = self.get_index(self.root_production.last().prev)
            if not match:
                self.add_index(self.root_production.last().prev)
            elif match.next != self.root_production.last().prev:
                self.root_production.last().prev.process_match(match)

    def add_index(self, digram):
        """docstring for index"""
        self.digram_index[digram.hash_value()] = digram

    def get_index(self, digram):
        """docstring for get"""
        return self.digram_index.get(digram.hash_value())

    def clear_index(self, digram):
        """docstring for clear_index"""
        if self.digram_index.get(digram.hash_value()) == digram:
            self.digram_index[digram.hash_value()] = None

    def print_grammar(self):
        """docstring for print_grammar"""
        output_array = []
        rule_set = [self.root_production]
    
        i = 0
        for rule in rule_set:
            output_array.append("%s --(%d)--> " % (i, rule.reference_count))
            line_length = rule.print_rule(rule_set, output_array, len("%s --(%d)--> " % (i, rule.reference_count)))
        
            if i > 0:
                output_array.append(' ' * (57 - line_length))
                line_length = rule.print_rule_expansion(rule_set, output_array, line_length)
            output_array.append('\n');
            i += 1
        return "".join(output_array)
開發者ID:mspandit,項目名稱:sequitur-python,代碼行數:51,代碼來源:grammar.py

示例3: process_match

# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import last [as 別名]
    def process_match(self, match):
        """Deal with a matching digram"""
        from rule import Rule
        rule = None
        if (match.prev.is_guard() and match.next.next.is_guard()):
            # reuse an existing rule
            rule = match.prev.rule
            self.substitute(rule)
            self.prev.propagate_change()
        else:
            # create a new rule
            rule = Rule(self.grammar)
            rule.last().insert_after(Symbol.factory(self.grammar, self))
            rule.last().insert_after(Symbol.factory(self.grammar, self.next))
            self.grammar.add_index(rule.first())
            
            match.substitute(rule)
            match.prev.propagate_change()
            self.substitute(rule)
            self.prev.propagate_change()

        # Check for an under-used rule
        if (NonTerminal == type(rule.first()) and (rule.first().rule.reference_count == 1)):
            rule.first().expand()
開發者ID:mspandit,項目名稱:sequitur-python,代碼行數:26,代碼來源:symbol.py


注:本文中的rule.Rule.last方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。