52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
import pandas as pd
|
|
from sklearn.naive_bayes import GaussianNB
|
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
nrows = 10000
|
|
|
|
|
|
def load_data(path):
|
|
try:
|
|
in_df = pd.read_csv(f'{path}/in.tsv', sep='\t', header=None, nrows=nrows, error_bad_lines=False)
|
|
except:
|
|
in_df = pd.read_csv(f'{path}/train.tsv', sep='\t', header=None, nrows=nrows, error_bad_lines=False)
|
|
try:
|
|
exp_df = pd.read_csv(f'{path}/expected.tsv', sep='\t', header=None, nrows=nrows, error_bad_lines=False)
|
|
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 = load_data('train')
|
|
x_df, y_df = in_df[0][1], in_df[0][0]
|
|
|
|
vectorizer = TfidfVectorizer(lowercase=True, stop_words=['english'])
|
|
train_x = vectorizer.fit_transform(x_df)
|
|
|
|
|
|
gnb = GaussianNB()
|
|
gnb.fit(train_x.toarray(), y_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() |