예제 #1
0
def word_translator(words):
    b = TextBlob(words)
    if b.detect_language() == "en":
        print "The word " + words + " is in english and means",\
                b.translate(to="es")
    elif b.detect_language() == "es":
        print "La palabra " + words +\
                " esta en espanol y en ingles significa", b.translate(to="en")
    return
예제 #2
0
def word_translator(words):
    b = TextBlob(words)
    if b.detect_language() == "en":
        print "The word " + words + " is in english and means",\
                b.translate(to="es") 
    elif b.detect_language() == "es":
        print "La palabra " + words +\
                " esta en espanol y en ingles significa", b.translate(to="en")
    return
예제 #3
0
def flat_doc(document, model, extremes=None):
	flat_doc = ""
	for field in document:
		if not isinstance(document[field], list): continue #No tomamos en cuenta los campos 'id' y '_version_': auto-generados por Solr
		for value in document[field]:
			## Detección y traducción ##
			if field=='author.authors.authorName' or field=='author.authorBio' or field=='description' or field=='quotes.quoteText':
				value_blob = TextBlob(value)
				try:
					if value_blob.detect_language() != 'en':
						try: 
							value = value_blob.translate(to='en')
						except Exception as e: 
							value = value #e = NotTranslated('Translation API returned the input string unchanged.',)
				except Exception as e:
					value = value #e = TranslatorError('Must provide a string with at least 3 characters.')
			############################
			flat_doc += str(value)+' ' #Se aplana el documento en un solo string
	flat_doc = preprocess_string(flat_doc, CUSTOM_FILTERS) #Preprocesa el string
	flat_doc = [w for w in flat_doc if w not in stop_words] #Remueve stop words
	if extremes:
		flat_doc = [w for w in flat_doc if w not in extremes]
	flat_doc = [w for w in flat_doc if w in model.vocab] #Deja sólo palabras del vocabulario
	if flat_doc == []:
		flat_doc = ['book'] #Si el libro queda vacío, agregarle un token para no tener problemas más adelante
	return flat_doc
예제 #4
0
 def get_translate(self, text):
     text = text.replace("text:", "")
     blob = TextBlob(text)
     lan = blob.detect_language()
     if lan != 'en':
         sentp = blob.translate(to="en")
     else:
         sentp = blob.translate(to="fa")
     sent = self.sender.sendMessage(str(sentp))
     self._editor = telepot.helper.Editor(self.bot, sent)
     self._edit_msg_ident = telepot.message_identifier(sent)
예제 #5
0
def handle_message_event(event):
    print(event)
    text = event.message.text
    source = event.source
    id = ''
    if isinstance(source, SourceUser):
        id = source.user_id
    elif isinstance(source, SourceGroup):
        id = source.group_id
    set_send_id(id)
    blob = TextBlob(text)
    if '狀態' in text:
        text = text.replace('狀態', '')
        if text == '':
            line_bot_api.reply_message(
                event.reply_token, TextSendMessage(text=get_all_messages()))
        else:
            name = text
            line_bot_api.reply_message(event.reply_token,
                                       TextSendMessage(text=get_message(name)))
    elif '報告' in text:
        matches = re.search('(.*)報告(\d*)', text)
        if matches.group(1) == '' and matches.group(2) == '':
            line_bot_api.reply_message(event.reply_token,
                                       TextSendMessage(text=get_all_reports()))
        elif matches.group(2) == '':
            line_bot_api.reply_message(
                event.reply_token,
                TextSendMessage(text=get_report(matches.group(1))))
        else:
            line_bot_api.reply_message(
                event.reply_token,
                TextSendMessage(text=get_report_url(matches.group(1),
                                                    int(matches.group(2)))))
    elif '敬禮' in text:
        line_bot_api.reply_message(event.reply_token,
                                   TextSendMessage(text='敬禮'))
    elif '安安' in text:
        line_bot_api.reply_message(event.reply_token,
                                   TextSendMessage(text='安'))
    elif '0.0' in text:
        line_bot_api.reply_message(event.reply_token,
                                   TextSendMessage(text='0.0'))
    elif blob.detect_language() == 'ru':
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=str(blob.translate(to='zh-TW'))))
예제 #6
0
def import_text(text, title=None):
	"""
	Import a text.

	"""
	blob = TextBlob(text)
	document = Document()
	if title is None:
		first_sentence = blob.sentences[0]
		if len(first_sentence) > 50:
			chunk = first_sentence[0:47]
			last_space = chunk.rfind(" ")
			title = "{0}...".format(first_sentence[0:last_space])
	document.title = title
	document.text = text
	document.language = blob.detect_language()
	sentiment = blob.sentiment
	document.polarity = sentiment[0]
	document.subjectivity = sentiment[1]
	document.is_tagged = False
	document.save()
	return document