Merge branch 'master' of https://git.wmi.amu.edu.pl/s470611/challenging-america-word-gap-prediction
This commit is contained in:
commit
be7281cb46
21036
dev-0/out.tsv
21036
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
54
run.py
54
run.py
@ -1,19 +1,10 @@
|
|||||||
|
from ast import Mod
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import csv
|
import csv
|
||||||
import regex as re
|
import regex as re
|
||||||
from nltk import bigrams, word_tokenize
|
from nltk import bigrams, word_tokenize
|
||||||
from collections import Counter, defaultdict
|
from collections import Counter, defaultdict
|
||||||
|
|
||||||
|
|
||||||
def clean(text):
|
|
||||||
text = str(text).lower().replace("-\\n", "").replace("\\n", " ")
|
|
||||||
return re.sub(r"\p{P}", "", text)
|
|
||||||
|
|
||||||
|
|
||||||
class Collection:
|
|
||||||
def __init__(self, path: str) -> None:
|
|
||||||
self._path = path
|
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
self.data = pd.read_csv(
|
self.data = pd.read_csv(
|
||||||
self._path,
|
self._path,
|
||||||
@ -25,7 +16,7 @@ class Collection:
|
|||||||
|
|
||||||
|
|
||||||
class Model:
|
class Model:
|
||||||
def __init__(self, alpha: float = 0.01) -> None:
|
def __init__(self, alpha):
|
||||||
self.alpha = alpha
|
self.alpha = alpha
|
||||||
self.model = defaultdict(lambda: defaultdict(lambda: 0))
|
self.model = defaultdict(lambda: defaultdict(lambda: 0))
|
||||||
self.vocab = set()
|
self.vocab = set()
|
||||||
@ -46,6 +37,7 @@ class Model:
|
|||||||
nominator = self.model[w1][w2] + self.alpha
|
nominator = self.model[w1][w2] + self.alpha
|
||||||
self.model[w1][w2] = nominator / denominator
|
self.model[w1][w2] = nominator / denominator
|
||||||
|
|
||||||
|
|
||||||
def _predict(self, word):
|
def _predict(self, word):
|
||||||
predictions = dict(self.model[word])
|
predictions = dict(self.model[word])
|
||||||
most_common = dict(Counter(predictions).most_common(6))
|
most_common = dict(Counter(predictions).most_common(6))
|
||||||
@ -67,7 +59,11 @@ class Model:
|
|||||||
|
|
||||||
return str_prediction
|
return str_prediction
|
||||||
|
|
||||||
def _save(self, save_path: str, data):
|
|
||||||
|
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
|
||||||
|
)
|
||||||
with open(save_path, "w") as file:
|
with open(save_path, "w") as file:
|
||||||
for _, row in data.iterrows():
|
for _, row in data.iterrows():
|
||||||
words = word_tokenize(clean(row[6]))
|
words = word_tokenize(clean(row[6]))
|
||||||
@ -77,30 +73,30 @@ class Model:
|
|||||||
prediction = self._predict(words[-1])
|
prediction = self._predict(words[-1])
|
||||||
file.write(prediction + "\n")
|
file.write(prediction + "\n")
|
||||||
|
|
||||||
def predict(self, read_path: str, save_path: str):
|
|
||||||
collection = Collection(read_path)
|
|
||||||
collection.read()
|
|
||||||
self._save(save_path, collection.data)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
data = Collection("train/in.tsv.xz")
|
data = pd.read_csv(
|
||||||
data.read()
|
"train/in.tsv.xz",
|
||||||
|
sep="\t",
|
||||||
|
error_bad_lines=False,
|
||||||
|
header=None,
|
||||||
|
quoting=csv.QUOTE_NONE,
|
||||||
|
)
|
||||||
|
train_labels = pd.read_csv(
|
||||||
|
"train/expected.tsv",
|
||||||
|
sep="\t",
|
||||||
|
error_bad_lines=False,
|
||||||
|
header=None,
|
||||||
|
quoting=csv.QUOTE_NONE,
|
||||||
|
)
|
||||||
|
|
||||||
train_labels = Collection("train/expected.tsv")
|
train_data = data[[6, 7]]
|
||||||
train_labels.read()
|
train_data = pd.concat([train_data, train_labels], axis=1)
|
||||||
|
|
||||||
|
|
||||||
train_data = data.data[[6, 7]]
|
|
||||||
train_data = pd.concat([train_data, train_labels.data], axis=1)
|
|
||||||
|
|
||||||
train_data["final"] = train_data[6] + train_data[0] + train_data[7]
|
train_data["final"] = train_data[6] + train_data[0] + train_data[7]
|
||||||
|
|
||||||
|
model = Model(0.0001)
|
||||||
model = Model()
|
|
||||||
model.train(train_data)
|
model.train(train_data)
|
||||||
|
|
||||||
model.predict("dev-0/in.tsv.xz", "dev-0/out.tsv")
|
model.predict("dev-0/in.tsv.xz", "dev-0/out.tsv")
|
||||||
model.predict("test-A/in.tsv.xz", "test-A/out.tsv")
|
model.predict("test-A/in.tsv.xz", "test-A/out.tsv")
|
14828
test-A/out.tsv
14828
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user