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


Python Markov.printout方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import printout [as 別名]
class Parser:
    """
    Base class for a lexer/parser that has the rules defined as methods
    """
    tokens = ()
    precedence = ()

    def __init__(self, **kw):
        self.debug = kw.get('debug', 0)
        self.sentences = []
        self.markov = Markov()
        self.clause_starter = {}
        self.para_starter = []
        self.words = kw.get('words', None)

        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        #print self.debugfile, self.tabmodule

        # Build the lexer and parser
        lex.lex(module=self, debug=self.debug)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)

    def run(self):
        s = sys.stdin.read()
        s = s.replace('\n\n', '\x00')
        s = s.replace('\x00\x00', '\x00')
        s = s.replace('\n\n', '')
        s = s.replace('\n', ' ')
        s = s.replace('  ', ' ')
        yacc.parse(s)
        print self.sentences
        self.markov.printout()
        print
        print "clause starters"
        keys = self.clause_starter.keys()
        keys.sort()
        for k in keys:
            v = self.clause_starter[k]
            print "\t", repr(k), v
        print
        print "para starters", self.para_starter
        print
        self.markov.prepare()
        sentence = random_sentence(self.markov, 800,
                                    starters=self.clause_starter,
                                    para_starters=self.para_starter)
        print_sentence(sentence, word_filter=self.words)
開發者ID:davelab6,項目名稱:telaro,代碼行數:57,代碼來源:telaro.py

示例2: __init__

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import printout [as 別名]
class Parser:
    """
    Base class for a lexer/parser that has the rules defined as methods
    """
    tokens = ()
    precedence = ()

    def __init__(self, **kw):
        self.debug = kw.get('debug', 0)
        self.sentences = []
        self.markov = Markov()
        self.clause_starter = {}
        self.para_starter = []
        self.word_filter = kw.get('word_filter', None)
        self.letter_priority = kw.get('letter_priority', None)

        try:
            modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser"+"_"+self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"
        #print self.debugfile, self.tabmodule

        # Build the lexer and parser
        lex.lex(module=self, debug=self.debug)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)

    def run(self, txt=None, para_starter=False):
        if txt is None:
            s = sys.stdin.read()
        else:
            s = txt
        s = s.replace('\n\n', '\x00')
        s = s.replace('\x00\x00', '\x00')
        s = s.replace('\n\n', '')
        s = s.replace('\n', ' ')
        s = s.replace('  ', ' ')
        yacc.parse(s)
        print self.sentences
        self.markov.printout()
        print
        print "clause starters"
        keys = self.clause_starter.keys()
        keys.sort()
        for k in keys:
            v = self.clause_starter[k]
            print "\t", repr(k), v
        print
        print "para starters", self.para_starter
        print
        word_filter = self.word_filter
        if self.letter_priority and word_filter:
            # certain words are given a higher priority (multiplier)
            # than others.
            states = self.markov.states
            for from_word, fp in states.items():
                for to_word in fp.keys():
                    if word_filter.has_key(to_word.lower()):
                        fp[to_word] *= self.letter_priority
            word_filter = None
        self.markov.prepare()
        if para_starter:
            para_starters = None
        else:
            para_starters = self.para_starter
        sentence = random_sentence(self.markov, 800,
                                    starters=self.clause_starter,
                                    para_starters=para_starters)
        return make_sentence(sentence, word_filter=word_filter)
開發者ID:davelab6,項目名稱:telaro,代碼行數:75,代碼來源:fileparse.py


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