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


Python Markov.learn方法代码示例

本文整理汇总了Python中markov.Markov.learn方法的典型用法代码示例。如果您正苦于以下问题:Python Markov.learn方法的具体用法?Python Markov.learn怎么用?Python Markov.learn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在markov.Markov的用法示例。


在下文中一共展示了Markov.learn方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AveryBot

# 需要导入模块: from markov import Markov [as 别名]
# 或者: from markov.Markov import learn [as 别名]
class AveryBot(SingleServerIRCBot):
    def __init__(self, mindfile, blfile, logfile, agefile,
        real_id, real, ident, birthday, friend):
        SingleServerIRCBot.__init__(self,
            [(ident.server, ident.port)], ident.nickname, ident.realname)

        self.mindfile = mindfile
        self.blfile = blfile
        self.logfile = logfile
        self.agefile = agefile
        # load mind
        try:
            self.mind = pickle.load(open(mindfile, 'rb'))
        except IOError:
            print("No markov file (" + mindfile + ") found; making a blank one")
            self.mind = Markov(2)

        # words that will highlight some nicks, in the form of a dictionary
        # from words to the nicks they hilight.
        try:
            self.blacklist = pickle.load(open(self.blfile, "rb"))
        except FileNotFoundError:
            self.blacklist = []

        class State(dict):
            def __getitem__(self, key):
                if key not in self:
                    super(State, self).__setitem__(key, random.getstate())

                return super(State, self).__getitem__(key)

        # the handle generator
        self.handlegen = Markov(2)
        for line in open("nicks", "r"):
            line = line[:-1] # strip newl
            n = []
            for c in line:
                n.append(MarkovElem(c))

            #n = map(MarkovElem, line)

            n[0].tags["pos"] = "BEGIN"
            n[-1].tags["pos"] = "END"

            self.handlegen.learn(n)

        try:
            self.states = pickle.load(open("rstate", "rb"))
        except FileNotFoundError:
            self.states = State()
        
        self.waiting_for_friend = queue.Queue() # channels to send lurkers to

        self.nick = ident.nickname
        self.password = ident.password
        self.channel = ident.channel    # active channels
        self.cafe = ident.cafe
        self.rstate = random.getstate() # random state
        self.real_id = real_id          # whether self.real is a user or a nick
        self.real = real                # real user she imitates (i.e. avery)
        #self.save_counter = 0           # write to disk every 100 talks
        self.friend = friend
        self.birthday = birthday

    def at_bday(self, birthday):
        now = datetime.datetime.today()
        bday = self.birthday.replace(year = now.year)
        if bday <= now: bday = bday.replace(year = bday.year + 1)
        print("next birthday:", bday)

        bday_action = threading.Timer((bday - now).total_seconds(), birthday)
        bday_action.daemon=True
        bday_action.start()

    def on_welcome(self, c, e):
        c.mode(self.nick, '+B-x')
        c.privmsg("NickServ", "IDENTIFY " + self.password)
        for channel in self.channel:
            c.join(channel)
        c.join(self.cafe)

        def birthday():
            age = int(open(self.agefile, 'r').read()) + 1
            c.action(self.cafe, "is %s years old today!" % age)
            print(age, file=open(self.agefile, 'w'))
            self.at_bday(birthday)

        self.at_bday(birthday)

    # cache each channel's mode
    def on_join(self, c, e):
        if e.source.nick == self.nick:
            c.mode(e.target, "")

    # basically borrowed from irc.bot.py's _on_mode(), since this library is a
    # god damn piece of shit
    def on_channelmodeis(self, c, e):
        print("debugging mode change:", e.arguments[1])
        modes = irc.modes.parse_channel_modes(e.arguments[1])
        t = e.arguments[0]
#.........这里部分代码省略.........
开发者ID:anachrome,项目名称:averybot,代码行数:103,代码来源:averybot.py

示例2: print

# 需要导入模块: from markov import Markov [as 别名]
# 或者: from markov.Markov import learn [as 别名]
hashtag = ''
hashtagmatch = re.match(r'#.+',trend)
if hashtagmatch is not None:
    hashtag = trend
print('hashtag? = ' + hashtag)

statustexts = twitter.search_statustexts(trend,300)
statustexts = [re.sub(hashtag,'',statustext) for statustext in statustexts]
statustexts = [re.sub(r'http.+(\n|\Z)','',statustext) for statustext in statustexts]
statustexts = [re.sub(r'RT.+:','',statustext) for statustext in statustexts]
statustexts = [escape.decode(statustext) for statustext in statustexts]
print(statustexts)

markov = Markov(2)
for statustext in statustexts:
    markov.learn(exmecab.wakati(statustext))

a_sentence = exmecab.wakati(random.choice(statustexts)).split(' ')
start_words = tuple(a_sentence[:2])

salad = markov.make_salad(50,start_words)

maxlen = 139 - len(hashtag)
if len(salad) > maxlen:
    salad = salad[:maxlen-1]

salad += '\n' + hashtag
print(salad)
print('len = ' + str(len(salad)))
twitter.tweet(salad)
开发者ID:haretaro,项目名称:takamayuki,代码行数:32,代码来源:takamayuki.py


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