本文整理汇总了Python中markov.Markov.addNewSentence方法的典型用法代码示例。如果您正苦于以下问题:Python Markov.addNewSentence方法的具体用法?Python Markov.addNewSentence怎么用?Python Markov.addNewSentence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类markov.Markov
的用法示例。
在下文中一共展示了Markov.addNewSentence方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CubeBot
# 需要导入模块: from markov import Markov [as 别名]
# 或者: from markov.Markov import addNewSentence [as 别名]
#.........这里部分代码省略.........
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room, self.nick) #enable group chat
"""
some utility functions to process the message text
"""
def removeItems(self, message):
#do all the operations!
message = self.removeParens(message)
message = self.removeUsernames(message)
message = self.removeAsteriskWords(message) #could return ""
return message
def removeParens(self, message):
return [re.sub('[\(\)]', '', x) for x in message]
def removeUsernames(self, message):
#if the first word is a username (ie, ends in ':'), remove it
try:
if message[0].endswith(':'):
message.remove(message[0])
except IndexError:
#means we've got an empty message
return
return message
def removeAsteriskWords(self, message):
#users correct typos with *word or word* generally
#we don't want alfred repeating those
#TODO add a spellchecker so alfred can exhibit this behavior correctly
try:
if len(message) == 1: #one word
if ' '.join(message).find('*') != -1: #contains an asterisk
#return an empty string
return ""
else:
return message
else:
return message
except TypeError: #we have an empty message
return
def botNickInText(self, message):
#strip punctuation from message_body
regex = re.compile('[%s]' % re.escape(string.punctuation))
message_no_punct = []
for word in message:
message_no_punct.append(regex.sub('', word).lower())
return self.nick in message_no_punct or (self.nick + "s") in message_no_punct
def mosttimes(self):
return random.random() > 0.3
def sometimes(self):
return random.random() > 0.7
#parse incoming messages
def messageHandler(self, msg):
#preprocess input
human_nick = msg['mucnick'] # whoever we're responding to
response = "" #initialize response
original_message_body = msg['body'].split()
message_body = self.removeItems(original_message_body)
if message_body == []:
return
#always make sure the message we're replying to didn't come from self
if human_nick != self.nick:
if human_nick == 'viraj' and message_body == ["alfred", "exit"]:
self.cleanShutDown()
#reply if username is mentioned
if self.botNickInText(original_message_body):
response = self.markov.generateText()
self.chatty = random.randint(0, 3)
#reply if animal sounds are mentioned :)
elif any( word in animalsounds_set for word in message_body ):
response = random.choice(animalsounds)
else: #username is not mentioned
self.markov.addNewSentence(message_body)
#send finished response if it's been modified
self.sendMessage(msg, response)
def cleanShutDown(self):
logging.info("Saving cache and quitting")
self.markov.saveCache()
sys.exit()
def sendMessage(self, msg, response):
if response != "":
self.send_message(mto=msg['from'].bare, mbody=response, mtype='groupchat')
logging.info("REPLY: " + response)