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


Python textblob.TextBlob方法代码示例

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


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

示例1: get_sentiment_analysis

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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 
开发者ID:stephancill,项目名称:mcafee2cash,代码行数:19,代码来源:main.py

示例2: create_data

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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] 
开发者ID:h2oai,项目名称:driverlessai-recipes,代码行数:26,代码来源:sentiment_score.py

示例3: stars_in_review

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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.

# %% 
开发者ID:snorkel-team,项目名称:snorkel-tutorials,代码行数:19,代码来源:recsys_tutorial.py

示例4: analize_sentiment

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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) 
开发者ID:zadewg,项目名称:Election-Meddling,代码行数:21,代码来源:deploy.py

示例5: _translate_message

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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 + ')')]) 
开发者ID:hangoutsbot,项目名称:hangoutsbot,代码行数:27,代码来源:_DEPRECATED_syncrooms_autotranslate.py

示例6: translate

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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) 
开发者ID:smlbiobot,项目名称:SML-Cogs,代码行数:18,代码来源:nlp.py

示例7: build_dict

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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 
开发者ID:eric-xw,项目名称:AREL,代码行数:22,代码来源:preprocess_ngrams.py

示例8: find_noun_phrases

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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] 
开发者ID:utkusen,项目名称:rhodiola,代码行数:20,代码来源:rhodiola.py

示例9: generate_html

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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 
开发者ID:hiway,项目名称:python-qutescript,代码行数:26,代码来源:sentimark.py

示例10: read_line_eos_noums

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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>' 
开发者ID:felipessalvatore,项目名称:MyTwitterBot,代码行数:20,代码来源:DataHolder.py

示例11: __init__

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
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() 
开发者ID:ministryofpromise,项目名称:tlp,代码行数:27,代码来源:tlp.py

示例12: iter_filth

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
def iter_filth(self, text):

        if not isinstance(self.disallowed_nouns, CanonicalStringSet):
            raise TypeError(
                'NameDetector.disallowed_nouns must be CanonicalStringSet'
            )

        # find the set of proper nouns using textblob.
        proper_nouns = set()
        blob = textblob.TextBlob(text)
        for word, part_of_speech in blob.tags:
            is_proper_noun = part_of_speech in ("NNP", "NNPS")
            if is_proper_noun and word.lower() not in self.disallowed_nouns:
                proper_nouns.add(word)

        # use a regex to replace the proper nouns by first escaping any
        # lingering punctuation in the regex
        # http://stackoverflow.com/a/4202559/564709
        if proper_nouns:
            re_list = []
            for proper_noun in proper_nouns:
                re_list.append(r'\b' + re.escape(str(proper_noun)) + r'\b')
            self.filth_cls.regex = re.compile('|'.join(re_list))
        else:
            self.filth_cls.regex = None
        return super(NameDetector, self).iter_filth(text) 
开发者ID:datascopeanalytics,项目名称:scrubadub,代码行数:28,代码来源:name.py

示例13: SentimentAnalysis

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
def SentimentAnalysis(_arg1, library="nltk"):
    """
    Sentiment Analysis is a procedure that assigns a score from -1 to 1
    for a piece of text with -1 being negative and 1 being positive. For
    more information on the function and how to use it please refer to
    tabpy-tools.md
    """
    if not (isinstance(_arg1[0], str)):
        raise TypeError

    supportedLibraries = {"nltk", "textblob"}

    library = library.lower()
    if library not in supportedLibraries:
        raise ValueError

    scores = []
    if library == "nltk":
        sid = SentimentIntensityAnalyzer()
        for text in _arg1:
            sentimentResults = sid.polarity_scores(text)
            score = sentimentResults["compound"]
            scores.append(score)
    elif library == "textblob":
        for text in _arg1:
            currScore = TextBlob(text)
            scores.append(currScore.sentiment.polarity)
    return scores 
开发者ID:tableau,项目名称:TabPy,代码行数:30,代码来源:SentimentAnalysis.py

示例14: analyze_sentiment

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
def analyze_sentiment(self, tweet):
        analysis = TextBlob(self.clean_tweet(tweet))
        
        if analysis.sentiment.polarity > 0:
            return 1
        elif analysis.sentiment.polarity == 0:
            return 0
        else:
            return -1 
开发者ID:vprusso,项目名称:youtube_tutorials,代码行数:11,代码来源:sentiment_anaylsis_twitter_data.py

示例15: noun_phrases

# 需要导入模块: import textblob [as 别名]
# 或者: from textblob import TextBlob [as 别名]
def noun_phrases(text):
    blob = TextBlob(text)
    return blob.tokenize() 
开发者ID:arguman,项目名称:arguman.org,代码行数:5,代码来源:utils.py


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