added kenlm model solution

This commit is contained in:
Dawid 2022-04-24 22:05:42 +02:00
commit df213184ca
15 changed files with 460642 additions and 0 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

9
README.md Normal file
View File

@ -0,0 +1,9 @@
Challenging America word-gap prediction
===================================
Guess a word in a gap.
Evaluation metric
-----------------
LikelihoodHashed is the metric

16
alpha_test.md Normal file
View File

@ -0,0 +1,16 @@
## Tested on 10k rows
|alpha|result|
|---|---|
|__0.0001__|__603.35__|
|0.001|656.93|
|0.1|933.45|
|0.2|962.28|
|0.3|975.78|
|0.4|983.92|
|0.5|989.45|
|0.6|993.51|
|0.7|996.63|
|0.8|999.11|
|0.9|1001.13|
|1.0|1002.83|

1
config.txt Normal file
View File

@ -0,0 +1 @@
--metric PerplexityHashed --precision 2 --in-header in-header.tsv --out-header out-header.tsv

10519
dev-0/expected.tsv Normal file

File diff suppressed because it is too large Load Diff

BIN
dev-0/in.tsv.xz Normal file

Binary file not shown.

10519
dev-0/out.tsv Normal file

File diff suppressed because it is too large Load Diff

1
in-header.tsv Normal file
View File

@ -0,0 +1 @@
FileId Year LeftContext RightContext
1 FileId Year LeftContext RightContext

5
kenlm.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
KENLM_BUILD_PATH='/home/dawid/kenlm/build'
$KENLM_BUILD_PATH/bin/lmplz -o 3 < train_data.txt > kenlm_model.arpa
$KENLM_BUILD_PATH/bin/build_binary kenlm_model.arpa kenlm_model.binary

1
out-header.tsv Normal file
View File

@ -0,0 +1 @@
Word
1 Word

126
run.py Normal file
View File

@ -0,0 +1,126 @@
from cmath import log10
import csv
import pandas as pd
import regex as re
import os
import kenlm
from nltk import word_tokenize
from collections import Counter, defaultdict
from english_words import english_words_set
# nltk.download("punkt")
# train set
train_data = pd.read_csv(
"train/in.tsv.xz",
sep="\t",
error_bad_lines=False,
warn_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
nrows=100_000
)
# training labels
train_labels = pd.read_csv(
"train/expected.tsv",
sep="\t",
error_bad_lines=False,
warn_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
nrows=100_000
)
# dev set
dev_data = pd.read_csv(
"dev-0/in.tsv.xz",
sep="\t",
error_bad_lines=False,
warn_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
)
# test set
test_data = pd.read_csv(
"test-A/in.tsv.xz",
sep="\t",
error_bad_lines=False,
warn_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
)
def prepare_text(text):
text = text.lower().replace("-\\n", "").replace("\\n", " ")
text = re.sub(r"\p{P}", "", text)
return text
def predict(word1, word2):
predictions = []
for word in english_words_set:
sentence = word1 + ' ' + word + ' ' + word2
text_score = model.score(sentence, bos=False, eos=False)
if len(predictions) < 12:
predictions.append((word, text_score))
else:
worst_score = None
for score in predictions:
if not worst_score:
worst_score = score
else:
if worst_score[1] > score[1]:
worst_score = score
if worst_score[1] < text_score:
predictions.remove(worst_score)
predictions.append((word, text_score))
probs = sorted(predictions, key=lambda tup: tup[1], reverse=True)
pred_str = ''
for word, prob in probs:
pred_str += f'{word}:{prob} '
pred_str += f':{log10(0.99)}'
return pred_str
def write_output():
with open("dev-0/out.tsv", "w") as file:
for _, row in dev_data.iterrows():
text = prepare_text(str(row[7]))
words = word_tokenize(text)
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 = predict(words[0], words[1])
file.write(prediction + "\n")
with open("test-A/out.tsv", "w") as file:
for _, row in test_data.iterrows():
text = prepare_text(str(row[7]))
words = word_tokenize(text)
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 = predict(words[0], words[1])
file.write(prediction + "\n")
if __name__ == "__main__":
print("Preparing data...")
train_data = train_data[[6, 7]]
train_data = pd.concat([train_data, train_labels], axis=1)
train_data["final"] = train_data[6] + train_data[0] + train_data[7]
train = train_data[['final']]
with open("./train_data.txt", 'a') as f:
for _, row in train_data.iterrows():
text = prepare_text(str(row["final"]))
f.write(text + '\n')
print("Preparing model...")
os.system('sh ./kenlm.sh')
model=kenlm.Model("kenlm_model.binary")
print("Writing outputs...")
write_output()

BIN
test-A/in.tsv.xz Normal file

Binary file not shown.

7414
test-A/out.tsv Normal file

File diff suppressed because it is too large Load Diff

432022
train/expected.tsv Normal file

File diff suppressed because it is too large Load Diff

BIN
train/in.tsv.xz Normal file

Binary file not shown.