text_plain_output_1.png
from ktrain import text

import ktrain

import pandas as pd

import pathlib

train_path = '../input/sentimentdatasets/testStackOverFlow.csv'
tr_path = pathlib.Path(train_path)
if tr_path.exists():
    train_df = pd.read_csv(train_path, encoding='utf-16', sep=';', header=None)
else:
    raise SystemExit('Train path does not exist.')
model_path = '../input/models/model.h5'
mo_path = pathlib.Path(model_path)
raise SystemExit('Model path does not exist.')
data_path = '../input/sentimentdatasets/github_gold.csv'
da_path = pathlib.Path(data_path)
if da_path.exists():
    test_df = pd.read_csv(data_path, sep=';', header=0)
else:
    raise SystemExit('Data path does not exist.')

(x_train, y_train), (x_test, y_test), preproc = text.texts_from_array(train_df[2], train_df[1], x_test=test_df['Text'], y_test=test_df['Polarity'], maxlen=500, preprocess_mode='bert')

learner = ktrain.get_learner(text.text_classifier('bert', (x_train, y_train), preproc=preproc), train_data=(x_train, y_train), val_data=(x_test, y_test), batch_size=6)

learner.load_model(model_path)

predictor = ktrain.get_predictor(learner.model, preproc)

have_test_dataset = False
data_df = test_df
if have_test_dataset:
    data = test_df[2].tolist()
    label = test_df[1].tolist()
else:
    total_dataset = data_df.shape[0]
    test_data = round(total_dataset * 0.3)
    data_list = data_df['Text'].tolist()
    data = data_list[-test_data - 1:-1]
    label_list = data_df['Polarity'].tolist()
    label = label_list[-test_data - 1:-1]

print('Showing prediction mistakes on test set!')
i = 0
correct = 0
wrong = 0
total = len(data)
for dt in data:
    result = predictor.predict(dt)
    if result == label[i]:
        correct += 1
    else:
        wrong += 1
        print('Wrong result on sentence:\n', dt, '\nExpected: ', label[i], '\nPredicted: ', result)
    i += 1