88 lines
2.1 KiB
Python
88 lines
2.1 KiB
Python
import lzma
|
|
import sys
|
|
from io import StringIO
|
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
import pandas as pd
|
|
import csv
|
|
import numpy
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.naive_bayes import GaussianNB
|
|
|
|
nrows = 10000
|
|
|
|
|
|
def load_data(path):
|
|
in_df = pd.read_csv(f'{path}/in.tsv.xz', sep='\t', nrows=nrows, header=None)
|
|
try:
|
|
exp_df = pd.read_csv(f'{path}/expected.tsv', sep='\t', nrows=nrows, header=None)
|
|
except:
|
|
exp_df = None
|
|
return in_df, exp_df
|
|
|
|
|
|
def write_res(data, path):
|
|
with open(path, 'w') as f:
|
|
for line in data:
|
|
f.write(f'{line}\n')
|
|
print(f"Data written {path}/out.tsv")
|
|
|
|
|
|
|
|
|
|
def main():
|
|
in_df, exp_df = load_data('train')
|
|
in_df = in_df.drop(in_df.columns[1], axis=1)
|
|
|
|
vectorizer = TfidfVectorizer(lowercase=True, stop_words=['english'])
|
|
X = vectorizer.fit_transform(in_df.to_numpy().ravel())
|
|
vectorizer.get_feature_names_out()
|
|
|
|
# in_df = in_df.reset_index()
|
|
|
|
tfidfVector = vectorizer.transform(in_df[0])
|
|
gnb = GaussianNB()
|
|
gnb.fit(tfidfVector.todense(), exp_df)
|
|
|
|
paths = ['dev-0', 'test-A']
|
|
|
|
for path in paths:
|
|
x = load_data(path)
|
|
vec = vectorizer.transform(x[0][0])
|
|
|
|
result = gnb.predict(vec.todense())
|
|
write_res(result, f'{path}/out.tsv')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
|
|
# testXPath = "./dev-0/in.tsv.xz"
|
|
# testYPath = "./dev-0/expected.tsv"
|
|
|
|
# testX = pd.read_csv(testXPath, sep='\t', nrows=nrows, header=None)
|
|
|
|
# testY = pd.read_csv(testYPath, sep='\t', nrows=nrows, header=None)
|
|
# testXtfidfVector = vectorizer.transform(testX[0])
|
|
|
|
|
|
|
|
# testXPath = "./test-A/in.tsv.xz"
|
|
# testYPath = "./test-A/expected.tsv"
|
|
|
|
# testX = pd.read_csv(testXPath, sep='\t', nrows=nrows, header=None)
|
|
|
|
# # testY = pd.read_csv(testYPath, sep='\t', nrows=nrows, header=None)
|
|
# testXtfidfVector = vectorizer.transform(testX[0])
|
|
|
|
|
|
|
|
# pred = gnb.predict(testXtfidfVector.todense())
|
|
# print(pred)
|
|
|
|
|
|
# with open(testYPath, 'w', newline='') as f_output:
|
|
# tsv_output = csv.writer(f_output, delimiter='\n')
|
|
# tsv_output.writerow(pred) |