437622
This commit is contained in:
commit
79dbf602ae
9
README.md
Normal file
9
README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
Challenging America word-gap prediction
|
||||||
|
===================================
|
||||||
|
|
||||||
|
Guess a word in a gap.
|
||||||
|
|
||||||
|
Evaluation metric
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
LikelihoodHashed is the metric
|
1
config.txt
Normal file
1
config.txt
Normal 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
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
BIN
dev-0/in.tsv.xz
Normal file
Binary file not shown.
10519
dev-0/out.tsv
Normal file
10519
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
1
in-header.tsv
Normal file
1
in-header.tsv
Normal file
@ -0,0 +1 @@
|
|||||||
|
FileId Year LeftContext RightContext
|
|
1
out-header.tsv
Normal file
1
out-header.tsv
Normal file
@ -0,0 +1 @@
|
|||||||
|
Word
|
|
65
run.py
Normal file
65
run.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
from nltk import trigrams, word_tokenize
|
||||||
|
from collections import defaultdict, Counter
|
||||||
|
import pandas as pd
|
||||||
|
import csv
|
||||||
|
import regex as re
|
||||||
|
|
||||||
|
|
||||||
|
def preprocess(text):
|
||||||
|
text = text.lower().replace('-\\n', '').replace('\\n', ' ')
|
||||||
|
return re.sub(r'\p{P}', '', text)
|
||||||
|
|
||||||
|
|
||||||
|
def predict(before, after):
|
||||||
|
prediction = dict(Counter(dict(trigram[before, after])).most_common(5))
|
||||||
|
result = ''
|
||||||
|
prob = 0.0
|
||||||
|
for key, value in prediction.items():
|
||||||
|
prob += value
|
||||||
|
result += f'{key}:{value} '
|
||||||
|
if prob == 0.0:
|
||||||
|
return 'to:0.02 be:0.02 the:0.02 or:0.01 not:0.01 and:0.01 a:0.01 :0.9'
|
||||||
|
result += f':{max(1 - prob, 0.01)}'
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def make_prediction(file):
|
||||||
|
data = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
|
||||||
|
with open(f'{file}/out.tsv', 'w', encoding='utf-8') as file_out:
|
||||||
|
for _, row in data.iterrows():
|
||||||
|
before, after = word_tokenize(preprocess(str(row[6]))), word_tokenize(preprocess(str(row[7])))
|
||||||
|
if len(before) < 3 or len(after) < 3:
|
||||||
|
prediction = 'to:0.02 be:0.02 the:0.02 or:0.01 not:0.01 and:0.01 a:0.01 :0.9'
|
||||||
|
else:
|
||||||
|
prediction = predict(before[-1], after[0])
|
||||||
|
file_out.write(prediction + '\n')
|
||||||
|
|
||||||
|
|
||||||
|
train_data = pd.read_csv('train/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=20000)
|
||||||
|
train_labels = pd.read_csv('train/expected.tsv', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=20000)
|
||||||
|
train_data = train_data[[6, 7]]
|
||||||
|
train_data = pd.concat([train_data, train_labels], axis=1)
|
||||||
|
train_data['line'] = train_data[6] + train_data[0] + train_data[7]
|
||||||
|
|
||||||
|
|
||||||
|
trigram = defaultdict(lambda: defaultdict(lambda: 0))
|
||||||
|
|
||||||
|
rows = train_data.iterrows()
|
||||||
|
rows_len = len(train_data)
|
||||||
|
for index, (_, row) in enumerate(rows):
|
||||||
|
text = preprocess(str(row['line']))
|
||||||
|
words = word_tokenize(text)
|
||||||
|
for word_1, word_2, word_3 in trigrams(words, pad_right=True, pad_left=True):
|
||||||
|
if word_1 and word_2 and word_3:
|
||||||
|
trigram[(word_1, word_3)][word_2] += 1
|
||||||
|
|
||||||
|
model_len = len(trigram)
|
||||||
|
for index, words_1_3 in enumerate(trigram):
|
||||||
|
count = sum(trigram[words_1_3].values())
|
||||||
|
for word_2 in trigram[words_1_3]:
|
||||||
|
trigram[words_1_3][word_2] += 0.25
|
||||||
|
trigram[words_1_3][word_2] /= float(count + 0.25 + len(word_2))
|
||||||
|
|
||||||
|
|
||||||
|
make_prediction('test-A')
|
||||||
|
make_prediction('dev-0')
|
BIN
test-A/in.tsv.xz
Normal file
BIN
test-A/in.tsv.xz
Normal file
Binary file not shown.
7414
test-A/out.tsv
Normal file
7414
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
432022
train/expected.tsv
Normal file
432022
train/expected.tsv
Normal file
File diff suppressed because it is too large
Load Diff
BIN
train/in.tsv.xz
Normal file
BIN
train/in.tsv.xz
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user