Wygładzanie Alpha

This commit is contained in:
Dominik Strzałko 2022-04-11 00:20:54 +02:00
parent 44393b0010
commit 6da349e88f
7 changed files with 18049 additions and 0 deletions

Binary file not shown.

Binary file not shown.

10519
dev-0/out.tsv Normal file

File diff suppressed because it is too large Load Diff

26
run.py Normal file
View File

@ -0,0 +1,26 @@
import pandas as pd
from collections import defaultdict
from utils import read_csv
from train import train_model, predict_data
def main():
data = read_csv("train/in.tsv.xz")
train_words = read_csv("train/expected.tsv")
train_data = data[[6, 7]]
train_data = pd.concat([train_data, train_words], axis=1)
train_data["607"] = train_data[6] + train_data[0] + train_data[7]
model = defaultdict(lambda: defaultdict(lambda: 0))
alpha = 0.00002
vocab = set()
train_model(train_data, model,vocab,alpha)
predict_data("dev-0/in.tsv.xz", "dev-0/out.tsv", model)
predict_data("test-A/in.tsv.xz", "test-A/out.tsv", model)
if __name__ == "__main__":
main()

7414
test-A/out.tsv Normal file

File diff suppressed because it is too large Load Diff

57
train.py Normal file
View File

@ -0,0 +1,57 @@
from collections import Counter
from nltk import bigrams, word_tokenize
from utils import read_csv, ENCODING, clean_text
DEFAULT_PREDICTION = "the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1"
def train_model(data, model,vocab,alpha):
for _, row in data.iterrows():
words = word_tokenize(clean_text(row["607"]))
for w1, w2 in bigrams(words, pad_left=True, pad_right=True):
if w1 and w2:
model[w2][w1] += 1
vocab.add(w2)
vocab.add(w1)
for w2 in model:
total_count = float(sum(model[w2].values()))
denominator = total_count + alpha * len(vocab)
for w1 in model[w2]:
nominator = model[w2][w1] + alpha
model[w2][w1] /= nominator / denominator
def predict_data(read_path, save_path, model):
data = read_csv(read_path)
with open(save_path, "w", encoding=ENCODING) as f:
for _, row in data.iterrows():
words = word_tokenize(clean_text(row[7]))
if len(words) < 3:
prediction = DEFAULT_PREDICTION
else:
prediction = predict(words[0], model)
f.write(prediction + "\n")
def predict(word, model):
predictions = dict(model[word])
most_common = dict(Counter(predictions).most_common(6))
total_prob = 0.0
str_prediction = ""
for word, prob in most_common.items():
total_prob += prob
str_prediction += f"{word}:{prob} "
if total_prob == 0.0:
return DEFAULT_PREDICTION
rem_prob = 1 - total_prob
if rem_prob < 0.01:
rem_prob = 0.01
str_prediction += f":{rem_prob}"
return str_prediction

33
utils.py Normal file
View File

@ -0,0 +1,33 @@
import pandas as pd
import regex as re
from csv import QUOTE_NONE
ENCODING = "utf-8"
REP = re.compile(r"[{}\[\]\&%^$*#\(\)@\t\n0123456789]+")
REM = re.compile(r"'s|[\-]\\n|\-\\n|\p{P}")
def read_csv(fname):
return pd.read_csv(
fname,
sep="\t",
on_bad_lines='skip',
header=None,
quoting=QUOTE_NONE,
encoding=ENCODING
)
def clean_text(text):
res = str(text).lower().strip()
res = res.replace("", "'")
res = REM.sub("", res)
res = REP.sub(" ", res)
res = res.replace("'s", " is")
res = res.replace("'ll", " will")
res = res.replace("won't", "will not")
res = res.replace("isn't", "is not")
res = res.replace("aren't", "are not")
res = res.replace("'ve'", "have")
return res.replace("'m", " am")