This commit is contained in:
s434766 2022-04-03 20:33:17 +02:00
parent 61e88a9c8c
commit dc5a5cfe83
3 changed files with 17997 additions and 0 deletions

10519
dev-0/out.tsv Normal file

File diff suppressed because it is too large Load Diff

64
run.py Normal file
View File

@ -0,0 +1,64 @@
from collections import defaultdict, Counter
from nltk import trigrams, word_tokenize
import csv
import regex as re
import pandas as pd
in_file = 'train/in.tsv.xz'
out_file = 'train/expected.tsv'
X_train = pd.read_csv(in_file, sep='\t', header=None, quoting=csv.QUOTE_NONE, nrows=10000, error_bad_lines=False)
Y_train = pd.read_csv(out_file, sep='\t', header=None, quoting=csv.QUOTE_NONE, nrows=10000, error_bad_lines=False)
X_train = X_train[[6, 7]]
X_train = pd.concat([X_train, Y_train], axis=1)
X_train['row'] = X_train[6] + X_train[0] + X_train[7]
def train(X_train, Y_train):
model = defaultdict(lambda: defaultdict(lambda: 0))
for _, (_, row) in enumerate(X_train.iterrows()):
text = preprocess(str(row['row']))
words = word_tokenize(text)
for w1, w2, w3 in trigrams(words, pad_right=True, pad_left=True):
if w1 and w2 and w3:
model[(w1, w3)][w2] += 1
for _, w13 in enumerate(model):
count = sum(model[w13].values())
for w2 in model[w13]:
model[w13][w2] += 0.25
model[w13][w2] /= float(count + 0.25 + len(w2))
return model
def preprocess(row):
row = re.sub(r'\p{P}', '', row.lower().replace('-\\n', '').replace('\\n', ' '))
return row
def predict_word(before, after):
output = ''
p = 0.0
Y_pred = dict(Counter(dict(model[before, after])).most_common(7))
for key, value in Y_pred.items():
p += value
output += f'{key}:{value} '
if p == 0.0:
output = 'the:0.04 be:0.04 to:0.04 and:0.02 not:0.02 or:0.02 a:0.02 :0.8'
return output
output += f':{max(1 - p, 0.01)}'
return output
def word_gap_prediction(file):
X_test = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', header=None, quoting=csv.QUOTE_NONE, error_bad_lines=False)
with open(f'{file}/out.tsv', 'w', encoding='utf-8') as output_file:
for _, row in X_test.iterrows():
before, after = word_tokenize(preprocess(str(row[6]))), word_tokenize(preprocess(str(row[7])))
if len(before) < 3 or len(after) < 3:
output = 'the:0.04 be:0.04 to:0.04 and:0.02 not:0.02 or:0.02 a:0.02 :0.8'
else:
output = predict_word(before[-1], after[0])
output_file.write(output + '\n')
model = train(X_train, Y_train)
word_gap_prediction('dev-0')
word_gap_prediction('test-A')

7414
test-A/out.tsv Normal file

File diff suppressed because it is too large Load Diff