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

103 lines
3.0 KiB
Python
Raw Normal View History

2022-04-10 19:14:38 +02:00
from ast import Mod
2022-04-03 11:32:58 +02:00
import pandas as pd
import csv
import regex as re
from nltk import bigrams, word_tokenize
from collections import Counter, defaultdict
2022-04-10 18:24:05 +02:00
def read(self):
2022-04-10 18:13:50 +02:00
self.data = pd.read_csv(
self._path,
sep="\t",
error_bad_lines=False,
header=None,
2022-04-10 18:24:05 +02:00
quoting=csv.QUOTE_NONE
2022-04-10 18:13:50 +02:00
)
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
class Model:
2022-04-10 19:14:38 +02:00
def __init__(self, alpha):
2022-04-10 18:13:50 +02:00
self.alpha = alpha
self.model = defaultdict(lambda: defaultdict(lambda: 0))
self.vocab = set()
def train(self, data):
for _, row in data.iterrows():
words = word_tokenize(clean(row["final"]))
2022-04-10 19:14:38 +02:00
for w1, w2 in bigrams(words, pad_left=True, pad_right=True):
if w1 and w2:
self.model[w1][w2] += 1
self.vocab.add(w1)
self.vocab.add(w2)
2022-04-10 18:13:50 +02:00
for w1 in self.model:
total_count = float(sum(self.model[w1].values()))
2022-04-10 18:43:05 +02:00
denominator = total_count + self.alpha * len(self.vocab)
2022-04-10 18:13:50 +02:00
for w2 in self.model[w1]:
2022-04-10 18:43:05 +02:00
nominator = self.model[w1][w2] + self.alpha
self.model[w1][w2] = nominator / denominator
2022-04-03 11:32:58 +02:00
2022-04-10 19:14:38 +02:00
2022-04-10 18:13:50 +02:00
def _predict(self, word):
predictions = dict(self.model[word])
2022-04-10 18:43:05 +02:00
most_common = dict(Counter(predictions).most_common(6))
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
total_prob = 0.0
str_prediction = ""
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
for word, prob in most_common.items():
total_prob += prob
str_prediction += f"{word}:{prob} "
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
if not total_prob:
return "the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1"
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
if 1 - total_prob >= 0.01:
str_prediction += f":{1-total_prob}"
else:
str_prediction += f":0.01"
2022-04-03 11:32:58 +02:00
2022-04-10 18:13:50 +02:00
return str_prediction
2022-04-03 11:32:58 +02:00
2022-04-10 19:14:38 +02:00
def predict(self, read_path, save_path):
data = pd.read_csv(
read_path, sep="\t", error_bad_lines=False, header=None, quoting=csv.QUOTE_NONE
)
2022-04-10 18:13:50 +02:00
with open(save_path, "w") as file:
for _, row in data.iterrows():
words = word_tokenize(clean(row[6]))
if len(words) < 3:
prediction = "the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1"
else:
prediction = self._predict(words[-1])
file.write(prediction + "\n")
if __name__ == '__main__':
2022-04-03 11:32:58 +02:00
data = pd.read_csv(
2022-04-10 19:14:38 +02:00
"train/in.tsv.xz",
sep="\t",
error_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
2022-04-03 11:32:58 +02:00
)
2022-04-10 19:14:38 +02:00
train_labels = pd.read_csv(
"train/expected.tsv",
sep="\t",
error_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
)
train_data = data[[6, 7]]
train_data = pd.concat([train_data, train_labels], axis=1)
2022-04-10 18:13:50 +02:00
train_data["final"] = train_data[6] + train_data[0] + train_data[7]
2022-04-03 11:32:58 +02:00
2022-04-10 19:14:38 +02:00
model = Model(0.0001)
2022-04-10 18:13:50 +02:00
model.train(train_data)
model.predict("dev-0/in.tsv.xz", "dev-0/out.tsv")
2022-04-10 19:14:38 +02:00
model.predict("test-A/in.tsv.xz", "test-A/out.tsv")