本文整理汇总了Python中textblob.TextBlob.replace方法的典型用法代码示例。如果您正苦于以下问题:Python TextBlob.replace方法的具体用法?Python TextBlob.replace怎么用?Python TextBlob.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类textblob.TextBlob
的用法示例。
在下文中一共展示了TextBlob.replace方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calcSentiment
# 需要导入模块: from textblob import TextBlob [as 别名]
# 或者: from textblob.TextBlob import replace [as 别名]
def calcSentiment(input):
''' calculate sentiment for each twitterfile and average this'''
# Part of code based on master thesis of Guangxue Cao
tweets_data = []
sentiment_array = []
total = 0
OneTweetTime = ""
average_sentiment = 0
# load input
for line in input:
tweets_data.append(line)
# iterate over all tweets
for tweet_data in tweets_data:
tweet = tweet_data["text"]
# analyze tweet with TextBlob to gain sentiment
tweet = TextBlob(tweet)
OneTweetTime = tweet_data["created_at"]
# remove empty lines
tweet = tweet.replace("\n", " ")
tweet = tweet.replace("\r "," ")
sentiment = tweet.sentiment.polarity
sentiment_array.append(sentiment)
for sentiment in sentiment_array:
total += sentiment
if len(sentiment_array) != 0:
average_sentiment = total / len(sentiment_array)
return [OneTweetTime, average_sentiment]
# writer.writerow([OneTweetTime,"sentiment:", average_sentiment])
# tweet = TextBlob(tweet,analyzer=NaiveBayesAnalyzer())
示例2: open
# 需要导入模块: from textblob import TextBlob [as 别名]
# 或者: from textblob.TextBlob import replace [as 别名]
#Training the classifier on the body dataset
with open("dataset2.json", 'r', encoding="utf-8-sig") as fp2:
cl2 = NaiveBayesClassifier(fp2, format="json")
#Taking the string values
str1 = str(headline)
headline = TextBlob(str1)
body = str(body)
tb_body = TextBlob(body)
subjectivity = tb_body.sentiment.subjectivity
subjectivity = float(subjectivity) * 100
body_classify = str(cl2.classify(body))
body = body.lower()
#Finding the subjectivity
headline = headline.replace('Was', '')
headline = headline.replace('was', '')
headline = headline.replace('’','')
#Finding the tags in the sentence
array = headline.tags
array1 = []
#Finding the hot words
for ii in array:
name, tag = ii
name = str(name)
name = name.lower()
if(tag.count('NN')>0):
name = TextBlob(name)
array1.append(name)
示例3: TextBlob
# 需要导入模块: from textblob import TextBlob [as 别名]
# 或者: from textblob.TextBlob import replace [as 别名]
auth.set_access_token(config['access_token'], config['access_token_secret'])
api = tweepy.API(auth)
# Load text file
filename=open("./txt/marx2.txt",'r')
text=filename.readlines()
text = ' '.join(text)
filename.close()
blob = TextBlob(text.decode('utf-8'))
tags = blob.tags
for blobs in blob.tags:
if blobs[1] == 'NNP':
wordchange = '#'+blobs[0]
blob = blob.replace(blobs[0],wordchange)
print "changing: " + wordchange
for sentence in blob.sentences:
sentence = re.sub('\#+', '#', str(sentence))
print sentence
print "--"
try:
print "next tweet: " + str(sentence)
api.update_status(sentence)
time.sleep(120)#Tweet every 15 minutes
except:
continue
#blob.translate(to="es") # 'La amenaza titular de The Blob...'