forked from azk0019/CourseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
50 lines (35 loc) · 1.42 KB
/
test.py
File metadata and controls
50 lines (35 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# import necessary packages
from simpletransformers.classification import ClassificationModel
import pandas as pd
import json
from emot.emo_unicode import UNICODE_EMO
from gensim.parsing.preprocessing import remove_stopwords
# Initialize variables
DATA_PATH = './data/test.jsonl'
FINAL_RESULTS = 'answer.txt'
MODEL_LOCATION = 'outputs'
MODEL_TYPE = 'bert'
word_dist = []
pred = []
# Converts emojis into text
def convert_emojis(text):
for emot in UNICODE_EMO:
text = text.replace(emot, "_".join(UNICODE_EMO[emot].replace(",", "").replace(":", "").split()))
return text
def predict_sarcasm(data_path, results, model_loc, model):
# Bringing in the test data
with open(data_path, 'r') as json_file:
json_list = list(json_file)
for json_str in json_list:
pred.append(json.loads(json_str))
pred_response = [remove_stopwords(convert_emojis(pred[i]['response'])) for i in range(len(pred))]
pred_id = [pred[i]['id'] for i in range(len(pred))]
model = ClassificationModel(model, model_loc, use_cuda=False)
predictions, raw_outputs = model.predict(pred_response)
pred_bert = pd.DataFrame({
'id': pred_id,
'label': predictions
})
pred_bert['label'] = pred_bert['label'].replace([1, 0], ['SARCASM', 'NOT_SARCASM'])
pd.DataFrame(pred_bert).to_csv(results, header=False, sep=',', index=False)
predict_sarcasm(DATA_PATH, FINAL_RESULTS, MODEL_LOCATION, MODEL_TYPE)