Andreagus's picture
Update aggregator.py
1d9007f verified
raw
history blame
8.91 kB
from bezinga_caller import bezinga_get
from finub_caller import get_finhub
from marketaux_caller import get_marketaux
from newsapi_caller import get_newsapi
from newsdata_caller import get_newsdata
from vantage_caller import get_vantage
from transformers import pipeline
def get_articles_sentiment(ticker, model):
pipe = pipeline("text-classification", model=model)
# getting a list of article of given ticket from different sources
try:
bezinga_list = bezinga_get(ticker)
print("BEZINGA returned")
bezinga_results = pipe(bezinga_list)
print("evaluated")
except Exception as e:
print(e)
bezinga_results = []
try:
newsapi_list = get_newsapi(ticker)
print("NEWSAPI returned")
newsapi_results = pipe(newsapi_list)
print("evaluated")
except Exception as e:
print(e)
newsapi_results = []
try:
newsdata_list = get_newsdata(ticker)
print("NEWSDATA returned")
newsdata_results = pipe(newsdata_list)
print("evaluated")
except Exception as e:
print(e)
newsdata_results = []
try:
finhub_list = get_finhub(ticker)
print("FINHUB returned")
finhub_results = pipe(finhub_list)
print("evaluated")
except Exception as e:
print(e)
finhub_results = []
try:
vantage_list = get_vantage(ticker)
print("VANTAGE returned")
vantage_results = pipe(vantage_list)
print("evaluated")
except Exception as e:
print(e)
vantage_results = []
# replacing values for calculations and doing the sentiment for each source
def replace_values(result):
# Replace values in the label column
for dict in result:
if dict["label"] == "LABEL_1":
dict["label"] = 2
else:
dict["label"] = 1
total_articles = len(bezinga_results) + len(finhub_results) + len(newsapi_results) + len(newsdata_results) + len(vantage_results)
bezinga_positives = []
bezinga_negatives = []
finhub_positives = []
finhub_negatives = []
newsapi_positives = []
newsapi_negatives = []
newsdata_positives = []
newsdata_negatives = []
vantage_positives = []
vantage_negatives = []
try:
replace_values(bezinga_results)
bezinga_label_mean = float(sum(d['label'] for d in bezinga_results)) / len(bezinga_results)
for dict in bezinga_results:
if dict["label"] == 2:
bezinga_positives.append(dict)
else:
bezinga_negatives.append(dict)
if len(bezinga_positives) > 0:
bezinga_positive_score_mean = float(sum(d['score'] for d in bezinga_positives)) / len(bezinga_positives)
if len(bezinga_negatives) > 0:
bezinga_negative_score_mean = float(sum(d['score'] for d in bezinga_negatives)) / len(bezinga_negatives)
except Exception as e:
print(e)
# finhub
if len(finhub_results) > 0:
replace_values(finhub_results)
finhub_label_mean = float(sum(d['label'] for d in finhub_results)) / len(finhub_results)
for dict in finhub_results:
if dict["label"] == 2:
finhub_positives.append(dict)
else:
finhub_negatives.append(dict)
if len(finhub_positives) > 0:
finhub_positive_score_mean = float(sum(d['score'] for d in finhub_positives)) / len(finhub_positives)
if len(finhub_negatives) > 0:
finhub_negative_score_mean = float(sum(d['score'] for d in finhub_negatives)) / len(finhub_negatives)
# newsapi
if len(newsapi_results) > 0:
replace_values(newsapi_results)
newsapi_label_mean = float(sum(d['label'] for d in newsapi_results) + 1) / (len(newsapi_results) + 2)
for dict in newsapi_results:
if dict["label"] == 2:
newsapi_positives.append(dict)
else:
newsapi_negatives.append(dict)
if len(newsapi_positives) > 0:
newsapi_positive_score_mean = float(sum(d['score'] for d in newsapi_positives)) / len(newsapi_positives)
if len(newsapi_negatives) > 0:
newsapi_negative_score_mean = float(sum(d['score'] for d in newsapi_negatives)) / len(newsapi_negatives)
# newsdata
if len(newsdata_results) > 0:
replace_values(newsdata_results)
newsdata_label_mean = float(sum(d['label'] for d in newsdata_results)) / len(newsdata_results)
for dict in newsdata_results:
if dict["label"] == 2:
newsdata_positives.append(dict)
else:
newsdata_negatives.append(dict)
if len(newsdata_positives) > 0:
newsdata_positive_score_mean = float(sum(d['score'] for d in newsdata_positives)) / len(newsdata_positives)
if len(newsdata_negatives) > 0:
newsdata_negative_score_mean = float(sum(d['score'] for d in newsdata_negatives)) / len(newsdata_negatives)
# vantage
if len(vantage_results) > 0:
replace_values(vantage_results)
vantage_label_mean = float(sum(d['label'] for d in vantage_results)) / len(vantage_results)
for dict in vantage_results:
if dict["label"] == 2:
vantage_positives.append(dict)
else:
vantage_negatives.append(dict)
if len(vantage_positives) > 0:
vantage_positive_score_mean = float(sum(d['score'] for d in vantage_positives)) / len(vantage_positives)
if len(vantage_negatives) > 0:
vantage_negative_score_mean = float(sum(d['score'] for d in vantage_negatives)) / len(vantage_negatives)
total_positives = len(bezinga_positives) + len(finhub_positives) + len(newsapi_positives) + len(newsdata_positives) + len(vantage_positives)
total_negatives = len(bezinga_negatives) + len(finhub_negatives) + len(newsapi_negatives) + len(newsdata_negatives) + len(vantage_negatives)
results_dict = {
"bezinga": {
"bezinga_articles": len(bezinga_results),
"bezinga_positives": len(bezinga_positives),
"bezinga_negatives": len(bezinga_negatives),
"bezinga_sentiment_mean": bezinga_label_mean if len(bezinga_results) > 0 else 0,
"bezinga_positive_score_mean": bezinga_positive_score_mean if len(bezinga_results) > 0 else 0,
"bezinga_negative_score_mean": bezinga_negative_score_mean if len(bezinga_results) > 0 else 0
},
"finhub": {
"finhub_articles": len(finhub_results),
"finhub_positives": len(finhub_positives),
"finhub_negatives": len(finhub_negatives),
"finhub_sentiment_mean": finhub_label_mean if len(finhub_results) > 0 else 0,
"finhub_positive_score_mean": finhub_positive_score_mean if len(finhub_results) > 0 else 0,
"finhub_negative_score_mean": finhub_negative_score_mean if len(finhub_results) > 0 else 0
},
"newsapi": {
"newsapi_articles": len(newsapi_results),
"newsapi_positives": len(newsapi_positives),
"newsapi_negatives": len(newsapi_negatives),
"newsapi_sentiment_mean": newsapi_label_mean if len(newsapi_results) > 0 else 0,
"newsapi_positive_score_mean": newsapi_positive_score_mean if len(newsapi_results) > 0 else 0,
"newsapi_negative_score_mean": newsapi_negative_score_mean if len(newsapi_results) > 0 else 0
},
"newsdata": {
"newsdata_articles": len(newsdata_results),
"newsdata_positives": len(newsdata_positives),
"newsdata_negatives": len(newsdata_negatives),
"newsdata_sentiment_mean": newsdata_label_mean if len(newsdata_results) > 0 else 0,
"newsdata_positive_score_mean": newsdata_positive_score_mean if len(newsdata_results) > 0 else 0,
"newsdata_negative_score_mean": newsdata_negative_score_mean if len(newsdata_results) > 0 else 0
},
"vantage": {
"vantage_articles": len(vantage_results),
"vantage_positives": len(vantage_positives),
"vantage_negatives": len(vantage_negatives),
"vantage_sentiment_mean": vantage_label_mean if len(vantage_results) > 0 else 0,
"vantage_positive_score_mean": vantage_positive_score_mean if len(vantage_results) > 0 else 0,
"vantage_negative_score_mean": vantage_negative_score_mean if len(vantage_results) > 0 else 0
},
"total_articles": total_articles,
"total_positives": total_positives,
"total_negatives": total_negatives
}
return results_dict