2022-04-23 17:23:40 +02:00
|
|
|
from collections import defaultdict, Counter
|
2022-04-03 23:42:16 +02:00
|
|
|
from nltk import trigrams, word_tokenize
|
|
|
|
import csv
|
|
|
|
import regex as re
|
2022-04-23 17:23:40 +02:00
|
|
|
import pandas as pd
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
X_train = pd.read_csv(
|
2022-04-03 23:42:16 +02:00
|
|
|
'train/in.tsv.xz',
|
|
|
|
sep='\t',
|
|
|
|
header=None,
|
|
|
|
quoting=csv.QUOTE_NONE,
|
2022-04-23 17:23:40 +02:00
|
|
|
nrows=70000,
|
|
|
|
on_bad_lines='skip')
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
Y_train = pd.read_csv(
|
2022-04-03 23:42:16 +02:00
|
|
|
'train/expected.tsv',
|
|
|
|
sep='\t',
|
|
|
|
header=None,
|
|
|
|
quoting=csv.QUOTE_NONE,
|
2022-04-23 17:23:40 +02:00
|
|
|
nrows=70000,
|
|
|
|
on_bad_lines='skip')
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
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]
|
2022-04-03 23:42:16 +02:00
|
|
|
|
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
def preprocess(row):
|
|
|
|
return re.sub(r'\p{P}', '', row.lower().replace('-\\n', '').replace('\\n', ' '))
|
2022-04-03 23:42:16 +02:00
|
|
|
|
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
def train(X_train, alpha):
|
|
|
|
model = defaultdict(lambda: defaultdict(lambda: 0))
|
|
|
|
vocabulary = set()
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
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
|
|
|
|
vocabulary.add(w1)
|
|
|
|
vocabulary.add(w2)
|
|
|
|
vocabulary.add(w3)
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
for _, w13 in enumerate(model):
|
|
|
|
count = float(sum(model[w13].values()))
|
|
|
|
denominator = count + alpha * len(vocabulary)
|
|
|
|
for w2 in model[w13]:
|
|
|
|
nominator = model[w13][w2] + alpha
|
|
|
|
model[w13][w2] = nominator / denominator
|
|
|
|
return model
|
2022-04-03 23:42:16 +02:00
|
|
|
|
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
def predict_word(before, after, model):
|
|
|
|
output = ''
|
|
|
|
p = 0.0
|
|
|
|
Y_pred = dict(Counter(dict(model[before, after])).most_common(7))
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
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)}'
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def prediction(file, model):
|
|
|
|
X_test = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', header=None, quoting=csv.QUOTE_NONE, on_bad_lines='skip')
|
|
|
|
|
|
|
|
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) < 2 or len(after) < 2:
|
|
|
|
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], model)
|
|
|
|
output_file.write(output + '\n')
|
2022-04-03 23:42:16 +02:00
|
|
|
|
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
model = train(X_train, 0.00002)
|
2022-04-03 23:42:16 +02:00
|
|
|
|
2022-04-23 17:23:40 +02:00
|
|
|
prediction('dev-0', model)
|
|
|
|
prediction('test-A', model)
|