akshare股市新聞情緒判斷


1

# -*- coding: utf-8 -*-
import time
import akshare as ak
from snownlp import SnowNLP

# 使用snownlp
stock_code = '603777'
date = time.strftime("%Y%m%d", time.localtime())
stock_news_em_df = ak.stock_news_em(stock=stock_code)

for i in stock_news_em_df.values[:, 1]:
    text=str(i)
    # text = u'中國人是好人'
    s = SnowNLP(text)
    for sentence in s.sentences:
        print(sentence, SnowNLP(sentence).sentences)
    print(s.sentiments)
    print(s.keywords(3))
    print(s.summary(3))
    # 小於0.4的為消極,否則為積極
    if s.sentiments<0.4:
        print('##########消極',i)
    elif s.sentiments>=0.4:
        print('##########積極',i)

 2

# 使用nltk
# from nltk.sentiment.vader import SentimentIntensityAnalyzer as sia
# import nltk
# import time
# import akshare as ak
# import jieba as jb
#
# # nltk.set_proxy('SYSTEM PROXY')
# # nltk.download('vader_lexicon')
#
# stock_code='603777'
# date=time.strftime("%Y%m%d", time.localtime())
# # sentences = ['This is the worst lunch I ever had!',
# #              'This is the best lunch I have ever had!!',
# #              'I don\'t like this lunch.',
# #              'I eat food for lunch.',
# #              'Red is a color.',
# #              'A really bad, horrible book, the plot was .']
# # '''每日快訊'''
# # stock_zh_a_alerts_cls_df = ak.stock_zh_a_alerts_cls()
# # '''當日最近 4 小時內的新聞資訊數據'''
# # js_news_df = ak.js_news(timestamp=date + "11:27:18")
# '''個股當日最近 20 條新聞資訊數據'''
# stock_news_em_df = ak.stock_news_em(stock=stock_code)
# sentences=[]
# for i in stock_news_em_df.values[:,1]:
#     seg_list = jb.cut_for_search(i)
#     print(", ".join(seg_list))
#     sentences.append(", ".join(seg_list))
# hal = sia()
# for sentence in sentences:
#     print(sentence)
#     ps = hal.polarity_scores(sentence)
#     for k in sorted(ps):
#         print('\t{}: {:>1.4}'.format(k, ps[k]), end='  ')
#     print()

 3

# 使用fair
# from flair.models import TextClassifier
# from flair.data import Sentence
#
# sia = TextClassifier.load('en-sentiment')
#
#
# def flair_prediction(x):
#     sentence = Sentence(x)
#     sia.predict(sentence)
#     score = sentence.labels[0]
#     if "POSITIVE" in str(score):
#         return "pos"
#     elif "NEGATIVE" in str(score):
#         return "neg"
#     else:
#         return "neu"
#
# flair_prediction('hahahahah')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM