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


Python Markov.generateText方法代码示例

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


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

示例1: CubeBot

# 需要导入模块: from markov import Markov [as 别名]
# 或者: from markov.Markov import generateText [as 别名]
class CubeBot (sleekxmpp.ClientXMPP):

	def __init__ (self, jid, password, room, nick, fileName):
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
		self.room = room
		self.nick = nick

		#initialize markov chain with input file
		inputFile = open(fileName)
		self.markov = Markov(inputFile)

		self.add_event_handler("session_start", self.start)
		self.add_event_handler("groupchat_message", self.messageHandler)

	#begin receiving/responding
	def start(self, event):
		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)
#.........这里部分代码省略.........
开发者ID:dcw329,项目名称:alfred,代码行数:103,代码来源:alfred.py


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