Python textblob.TextBlob() Examples

The following are 30 code examples of textblob.TextBlob(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module textblob , or try the search function .
Example #1
Source File: preprocess_ngrams.py    From AREL with MIT License 6 votes vote down vote up
def build_dict(params):
    story_line = json.load(open(params['input_json'], 'r'))
    wtoi = story_line['words2id']

    count = 0
    refs_words = []
    for stories in story_line['album2stories'][params['split']].values():
        ref_words = []
        for story_id in stories:
            txt = story_line[params['split']][story_id]['origin_text']
            tmp_tokens = TextBlob(txt).tokens + ['<EOS>']
            tmp_tokens = [_ if _ in wtoi else '<UNK>' for _ in tmp_tokens]
            ref_words.append(' '.join(tmp_tokens))
        refs_words.append(ref_words)
        count += 1
    print('total albums: ', count)

    ngram_words = compute_doc_freq(create_crefs(refs_words))

    return ngram_words, count 
Example #2
Source File: deploy.py    From Election-Meddling with MIT License 6 votes vote down vote up
def analize_sentiment(tweet):
    '''
    Utility function to classify the polarity of a tweet
    using textblob.
    '''

    try:
       analysis = textblob.TextBlob(data_preparation(tweet))
 
    except:
       analysis = textblob.TextBlob(tweet)

    if analysis.sentiment.polarity > 0:
        return 1, int(analysis.sentiment.subjectivity)

    elif analysis.sentiment.polarity == 0:
        return 0, int(analysis.sentiment.subjectivity)
    else:
        return -1, int(analysis.sentiment.subjectivity) 
Example #3
Source File: DataHolder.py    From MyTwitterBot with MIT License 6 votes vote down vote up
def read_line_eos_noums(self,
                            path):
        """
        Generator.
        Similar as the function read_line_eos from
        the text_mani module. The only diference here
        is that we keep track of all the noums.

        :type path: str
        """
        for line in open(path):
            if len(list(self.all_noums)) <= self.max_noums:
                blob = TextBlob(line)
                noums = set(blob.noun_phrases)
                self.all_noums = self.all_noums.union(noums)
            for word in line.split():
                yield word
            yield '<eos>' 
Example #4
Source File: sentimark.py    From python-qutescript with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def generate_html(paragraphs, title_text):
    doc = dominate.document(title='Summary: {}'.format(title_text))

    with doc.head:
        style("""\
            body {
                background-color: #F9F8F1;
                color: #2C232A;
                font-family: sans-serif;
                font-size: 1.2em;
            }

        """)

    with doc:
        div(id='header').add(h1(title_text))
        with div():
            attr(cls='body')
            for para in paragraphs:
                tb = TextBlob(para)
                with p():
                    for sentence in tb.sentences:
                        span(sentence, style="color: {}".format(get_polarity_color(sentence.polarity)))
    return doc 
Example #5
Source File: rhodiola.py    From rhodiola with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def find_noun_phrases(string):
    noun_counts = {}
    try:
        blob = TextBlob(string.decode('utf-8'))
    except:
        print "Error occured"
        return None 
    if blob.detect_language() != "en":
        print "Tweets are not in English"
        sys.exit(1)
    else:   
        for noun in blob.noun_phrases:
            if noun in stopwords.words('english') or noun in extra_stopwords or noun == '' or len(noun) < 3:
                pass
            else:   
                noun_counts[noun.lower()] = blob.words.count(noun)
    sorted_noun_counts = sorted(noun_counts.items(), key=operator.itemgetter(1),reverse=True)
    return sorted_noun_counts[0:15] 
Example #6
Source File: tlp.py    From tlp with MIT License 6 votes vote down vote up
def __init__(self, raw_text=None, text_title=None):

        try:
            # props for internal use
            self._raw_text = raw_text
            self._text_title = text_title

            # props to store data
            self._summary = str()
            self._keywords = set()
            self._iocs = dict()
            self._tlp = None
            self._debug = dict({'iocs': dict(), 'keywords': dict()})

            if self._raw_text != None:
                if not type(self._raw_text) is unicode:
                    self._raw_text = self._raw_text.decode('utf8')
                self._tlpfilter = TLPFilter()
                self._clean_text = self._tlpfilter.text(self._raw_text)
                self._blob = TextBlob(self._raw_text)
                self._clean_blob = TextBlob(self._clean_text)

        except Exception as e:
            import traceback
            traceback.print_exc() 
Example #7
Source File: _DEPRECATED_syncrooms_autotranslate.py    From hangoutsbot with GNU Affero General Public License v3.0 6 votes vote down vote up
def _translate_message(bot, broadcast_list, context):
    if context and "autotranslate" in context:
        _autotranslate = context["autotranslate"]
        origin_language = _get_room_language(bot, _autotranslate["conv_id"])
        for send in broadcast_list:
            target_conversation_id = send[0]
            response = send[1]
            target_language = _get_room_language(bot, target_conversation_id)
            if origin_language != target_language:
                logger.debug("translating {} to {}".format(origin_language, target_language))
                translated = _autotranslate["event_text"]
                try:
                    en_blob = TextBlob(_autotranslate["event_text"])
                    translated = "{0}".format(en_blob.translate(to=target_language))
                    #translated = gs.translate(_autotranslate["event_text"], target_language
                except Exception:
                    logger.debug("Translation Api returned string unchanged")
                else:
                    pass
                finally:
                    if _autotranslate["event_text"] != translated:
                    # mutate the original response by reference
                        response.extend([
                            hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
                            hangups.ChatMessageSegment('(' + translated + ')')]) 
Example #8
Source File: recsys_tutorial.py    From snorkel-tutorials with Apache License 2.0 6 votes vote down vote up
def stars_in_review(x, low_rating_strs, high_rating_strs):
    if not isinstance(x.review_text, str):
        return ABSTAIN
    for low_rating_str in low_rating_strs:
        if low_rating_str in x.review_text.lower():
            return NEGATIVE
    for high_rating_str in high_rating_strs:
        if high_rating_str in x.review_text.lower():
            return POSITIVE
    return ABSTAIN


# %% [markdown]
# We can also run [TextBlob](https://textblob.readthedocs.io/en/dev/index.html), a tool that provides a pretrained sentiment analyzer, on the reviews, and use its polarity and subjectivity scores to estimate the user's rating for the book.
# As usual, these thresholds were picked by analyzing the score distributions and running error analysis.

# %% 
Example #9
Source File: sentiment_score.py    From driverlessai-recipes with Apache License 2.0 6 votes vote down vote up
def create_data(X: dt.Frame = None) -> Union[str, List[str],
                                                 dt.Frame, List[dt.Frame],
                                                 np.ndarray, List[np.ndarray],
                                                 pd.DataFrame, List[pd.DataFrame]]:
        # exit gracefully if method is called as a data upload rather than data modify
        if X is None:
            return []
        import os
        from h2oaicore.systemutils import config
        from textblob import TextBlob

        X = dt.Frame(X).to_pandas()
        for text_colname in text_colnames:
            X["sentiment_dai_" + text_colname] = X[text_colname].astype(str).fillna("NA").apply(
                lambda x: TextBlob(x).sentiment[0])

        temp_path = os.path.join(config.data_directory, config.contrib_relative_directory)
        os.makedirs(temp_path, exist_ok=True)

        # Save files to disk
        file_train = os.path.join(temp_path, output_dataset_name + ".csv")
        X.to_csv(file_train, index=False)

        return [file_train] 
Example #10
Source File: main.py    From mcafee2cash with MIT License 6 votes vote down vote up
def get_sentiment_analysis(text, coins):
	"""Return the sentiment analysis of coins mentioned in text in
	the form of a dictionary that aggregates the sentiment of
	sentences that include each of the coins.
	"""
	sentiment = {}
	blob = TextBlob(text)
	for sentence in blob.sentences:
		lowercase_words = [x.lower() for x in sentence.words]
		for coin in coins:
			if coin[0].lower() in lowercase_words or coin[1].lower() in lowercase_words:
				try:
					sentiment[coin] += sentence.sentiment.polarity
				except:
					sentiment[coin] = sentence.sentiment.polarity
	
	return sentiment, blob.sentiment.polarity 
Example #11
Source File: answer_beautifier.py    From ReuBERT with MIT License 5 votes vote down vote up
def _is_positive(self, sentence):
        sentiment = TextBlob(sentence).sentiment.polarity
        return sentiment 
Example #12
Source File: sentiment_analysis.py    From AirBnbPricePrediction with MIT License 5 votes vote down vote up
def calculate_sentiment(entry):
    if (type(entry) != str and math.isnan(entry)):
        return -55
    opinion = TextBlob(entry)
    return opinion.sentiment.polarity 
Example #13
Source File: Clean.py    From ClusType with GNU General Public License v3.0 5 votes vote down vote up
def clean_and_tag(self):
        with open('Intermediate/full_sentences.txt', 'w') as f,\
                open('Intermediate/full_pos.txt','w') as g,\
                open('Intermediate/sentences.txt', 'w') as m,\
                open('Intermediate/pos.txt', 'w') as n:
            for i in xrange(len(self.Documents)):
                if i%10000 == 0 and i!=0:
                    print str(i)+" documents processed."
                doc = self.Documents[i]
                cleaned_doc = self.remove_things(doc)
                blob = TextBlob(cleaned_doc)
                for j in xrange(len(blob.sentences)):
                    sent = blob.sentences[j]
                    sent = self.replace_nums(sent)
                    split_sentence = self.P.split(sent)

                    for k in xrange(len(split_sentence)):
                        frag = split_sentence[k]
                        sent_blob = TextBlob(frag, pos_tagger=self.tagger)
                        words, pos = [],[]
                        for word,tag in sent_blob.pos_tags:
                            words.append(word)
                            pos.append(tag)
                        f.write(str(i)+":"+str(j)+":"+str(k)+":"+(" ".join(words)+"\n"))
                        g.write(" ".join(pos)+"\n")
                        no_stop_words, no_stop_pos = self.remove_stopwords(words,pos)
                        m.write(str(i)+":"+str(j)+":"+str(k)+":"+(" ".join(no_stop_words)+"\n"))
                        n.write(" ".join(no_stop_pos)+"\n") 
Example #14
Source File: answer_beautifier.py    From ReuBERT with MIT License 5 votes vote down vote up
def _is_subjective(self, sentence):
        subjectivity = TextBlob(sentence).sentiment.subjectivity
        return subjectivity > 0.0 
Example #15
Source File: extend_dataset.py    From toxic with MIT License 5 votes vote down vote up
def translate(comment, language):
    if hasattr(comment, "decode"):
        comment = comment.decode("utf-8")

    text = TextBlob(comment)
    try:
        text = text.translate(to=language)
        text = text.translate(to="en")
    except NotTranslated:
        pass

    return str(text) 
Example #16
Source File: classifiers.py    From fine-grained-sentiment with MIT License 5 votes vote down vote up
def score(self, text: str) -> float:
        # pip install textblob
        from textblob import TextBlob
        return TextBlob(text).sentiment.polarity 
Example #17
Source File: explainer.py    From fine-grained-sentiment with MIT License 5 votes vote down vote up
def score(self, text: str) -> float:
        # pip install textblob
        from textblob import TextBlob
        return TextBlob(text).sentiment.polarity 
Example #18
Source File: Sentiment_Analysis_Crypto_Historic.py    From Cryptocurrency-Trading-Bots-Python-Beginner-Advance with MIT License 5 votes vote down vote up
def get_tweet_sentiment(self, tweet):
        '''
        Utility function to classify sentiment of passed tweet
        using textblob's sentiment method
        '''
        # create TextBlob object of passed tweet text
        analysis = TextBlob(self.clean_tweet(tweet))
        # set sentiment
        if analysis.sentiment.polarity > 0:
            return 'positive'
        elif analysis.sentiment.polarity == 0:
            return 'neutral'
        else:
            return 'negative' 
Example #19
Source File: taggers.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def tag(self, text):
        """Tag a string or BaseBlob."""
        if isinstance(text, textblob.compat.text_type):
            text = tb.TextBlob(text)

        return nltk.tag.pos_tag(text.tokens) 
Example #20
Source File: text_blob.py    From threatconnect-playbooks with Apache License 2.0 5 votes vote down vote up
def main():
    """."""
    args = parse_arguments()
    # read the string from the playbook to get the actual value of the argument
    string = tcex.playbook.read(args.string)
    n_gram_number = int(tcex.playbook.read(args.n_gram))

    tcex.log.info('String value: {}'.format(string))
    tcex.log.info('n-gram number: {}'.format(n_gram_number))

    blob = TextBlob(string)

    tags = dict()
    for tag in blob.tags:
        tags[tag[0]] = tag[1]

    tcex.playbook.create_output('json', blob.json)
    tcex.playbook.create_output('nGrams', [str(n_gram) for n_gram in blob.ngrams(n=n_gram_number)])
    tcex.playbook.create_output('nounPhrases', blob.noun_phrases)
    tcex.playbook.create_output('npCounts', blob.np_counts[1])
    tcex.playbook.create_output('polarity', blob.polarity)
    tcex.playbook.create_output('sentences', [str(sentence) for sentence in blob.sentences])
    tcex.playbook.create_output('subjectivity', blob.subjectivity)
    tcex.playbook.create_output('tags', tags)
    tcex.playbook.create_output('tokens', blob.tokens)
    tcex.playbook.create_output('wordCounts', blob.word_counts[1])
    tcex.playbook.create_output('words', blob.words)

    tcex.exit(0) 
Example #21
Source File: bot.py    From telegram-moderator with MIT License 5 votes vote down vote up
def log_message(self, user_id, user_message, chat_id):

        if user_message is None:
            user_message = "[NO MESSAGE]"

        try:
            s = session()
            language_code = english_message = ""
            polarity = subjectivity = 0.0
            try:
                # translate to English & log the original language
                translator = Translator()
                translated = translator.translate(user_message)
                language_code = translated.src
                english_message = translated.text
                # run basic sentiment analysis on the translated English string
                analysis = TextBlob(english_message)
                polarity = analysis.sentiment.polarity
                subjectivity = analysis.sentiment.subjectivity
            except Exception as e:
                print("Error translating message: {}".format(e))
            msg1 = Message(user_id=user_id, message=user_message, chat_id=chat_id, 
                language_code=language_code, english_message=english_message, polarity=polarity,
                subjectivity=subjectivity)
            s.add(msg1)
            s.commit()
            s.close()
        except Exception as e:
            print("Error logging message: {}".format(e))
            print(traceback.format_exc()) 
Example #22
Source File: article.py    From wikipedia-question-generator with MIT License 5 votes vote down vote up
def __init__(self, title):
        self.page = wikipedia.page(title)
        self.summary = TextBlob(self.page.summary) 
Example #23
Source File: Utils.py    From NotSoBot with MIT License 5 votes vote down vote up
def translate(self, lang1:str, lang2:str, *, text:str=None):
		if text is None:
			text = lang2
		try:
			translated = textblob.TextBlob(text).translate(from_lang=lang1, to=lang2)
		except textblob.exceptions.NotTranslated:
			await self.bot.say('Translation error.')
		else:
			await self.bot.say(translated) 
Example #24
Source File: nlp.py    From SML-Cogs with MIT License 5 votes vote down vote up
def translate_channel(self, msg, settings):
        """Translate channel according to settings."""
        server = msg.server
        if not settings.get("on_off"):
            return
        if msg.channel is None:
            return
        if msg.channel.id != settings.get("from_channel_id"):
            return
        if msg.author == server.me:
            return
        if msg.author.bot:
            return
        blob = TextBlob(msg.content)
        detected_lang = blob.detect_language()
        out = []
        for language in settings.get("languages"):
            if language != detected_lang:
                try:
                    # translated_msg = blob.translate(to=language)
                    translated_msg = await self._translate(msg.content, dest=language)
                    out.append(
                        "`{}` {}".format(
                            language, translated_msg))
                # except (textblob.exceptions.NotTranslated,
                #         textblob.exceptions.TranslatorError):
                #     pass
                except Exception as e:
                    print(e)
            if len(out):
                to_channel = self.bot.get_channel(settings.get("to_channel_id"))
                out.insert(0,
                           "**{}**\n`{}` {} {}".format(
                               msg.author.display_name,
                               detected_lang,
                               msg.content,
                               ' '.join([a.get('url') for a in msg.attachments])
                           ))
                await self.bot.send_message(to_channel, '\n'.join(out)) 
Example #25
Source File: nlp.py    From SML-Cogs with MIT License 5 votes vote down vote up
def spellcheck(self, ctx: Context, *, text: str):
        """Auto-correct spelling mistakes."""
        b = TextBlob(text)
        await self.bot.say(b.correct()) 
Example #26
Source File: nlp.py    From SML-Cogs with MIT License 5 votes vote down vote up
def sentiment(self, ctx: Context, *, text: str):
        """Return sentiment analysis of a text."""
        blob = TextBlob(text)
        stmt = blob.sentiment
        await self.bot.say(
            "Polairty: {0.polarity}\n"
            "Subjectivity: {0.subjectivity}"
            "".format(stmt)) 
Example #27
Source File: nlp.py    From SML-Cogs with MIT License 5 votes vote down vote up
def translate(self, ctx: Context, to_lang: str, *, text: str):
        """Translate to another language.

        Example:
        !translate es Simple is better than complex.
        will translate sentence to Spanish.

        !translatelang
        will list all the supported languages
        """
        out = await self._translate(text, dest=to_lang)
        await self.bot.say(out)

        # blob = TextBlob(text)
        # out = blob.translate(to=to_lang)
        # await self.bot.say(out) 
Example #28
Source File: app.py    From Python-DevOps with MIT License 5 votes vote down vote up
def classify():
    text = request.args.get('text')
    result = TextBlob(process_text(text))
    return jsonify(
        {
            'polarity': result.sentiment.polarity,
            'subjectivity': result.sentiment.subjectivity,
        }
    ) 
Example #29
Source File: models.py    From pytrader with MIT License 5 votes vote down vote up
def set_sentiment(self):
        sentiment = textblob.TextBlob(self.text).sentiment
        self.sentiment_polarity = sentiment.polarity
        self.sentiment_subjectivity = sentiment.subjectivity 
Example #30
Source File: Sentiment.py    From booksoup with MIT License 5 votes vote down vote up
def sentiment_timeline(self, name):
        timeline = self.fbt.generate_date_dict()
        sentiment_counts = self.fbt.generate_date_dict()
        for message in self.messages:
            if message.content is None or message.name != name:
                continue
            blob = TextBlob(message.content)
            timeline[message.date] += blob.sentiment.polarity
            sentiment_counts[message.date] += 1

        for k,v in timeline.iteritems():
            if v == 0:
                continue
            timeline[k] = v/sentiment_counts[k]
        return timeline