[英]Adding '-' sign to negative flair sentiment analysis
我正在為股市分析創建情緒分析代碼。 這是代碼的核心:
import flair
flair_sentiment = flair.models.TextClassifier.load('en-sentiment')
columns = ['ticker', 'date', 'time', 'headline']
parsed_and_scored_news = pd.DataFrame(parsed_news, columns=columns)
sentiment = []
for head in parsed_and_scored_news['headline']:
s = flair.data.Sentence(head)
flair_sentiment.predict(s)
total_sentiment = s.labels
sentiment.append(total_sentiment)
scores_df = pd.DataFrame(sentiment)
parsed_and_scored_news = parsed_and_scored_news.join(scores_df, rsuffix='_right')
# Convert the date column from string to datetime
parsed_and_scored_news['date'] = pd.to_datetime(parsed_and_scored_news.date).dt.dateparsed_and_scored_news.head()
生產以下output:
ticker date time headline 0
0 AMZN 2021-03-26 02:37PM Tech stocks are going to do vey well going for... POSITIVE (0.9986)
1 AMZN 2021-03-26 01:17PM Amazon mocked idea its drivers urinated in bot... NEGATIVE (0.9855)
2 AMZN 2021-03-26 01:11PM ThredUp CEO on IPO day: Dont tax resale and Am... NEGATIVE (0.6743)
3 AMZN 2021-03-26 12:54PM Why this retailer is seeing a triple-digit sal... POSITIVE (0.9597)
4 AMZN 2021-03-26 12:07PM How to secure your smart home camera POSITIVE (0.9981)
因為我想將數據輸入到 ML model 我需要分數是數字。 我知道使用probability = sentence.labels[0].score
只給我們分數,但這意味着沒有辦法區分陳述是積極的還是消極的。 有沒有辦法在歸類為負數的分數后面添加一個“-”(否定)符號。 例如 - NEGATIVE (0.9855) = -9855
。 這將確保信息是數字的並且是有用的。
這段代碼對我有用:
sentiment = []
sentiment_score =[]
for head in parsed_and_scored_news['headline']:
s = flair.data.Sentence(head)
flair_sentiment.predict(s)
total_sentiment = s.labels[0].value
total_sentiment_score = s.labels[0].score
sentiment.append(total_sentiment)
sentiment_score.append(total_sentiment_score)
scores_df = pd.DataFrame(sentiment)
scores_df_1 = pd.DataFrame(sentiment_score)
parsed_and_scored_news = parsed_and_scored_news.join(scores_df, rsuffix='_right')
parsed_and_scored_news = parsed_and_scored_news.join(scores_df_1, rsuffix='_right')
st = parsed_and_scored_news['0_right'].tolist()
count = -1
for item in parsed_and_scored_news['0']:
count = count+1
if item == 'NEGATIVE':
lst[count] = 0-lst[count]
scores_final = pd.DataFrame(lst)
parsed_and_scored_news = parsed_and_scored_news.join(scores_final, rsuffix='_final')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.