challenging-america-word-ga.../run.py

66 lines
2.4 KiB
Python
Raw Normal View History

2022-04-12 10:01:45 +02:00
#%%
import pandas as pd
import csv
2022-04-24 17:49:53 +02:00
import os
import kenlm
2022-04-24 20:32:19 +02:00
from collections import Counter, defaultdict
from math import log10
2022-04-12 10:01:45 +02:00
#%%
2022-04-23 10:07:45 +02:00
def clean(text):
2022-04-24 20:32:19 +02:00
text = str(text).lower().strip().replace("", "'").replace('\\n', " ").replace("'t", " not").replace("'s", " is").replace("'ll", " will").replace("'m", " am").replace("'ve", " have").replace(",", "").replace("-", "").replace(".", "").replace("'", "".replace("", "").replace(">", ""))
2022-04-23 10:07:45 +02:00
return text
2022-04-12 10:01:45 +02:00
2022-04-23 10:07:45 +02:00
train_in = pd.read_csv("train/in.tsv.xz", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)[[6, 7]]
train_expected = pd.read_csv("train/expected.tsv", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)
data = pd.concat([train_in, train_expected], axis=1)
data = data[6] + data[0] + data[7]
data = data.apply(clean)
2022-04-12 10:01:45 +02:00
2022-04-24 17:49:53 +02:00
if not os.path.isfile('train_file.txt'):
with open("train_file.txt", "w+") as f:
for text in data:
f.write(text + "\n")
2022-04-12 10:01:45 +02:00
2022-04-23 10:07:45 +02:00
#%%
2022-04-24 20:32:19 +02:00
#get_ipython().system('../kenlm/build/bin/lmplz -o 4 < train_file.txt > model.arpa --skip_symbols')
2022-04-24 17:49:53 +02:00
model = kenlm.Model("model.arpa")
#%%
import nltk
2022-04-24 20:32:19 +02:00
from nltk import word_tokenize
2022-04-24 17:49:53 +02:00
nltk.download('punkt')
2022-04-12 10:01:45 +02:00
2022-04-24 17:49:53 +02:00
most_common = defaultdict(lambda: 0)
for text in data:
words = word_tokenize(text)
if "d" in words:
words.remove("d")
for w in words:
most_common[w] += 1
most_common = Counter(most_common).most_common(8000)
2022-04-12 10:01:45 +02:00
#%%
2022-04-23 10:07:45 +02:00
def predict(path, result_path):
2022-04-24 20:32:19 +02:00
data = pd.read_csv(path, sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE)
2022-04-12 10:01:45 +02:00
with open(result_path, "w+", encoding="UTF-8") as f:
2022-04-24 20:32:19 +02:00
for i, row in data.iterrows():
2022-04-24 17:49:53 +02:00
result = {}
2022-04-24 20:32:19 +02:00
before = word_tokenize(clean(str(row[6])))[-3:]
if(len(before) < 2):
2022-04-24 17:49:53 +02:00
result = "a:0.2 the:0.2 to:0.2 of:0.1 and:0.1 of:0.1 :0.1"
2022-04-24 20:32:19 +02:00
else:
for w in most_common:
word = w[0]
prob = model.score(" ".join(before + [word]))
result[word] = prob
predictions = dict(Counter(result).most_common(12))
result = ""
for word, prob in predictions.items():
result += f"{word}:{prob} "
result += f':{log10(0.99)}'
2022-04-24 17:49:53 +02:00
f.write(result + "\n")
print(result)
2022-04-12 10:01:45 +02:00
2022-04-23 10:07:45 +02:00
predict("dev-0/in.tsv.xz", "dev-0/out.tsv")
2022-04-24 20:32:19 +02:00
predict("test-A/in.tsv.xz", "test-A/out.tsv")