text_plain_output_4.png text_plain_output_3.png text_plain_output_1.png
from simpletransformers.classification import ClassificationModel

import numpy as np

import pandas as pd

df = pd.read_csv('../input/sentiment-analysis-for-financial-news/all-data.csv', encoding='ISO-8859-1')

from simpletransformers.classification import ClassificationModel
model = ClassificationModel('bert', 'bert-base-cased', num_labels=3, args={'reprocess_input_data': True, 'overwrite_output_dir': True}, use_cuda=False)

def making_label(st):
    if st == 'positive':
        return 0
    elif st == 'neutral':
        return 2
    else:
        return 1
train['label'] = train['sentiment'].apply(making_label)
eva['label'] = eva['sentiment'].apply(making_label)

train_df = pd.DataFrame({'text': train['statement'][:1500].replace('\\n', ' ', regex=True), 'label': train['label'][:1500]})
eval_df = pd.DataFrame({'text': eva['statement'][-400:].replace('\\n', ' ', regex=True), 'label': eva['label'][-400:]})

model.train_model(train_df)

result, model_outputs, wrong_predictions = model.eval_model(eval_df)

lst = []
for arr in model_outputs:
    lst.append(np.argmax(arr))

def get_result(statement):
    result = model.predict([statement])
    pos = np.where(result[1][0] == np.amax(result[1][0]))
    pos = int(pos[0])
    sentiment_dict = {0: 'positive', 1: 'negative', 2: 'neutral'}
    return

get_result('Sales in Finland decreased by 2.0 % , and international sales decreased by 9.3 % in terms of euros , and by 15.1 % in terms of local currencies .')