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


Python Markov.saveCache方法代碼示例

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


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

示例1: CubeBot

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import saveCache [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)
開發者ID:dcw329,項目名稱:alfred,代碼行數:104,代碼來源:alfred.py


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