Reading Train file done, fixed os path issues

pavan
Pavan Mandava 6 years ago
parent 3fe33ab51a
commit 0577f982a2

@ -1,6 +1,8 @@
from eval.metrics import f1_score
import utils.constants as const
from sklearn.metrics import f1_score as f1
import os
from utils.csv import read_csv_file
y_true = ['positive', 'positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative']
y_pred = ['positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative', 'positive', 'positive', 'negative', 'negative', 'negative']
@ -18,3 +20,13 @@ for result in result_list:
result.print_result()
print('SK Learn F1 Score (MACRO):: ', f1(y_true, y_pred, ['positive', 'negative'], average='macro'))
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
train_file_path = project_root+'/data/tsv/train.tsv'
print(train_file_path)
data = read_csv_file(csv_file_path=train_file_path, delimiter='\t')
for inst in data[:5]:
inst.print()

@ -0,0 +1,19 @@
import csv
from utils.models import DataInstance
def read_csv_file(csv_file_path, delimiter='\t'):
"""
This function takes file path as an argument, reads the data file and
returns a list of DataInstance objects with text and true labels
:param delimiter: Delimiter for the file. Default is Tab(\t)
:param csv_file_path: path to the TSV/CSV file
:return: returns a list of DataInstance class objects. <utils.models.DataInstance>
"""
with open(csv_file_path, 'r') as file:
file_data = csv.reader(file, delimiter=delimiter)
data = []
for row in file_data:
data.append(DataInstance(row[0], row[2], row[3]))
return data

@ -0,0 +1,13 @@
class DataInstance:
"""
Model Class for carrying Training and Testing data from tsc/csv file
"""
def __init__(self, r_id, text, true_label):
self.did = r_id
self.text = text
self.true_label = true_label
def print(self):
print('True Label :: ', self.true_label, ' Text :: ', self.text)
Loading…
Cancel
Save