Ejemplo n.º 1
0
class TwitterListener(StreamListener):
    def __init__(self):
        self.cache = LFUCache(maxsize=50)
        self.cache2 = LFUCache(maxsize=50)
        Timer(interval=60, function=self.print_keys)
        Timer(interval=30, function=self.check_cached_words)

    def on_data(self, data):
        data_lst = json.loads(data)
        data_lst = data_lst.get('text', '').split()

        if self.cache.currsize == self.cache.maxsize:
            for key in list(self.cache.keys()):
                if self.cache[key] == 0:
                    del self.cache[key]

        for word in data_lst:
            if word in self.cache.keys():
                self.cache[word] += 1
            else:
                self.cache[word] = 1
            if self.cache[word] < 0:
                del self.cache[word]

        return True

    def print_keys(self):
        """
        print recent words and update the second cache every 60 seconds
        """
        print(list(self.cache.items()))
        self.cache2.update(self.cache)
        return True

    def check_cached_words(self):
        """
        Decrease score of word by 1 if the score does not change within
        60 seconds
        """
        for word in list(self.cache.keys()):
            if self.cache.get(word) == self.cache2.get(word):
                self.cache[word] -= 1
        return True
Ejemplo n.º 2
0
class WordBot():
	def __init__(self):
		self.dictionaryCache = LFUCache(maxsize = 200)
		self.urbandictionaryCache = LFUCache(maxsize = 200)
		self.wordOfTheDayCache = {}
		self.bot = telebot.TeleBot(bot_token)
		self.session = requests.Session()
		self.session.mount('http://', requests.adapters.HTTPAdapter(max_retries=5))
		self.session.mount('https://', requests.adapters.HTTPAdapter(max_retries=5))

	def handle_inline(self, query):
		try:
			query_word = query.get('query')
			default_word = self.getWordOfTheDay()
			inline_answers = []
			if default_word:
				default_result = types.InlineQueryResultArticle(
					'1', 
					'Word of the day', 
					types.InputTextMessageContent(
						'*' + default_word['word'] + '*\n' + default_word['definitions'],
						parse_mode='markdown'
					),
					description=default_word['word']
				)
				inline_answers = [default_result]
			if query_word or query_word != '':
				reply = self.make_reply('/define ' + query_word)
				desc = reply if reply == 'Word not found.' else None
				query_result = types.InlineQueryResultArticle('2', 
					query_word, 
					types.InputTextMessageContent(
						reply,
						parse_mode='markdown'
					),
					description=desc
				)
				inline_answers = [query_result]
			self.bot.answer_inline_query(query.get('id'), inline_answers)
		except Exception as e:
			pass
			#sprint(e)

	def handle_message(self, message):
		if 'new_chat_participant' in message:
			return
		query = message.get('text')
		if not query:
			return
		if '@LexicoBot' in query:
			query = query.replace('@LexicoBot', '')
		reply = self.make_reply(query)
		if reply != '':
			self.bot.send_chat_action(message['chat']['id'], 'typing')
			self.bot.send_message(message['chat']['id'], reply, parse_mode='markdown')
	   
	def make_reply(self, query):
		if query in ['/start', '/help']:
			return start_message		
		reply_message = ''
		if query == '/today':
			wordData = self.getWordOfTheDay()
			if wordData is None:
				return 'Server error.'
			query = '/define ' + wordData['word']
		query = query.split()
		if len(query) > 1:
			if query[0] in ['/define', '/synonyms', '/antonyms', '/use', '/all', '/ud']:
				word = ' '.join(query[1::])
				reply_message = '*' +  word + '*\n'
				if query[0] != '/ud':
					wordData = self.dictionaryCache.get(word)
					if wordData is None:
						wordData = self.getWord(word)
					else:
						pass
						#print 'Cache hit ' + word 
					if wordData is None:
						return 'Word not found.'
					if query[0] in ['/define','/all']:
						reply_message += wordData['definitions'] + '\n'
					if query[0] in ['/synonyms','/all']:
						reply_message += wordData['synonyms'] + '\n'
					if query[0] in ['/antonyms','/all']:
						reply_message += wordData['antonyms'] + '\n'
					if query[0] in ['/use','/all']:
						reply_message += wordData['examples'] + '\n'
				else:
					wordData = self.urbandictionaryCache.get(word)
					if wordData is None:
						wordData = self.getUrbandictionaryWord(word)
					if wordData is None:
						return 'Word not found'
					reply_message += wordData['definition'] + '\n'
					reply_message += wordData['example']	
		return reply_message
	
	def updateCache(self, word, wordData):
		dataDict = {}
		definitionText = '*Definitions*' +'\n'
		synonymsText = '*Synonyms*' + '\n'
		antonymsText = '*Antonyms*' + '\n'
		examplesText = '*Examples*' + '\n'
		definitions = self.getDefinitions(wordData)
		synonyms = self.getSynonyms(wordData)
		antonyms = self.getAntonyms(wordData)
		examples = self.getExamples(wordData)
		if not definitions:
			definitionText += 'No definitions found.\n'
		if not synonyms:
			synonymsText += 'No synonyms found.\n'
		if not antonyms:
			antonymsText += 'No antonyms found.\n'
		if not examples:
			examplesText += 'No examples found.\n'		
		for definition in self.getDefinitions(wordData):
			if definition[0]:
				definitionText += definition[0] + ': '
			definitionText += definition[1] + '\n\n'	
		definitionText = definitionText[:-1]	
		for synonym in synonyms[:3]:
			synonymsText += synonym + '\n'
		for antonym in antonyms[:3]:
			antonymsText += antonym + '\n'
		for index, example in enumerate(examples[:3]):
			examplesText += str(index+1) + '. ' + example + '\n\n'
		examplesText = examplesText[:-1]
		dataDict['word'] = word
		dataDict['definitions'] = definitionText
		dataDict['synonyms'] = synonymsText
		dataDict['antonyms'] = antonymsText
		dataDict['examples'] = examplesText
		self.dictionaryCache.update({word:dataDict})
		return dataDict
			
	def getDefinitions(self, wordData):
		partCounter = Counter()
		definitions = []
		for definition in wordData:
			if 'partOfSpeech' in definition.keys() and partCounter[definition['partOfSpeech']] < 2:
				definitions.append( 
					('_' + definition['partOfSpeech'] + '_ ', 
					definition['text'])
				)
				partCounter[definition['partOfSpeech']] += 1
			else:
				definitions.append(('',definition['text']))
		return definitions

	def getSynonyms(self, wordData):
		synonyms = []
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType'] == 'synonym':
				for synonym in relatedWords['words']:
					synonyms.append(synonym)
		
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType']	 == 'cross-reference':
				for synonym in relatedWords['words']:
					synonyms.append(synonym)	
		return synonyms

	def getAntonyms(self, wordData):
		antonyms = []
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType']	 == 'antonym':
				for antonym in relatedWords['words']:
					antonyms.append(antonym)
		return antonyms

	def getExamples(self, wordData):
		examples = []
		for index,example in enumerate(wordData[0]['examples']):
			examples.append(example['text'].replace('\n',''))
		return examples
		
	def getEtymology(self, wordData):
		etymologies = []
		for etymology in wordData[0]['etymologies']:
			etymologies.append(etymology)
		return etymology

	def getWord(self, word):
		def_url = wordnik_url + word + '/definitions?limit=15&api_key=' + wordnik_api_key
		example_url = wordnik_url + word + '/examples?api_key=' + wordnik_api_key
		related_url = wordnik_url + word + '/relatedWords?api_key=' + wordnik_api_key
		urls = [def_url, example_url, related_url]
		data = []
		for url in urls:
			try:
				response = self.session.get(url, verify=False)
				if response.status_code != 200:
					return None
				data.append(json.loads(response.text.encode('utf-8')))
			except ValueError:
				return None
		if not data[0]:
			return None
		wordData = data[0]
		try:
			wordData[0]['examples'] = data[1]['examples']
		except KeyError:
			wordData[0]['examples'] = []
		try:
			wordData[0]['relatedWords'] = data[2]
		except KeyError:
			wordData[0]['relatedWords'] = []
		return self.updateCache(word,wordData)
	
	def getUrbandictionaryWord(self, word):
		api_url = 'http://api.urbandictionary.com/v0/define?term='
		#response = requests.get(api_url+word, verify=False)
		response = urllib2.urlopen(api_url + word)
		data = json.loads(response.read().decode('utf-8'))
		if data['result_type'] == 'no_results' or not data['list']:
			return None
		wordData = {}
		wordData['definition'] = '*Definition*' + '\n'
		wordData['example'] = '*Example*'  + '\n'
		try:
			if data['list'][0]['definition']:
				wordData['definition'] += data['list'][0]['definition'].strip() + '\n'
			else:
				return None
		except KeyError:
			return None
		try:
			if data['list'][0]['example']:
				wordData['example'] += data['list'][0]['example'].strip() + '\n'
			else:
				wordData['example'] += 'No example found.'
		except KeyError:
			wordData['example'] += 'No example found.'			
		self.urbandictionaryCache.update({word:wordData})
		return wordData
	
	def getWordOfTheDay(self):
		wordOfTheDay = self.wordOfTheDayCache.get(datetime.now().day, None)
		if wordOfTheDay is None:
			today = datetime.strftime(datetime.now(), '%Y-%m-%d')
			url = wordnik_url[:-10] + 'words.json/wordOfTheDay?api_key=' + wordnik_api_key + '&date=' + today
			data = []
			response = self.session.get(url, verify=False)
			try:
				data.append(json.loads(response.text.encode('utf-8')))
			except ValueError:
				return None
			wordOfTheDay = data[0]['word']
			self.wordOfTheDayCache.clear()
			self.wordOfTheDayCache[datetime.now().day] = wordOfTheDay
		else:
			pass
			#print 'Today Cache Hit ' + wordOfTheDay
		wordData = self.dictionaryCache.get(wordOfTheDay)
		if not wordData:
			url = wordnik_url + wordOfTheDay + '/relatedWords?api_key=' + wordnik_api_key
			wordData = [definition for definition in data[0]['definitions']]
			for definition in wordData:
				definition['attributionText'] = ''
			wordData[0]['examples'] = data[0]['examples']
			response = self.session.get(url, verify = False)
			relatedWords = json.loads(response.text)
			wordData[0]['relatedWords'] = relatedWords
			wordData[0]['word'] = wordOfTheDay
			return self.updateCache(wordOfTheDay, wordData)	
		else:
			#print 'Cache hit ' + wordOfTheDay
			return wordData	
Ejemplo n.º 3
0
class CoapLFUCache(CoapCache):
    def __init__(self, max_dim):
        """

        :param max_dim:
        """
        print "Using LFU Cache with dimension : " + str(max_dim)
        self.cache = LFUCache(maxsize=max_dim)

    def update(self, key, element):
        """

        :param key:
        :param element:
        :return:
        """
        print "updating cache"
        print "key: ", key.hashkey
        print "element: ", element
        self.cache.update([(key.hashkey, element)])

    def get(self, key):
        """

        :param key:
        :return: CacheElement
        """
        try:
            response = self.cache[key.hashkey]
        except KeyError:
            print "problem here"
            response = None
        return response

    def is_full(self):
        """
        :return:
        """
        if self.cache.currsize == self.cache.maxsize:
            return True
        return False

    def is_empty(self):
        """

        :return:
        """

        if self.cache.currsize == 0:
            return True
        return False

    def debug_print(self):
        """

        :return:
        """
        print "size = ", self.cache.currsize
        list = self.cache.items()
        for key, element in list:
            print "element.max age ", element.max_age
            print "element.uri", element.uri
            print "element.freshness ", element.freshness
Ejemplo n.º 4
0
class Dictionary(object):
    def __init__(self):
        self.word_api = WordApi.WordApi(
            swagger.ApiClient(wordnik_api_key, wordnik_api))
        self.wordoftheday_api = WordsApi.WordsApi(
            swagger.ApiClient(wordnik_api_key, wordnik_api))
        self.urbandictionary_api = urbandictionary_api
        self.dictionaryCache = LFUCache(maxsize=1000)
        self.urbandictionaryCache = LFUCache(maxsize=1000)
        self.wordOfTheDayCache = {}
        self.session = requests.Session()
        self.session.mount('http://',
                           requests.adapters.HTTPAdapter(max_retries=5))
        self.session.mount('https://',
                           requests.adapters.HTTPAdapter(max_retries=5))

    def part_of_speech_filter(self, counter, pos):
        counter[pos] += 1
        return counter[pos] <= 2

    def is_valid_definition(self, definition):
        return definition.partOfSpeech and definition.text

    def get_word(self, word):
        if word in self.dictionaryCache: return self.dictionaryCache[word]
        definitions = self.word_api.getDefinitions(word,
                                                   limit=20,
                                                   useCanonical=True)
        if not definitions: return None
        counter = Counter()
        definitions = list(
            filter(
                lambda d: self.part_of_speech_filter(counter, d.partOfSpeech)
                and self.is_valid_definition(d), definitions))
        examples = self.word_api.getExamples(word, limit=5, useCanonical=True)
        relatedWords = self.word_api.getRelatedWords(
            word, limitPerRelationshipType=5, useCanonical=True)
        if not relatedWords: relatedWords = []
        synonyms = [x for x in relatedWords if x.relationshipType == 'synonym']
        antonyms = [x for x in relatedWords if x.relationshipType == 'antonym']
        word_dict = {
            'word': word,
            'definitions': definitions,
            'example': sorted(examples.examples, key=lambda x: len(x.text))[0],
            'synonyms': synonyms[0].words if synonyms else [],
            'antonyms': antonyms[0].words if antonyms else []
        }
        self.dictionaryCache.update({word: word_dict})
        return word_dict

    def get_word_of_the_day(self):
        wordOfTheDay = self.wordOfTheDayCache.get(datetime.now().day, None)
        if wordOfTheDay and wordOfTheDay in self.dictionaryCache:
            return self.dictionaryCache[wordOfTheDay]
        wordOfTheDay = self.wordoftheday_api.getWordOfTheDay()
        try:
            relatedWords = self.word_api.getRelatedWords(
                wordOfTheDay.word,
                limitPerRelationshipType=5,
                useCanonical=True)
        except Exception:
            relatedWords = []
        synonyms = [
            x for x in relatedWords if x.relationshipType == 'synonym'
        ] if relatedWords else []
        antonyms = [
            x for x in relatedWords if x.relationshipType == 'antonym'
        ] if relatedWords else []
        word_dict = {
            'word': wordOfTheDay.word,
            'definitions': wordOfTheDay.definitions,
            'example': sorted(wordOfTheDay.examples,
                              key=lambda x: len(x.text))[0],
            'synonyms': synonyms,
            'antonyms': antonyms
        }
        self.dictionaryCache.update({wordOfTheDay.word: word_dict})
        self.wordOfTheDayCache.clear()
        self.wordOfTheDayCache[datetime.now().day] = wordOfTheDay.word
        return word_dict

    def get_urbandictionary_word(self, word):
        if word in self.urbandictionaryCache:
            return self.urbandictionaryCache[word]
        url = '{}/define'.format(self.urbandictionary_api)
        url_params = {'term': word}
        res = self.session.get(url, params=url_params)
        json_res = json.loads(res.text)
        if not json_res['list']: return None
        word_dict = {
            'word': word,
            'definition': json_res['list'][0]['definition'],
            'example': json_res['list'][0]['example']
        }
        self.urbandictionaryCache.update({word: word_dict})
        return word_dict
Ejemplo n.º 5
0
class WordBot():

	def __init__(self):
		self.offset = ''
		self.URL = 'https://api.telegram.org/bot' + bot_token
		self.session = requests.Session()
		self.session.mount("http://", requests.adapters.HTTPAdapter(max_retries=2))
		self.session.mount("https://", requests.adapters.HTTPAdapter(max_retries=2))
		self.cache = LFUCache(maxsize = 200)
		self.startMessage = start_message
		self.keyboard = {"keyboard":[["/define"],["/synonyms"],["/antonyms"],["/examples"],["/all"]]}

	def processUpdates(self):
		response = self.session.get(self.URL + '/getUpdates?offset=' + str(self.offset),verify=False)
		print "Got update"
		status = False
		updates = json.loads(response.text)
		if updates['result']:
			self.offset = updates['result'][0]['update_id'] + 1
			query = updates['result'][0]['message']['text']
			chat_id = updates['result'][0]['message']['chat']['id']
			self.session.get(self.URL + '/sendChatAction?chat_id=' + str(chat_id) +'&action=typing',verify=False)
			message = self.makeMessage(query)			
			status = self.sendMessage(message,chat_id)
		return status
	
	def makeMessage(self,query):
		message = self.startMessage
		if query == '/stop':
			message = 'Bot disabled.'
		elif query == '/today':
			wordData = self.getWordOfTheDay()
			query = '/define ' + wordData['word']
		query = query.split()
		if len(query) > 1:
			if query[0] not in ['/define','/synonyms','/antonyms','/use','/all']:
				return self.startMessage
			word = ' '.join(query[1::]).lower()
			message = 'Word: ' +  word + '\n'
			message += '=' * (len(word) + 7) + '\n'
			if self.cache.get(word):
				print "Fetching from cache"
				wordData = self.cache.get(word)
			else:
				wordData = self.getWord(word)
				if wordData is None:
					return 'Word not found.'
			if query[0] in ['/define','/all']:
				message += wordData['definitions'] + '\n'
			if query[0] in ['/synonyms','/all']:
				message += wordData['synonyms'] + '\n'
			if query[0] in ['/antonyms','/all']:
				message += wordData['antonyms'] + '\n'
			if query[0] in ['/use','/all']:
				message += wordData['examples'] + '\n'	
		return message
	
	def updateCache(self,word,wordData):
		dataDict = {}
		definitionText = 'Definitions :-' + '\n'
		definitionText += '-' * 20 + '\n'
		synonymsText = 'Synonyms :-' + '\n'
		synonymsText += '-' * 20 + '\n'
		antonymsText = 'Antonyms :-' + '\n'
		antonymsText += '-' * 20 + '\n'
		examplesText = 'Exmaples :-' + '\n'
		examplesText += '-' * 20 + '\n'
		definitions = self.getDefinitions(wordData)
		synonyms = self.getSynonyms(wordData)
		antonyms = self.getAntonyms(wordData)
		examples = self.getExamples(wordData)
		if not definitions:
			definitionText += 'No definitions found.\n'
		if not synonyms:
			synonymsText += 'No synonyms found.\n'
		if not antonyms:
			antonymsText += 'No antonyms found.\n'
		if not examples:
			examplesText += 'No examples found.\n'
			
		for definition in self.getDefinitions(wordData):
			definitionText += definition[0] + '\n' + definition[1] + ': ' +  definition[2] + '\n\n'
		for synonym in synonyms[:5]:
			synonymsText += synonym + '\n'
		for antonym in antonyms[:5]:
			antonymsText += antonym + '\n'
		for index,example in enumerate(examples[:5]):
			examplesText += str(index+1) + ". " + example + '\n\n'
		
		dataDict['word'] = word
		dataDict['definitions'] = definitionText
		dataDict['synonyms'] = synonymsText
		dataDict['antonyms'] = antonymsText
		dataDict['examples'] = examplesText
		self.cache.update({word:dataDict})
		return dataDict 
			
	def getDefinitions(self,wordData):
		partCounter = Counter()
		definitions = []
		for definition in wordData:
			if partCounter[definition['partOfSpeech']] < 2:
				definitions.append((definition['attributionText'],definition['partOfSpeech'],definition['text']))
				partCounter[definition['partOfSpeech']] += 1
		return definitions


	def getSynonyms(self,wordData):
		synonyms = []
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType'] == 'synonym':
				for synonym in relatedWords['words']:
					synonyms.append(synonym)
		
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType']  == 'cross-reference':
				for synonym in relatedWords['words']:
					synonyms.append(synonym)
		
		return synonyms

	def getAntonyms(self,wordData):
		antonyms = []
		for relatedWords in wordData[0]['relatedWords']:
			if relatedWords['relationshipType']  == 'antonym':
				for antonym in relatedWords['words']:
					antonyms.append(antonym)
		return antonyms

	def getExamples(self,wordData):
		examples = []
		for index,example in enumerate(wordData[0]['examples']):
			examples.append(example['text'].replace('\n',''))
		return examples
		
	def getEtymology(self,wordData):
		etymologies = []
		for etymology in wordData[0]['etymologies']:
			etymologies.append(etymology)
		return etymology

	def getWord(self,word):
		url1 = wordnik_url + word + '/definitions?api_key=' + wordnik_api_key
		url2 = wordnik_url + word + '/examples?api_key=' + wordnik_api_key
		url3 = wordnik_url + word + '/relatedWords?api_key=' + wordnik_api_key
		urls = [url1,url2,url3]
		data = []
		for url in urls:
			try:
				response = self.session.get(url,verify=False)
				data.append(json.loads(response.text.encode('utf-8')))
			except ValueError:
				return None
		if not data[0]:
			return None
		wordData = data[0]
		try:
			wordData[0]['examples'] = data[1]['examples']
		except KeyError:
			wordData[0]['examples'] = []
		try:
			wordData[0]['relatedWords'] = data[2]
		except KeyError:
			wordData[0]['relatedWords'] = []
		return self.updateCache(word,wordData)
	
	def getWordOfTheDay(self):
		today = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
		url = wordnik_url[:-10] + 'words.json/wordOfTheDay?api_key=' + wordnik_api_key + '&date=' + today
		wordData = []
		data = []
		response = self.session.get(url,verify = False)
		data.append(json.loads(response.text.encode('utf-8')))
		word = data[0]['word']
		if self.cache.get(word):
			wordData = self.cache.get(word)
			return wordData
		url = wordnik_url + word + '/relatedWords?api_key=' + wordnik_api_key
		wordData = [definition for definition in data[0]['definitions']]
		for definition in wordData:
			definition['attributionText'] = ''
		wordData[0]['examples'] = data[0]['examples']
		response = self.session.get(url,verify = False)
		relatedWords = json.loads(response.text)
		wordData[0]['relatedWords'] = relatedWords
		wordData[0]['word'] = word
		return self.updateCache(word,wordData)		
			
	def sendMessage(self,message,chat_id):
		dataDict = {'chat_id':str(chat_id),
				'text':message.encode('utf-8'),
				'reply_markup':self.keyboard}
		response = self.session.post(self.URL + '/sendMessage',data = dataDict)
		if response.status_code == 200:
			return True
		else:
			return False
Ejemplo n.º 6
0
class Dictionary(object):
    def __init__(self):
        self.dictionaryCache = LFUCache(maxsize=1000)
        self.urbandictionaryCache = LFUCache(maxsize=1000)
        self.wordOfTheDayCache = {}
        self.session = requests.Session()
        self.session.mount('http://',
                           requests.adapters.HTTPAdapter(max_retries=5))
        self.session.mount('https://',
                           requests.adapters.HTTPAdapter(max_retries=5))

    def updateCache(self, word, wordData):
        dataDict = {}
        definitionText = '*Definitions*' + '\n'
        synonymsText = '*Synonyms*' + '\n'
        antonymsText = '*Antonyms*' + '\n'
        examplesText = '*Examples*' + '\n'
        definitions = self.getDefinitions(wordData)
        synonyms = self.getSynonyms(wordData)
        antonyms = self.getAntonyms(wordData)
        examples = self.getExamples(wordData)
        if not definitions:
            definitionText += 'No definitions found.\n'
        if not synonyms:
            synonymsText += 'No synonyms found.\n'
        if not antonyms:
            antonymsText += 'No antonyms found.\n'
        if not examples:
            examplesText += 'No examples found.\n'
        for definition in definitions:
            if definition[0]:
                definitionText += definition[0] + ': '
            definitionText += definition[1] + '\n\n'
        definitionText = definitionText[:-1]
        for synonym in synonyms[:3]:
            synonymsText += synonym + '\n'
        for antonym in antonyms[:3]:
            antonymsText += antonym + '\n'
        for index, example in enumerate(examples[:3]):
            examplesText += str(index + 1) + '. ' + example + '\n\n'
        examplesText = examplesText[:-1]
        dataDict['word'] = word
        dataDict['definitions'] = definitionText
        dataDict['synonyms'] = synonymsText
        dataDict['antonyms'] = antonymsText
        dataDict['examples'] = examplesText
        self.dictionaryCache.update({word: dataDict})
        return dataDict

    def getDefinitions(self, wordData):
        partCounter = Counter()
        definitions = []
        for definition in wordData:
            if 'partOfSpeech' in definition.keys(
            ) and partCounter[definition['partOfSpeech']] < 2:
                definitions.append(('_' + definition['partOfSpeech'] + '_ ',
                                    definition['text']))
                partCounter[definition['partOfSpeech']] += 1
            else:
                definitions.append(('', definition['text']))
        return definitions

    def getSynonyms(self, wordData):
        synonyms = []
        for relatedWords in wordData[0]['relatedWords']:
            if relatedWords['relationshipType'] == 'synonym':
                for synonym in relatedWords['words']:
                    synonyms.append(synonym)

        for relatedWords in wordData[0]['relatedWords']:
            if relatedWords['relationshipType'] == 'cross-reference':
                for synonym in relatedWords['words']:
                    synonyms.append(synonym)
        return synonyms

    def getAntonyms(self, wordData):
        antonyms = []
        for relatedWords in wordData[0]['relatedWords']:
            if relatedWords['relationshipType'] == 'antonym':
                for antonym in relatedWords['words']:
                    antonyms.append(antonym)
        return antonyms

    def getExamples(self, wordData):
        examples = []
        for index, example in enumerate(wordData[0]['examples']):
            examples.append(example['text'].replace('\n', ''))
        return examples

    def getEtymology(self, wordData):
        etymologies = []
        for etymology in wordData[0]['etymologies']:
            etymologies.append(etymology)
        return etymology

    def getWord(self, word):
        if '#' in word:
            return None
        def_url = wordnik_url + word + '/definitions?limit=15&api_key=' + wordnik_api_key
        example_url = wordnik_url + word + '/examples?api_key=' + wordnik_api_key
        related_url = wordnik_url + word + '/relatedWords?api_key=' + wordnik_api_key
        urls = [def_url, example_url, related_url]
        data = []
        for url in urls:
            try:
                response = self.session.get(url, verify=False)
                if response.status_code != 200:
                    return None
                data.append(json.loads(response.text.encode('utf-8')))
            except ValueError:
                return None
        if not data[0]:
            return None
        wordData = data[0]
        try:
            wordData[0]['examples'] = data[1]['examples']
        except KeyError:
            wordData[0]['examples'] = []
        try:
            wordData[0]['relatedWords'] = data[2]
        except KeyError:
            wordData[0]['relatedWords'] = []
        return self.updateCache(word, wordData)

    def getUrbandictionaryWord(self, word):
        api_url = 'http://api.urbandictionary.com/v0/define?term='
        #response = requests.get(api_url+word, verify=False)
        response = urllib2.urlopen(api_url + word)
        data = json.loads(response.read().decode('utf-8'))
        if data['result_type'] == 'no_results' or not data['list']:
            return None
        wordData = {}
        wordData['definition'] = '*Definition*' + '\n'
        wordData['example'] = '*Example*' + '\n'
        try:
            if data['list'][0]['definition']:
                wordData['definition'] += data['list'][0]['definition'].strip(
                ) + '\n'
            else:
                return None
        except KeyError:
            return None
        try:
            if data['list'][0]['example']:
                wordData['example'] += data['list'][0]['example'].strip(
                ) + '\n'
            else:
                wordData['example'] += 'No example found.'
        except KeyError:
            wordData['example'] += 'No example found.'
        self.urbandictionaryCache.update({word: wordData})
        return wordData

    def getWordOfTheDay(self):
        wordOfTheDay = self.wordOfTheDayCache.get(datetime.now().day, None)
        if wordOfTheDay is None:
            today = datetime.strftime(datetime.now(), '%Y-%m-%d')
            url = wordnik_url[:
                              -10] + 'words.json/wordOfTheDay?api_key=' + wordnik_api_key + '&date=' + today
            data = []
            response = self.session.get(url, verify=False)
            try:
                data.append(json.loads(response.text.encode('utf-8')))
            except ValueError:
                return None
            wordOfTheDay = data[0]['word']
            self.wordOfTheDayCache.clear()
            self.wordOfTheDayCache[datetime.now().day] = wordOfTheDay
        else:
            pass
        wordData = self.dictionaryCache.get(wordOfTheDay)
        if not wordData:
            url = wordnik_url + wordOfTheDay + '/relatedWords?api_key=' + wordnik_api_key
            wordData = [definition for definition in data[0]['definitions']]
            for definition in wordData:
                definition['attributionText'] = ''
            wordData[0]['examples'] = data[0]['examples']
            response = self.session.get(url, verify=False)
            relatedWords = json.loads(response.text)
            wordData[0]['relatedWords'] = relatedWords
            wordData[0]['word'] = wordOfTheDay
            return self.updateCache(wordOfTheDay, wordData)
        else:
            return wordData