2022-04-01 11:43:28 +02:00
|
|
|
import pandas as pd
|
2022-04-02 15:26:18 +02:00
|
|
|
import csv
|
2022-04-02 17:35:49 +02:00
|
|
|
import regex as re
|
2022-04-01 11:43:28 +02:00
|
|
|
from nltk import trigrams, word_tokenize
|
|
|
|
from collections import Counter, defaultdict
|
|
|
|
|
2022-04-02 17:35:49 +02:00
|
|
|
def clean_text(text):
|
|
|
|
text = text.lower().replace('-\\n', '').replace('\\n', ' ')
|
|
|
|
text = re.sub(r'\p{P}', '', text)
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2022-04-02 15:26:18 +02:00
|
|
|
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)
|
|
|
|
train_labels = pd.read_csv('train/expected.tsv', sep='\t', error_bad_lines=False, warn_bad_lines=False, header=None, quoting=csv.QUOTE_NONE)
|
2022-04-01 11:43:28 +02:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
model = defaultdict(lambda: defaultdict(lambda: 0))
|
|
|
|
|
|
|
|
for index, row in train_data.iterrows():
|
2022-04-02 17:35:49 +02:00
|
|
|
text = clean_text(str(row['final']))
|
2022-04-01 11:43:28 +02:00
|
|
|
words = word_tokenize(text)
|
|
|
|
for w1, w2, w3 in trigrams(words, pad_right=True, pad_left=True):
|
2022-04-02 17:35:49 +02:00
|
|
|
if w1 and w2 and w3:
|
|
|
|
model[(w2, w3)][w1] += 1
|
|
|
|
if index > 20000:
|
2022-04-02 17:05:56 +02:00
|
|
|
break
|
2022-04-01 11:43:28 +02:00
|
|
|
|
|
|
|
for w2_w3 in model:
|
|
|
|
total_count = float(sum(model[w2_w3].values()))
|
|
|
|
for w1 in model[w2_w3]:
|
|
|
|
model[w2_w3][w1] /= total_count
|
|
|
|
|
|
|
|
def predict_probs(word1, word2):
|
|
|
|
raw_prediction = dict(model[word1, word2])
|
2022-04-02 17:05:56 +02:00
|
|
|
prediction = dict(Counter(raw_prediction).most_common(12))
|
2022-04-01 11:43:28 +02:00
|
|
|
|
|
|
|
total_prob = 0.0
|
|
|
|
str_prediction = ''
|
|
|
|
|
|
|
|
for word, prob in prediction.items():
|
|
|
|
total_prob += prob
|
|
|
|
str_prediction += f'{word}:{prob} '
|
|
|
|
|
2022-04-02 17:35:49 +02:00
|
|
|
if total_prob == 0.0:
|
|
|
|
return 'the:0.3 be:0.2 to:0.2 of:0.2 :0.1'
|
|
|
|
|
2022-04-01 11:43:28 +02:00
|
|
|
remaining_prob = 1 - total_prob
|
|
|
|
|
2022-04-02 17:05:56 +02:00
|
|
|
if remaining_prob < 0.0001:
|
|
|
|
remaining_prob = 0.0001
|
2022-04-01 11:43:28 +02:00
|
|
|
|
2022-04-02 17:05:56 +02:00
|
|
|
str_prediction += f':{remaining_prob}'
|
2022-04-01 11:43:28 +02:00
|
|
|
|
|
|
|
return str_prediction
|
|
|
|
|
2022-04-02 17:35:49 +02:00
|
|
|
|
2022-04-02 15:26:18 +02:00
|
|
|
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_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)
|
2022-04-01 11:43:28 +02:00
|
|
|
|
|
|
|
with open('dev-0/out.tsv', 'w') as file:
|
|
|
|
for index, row in dev_data.iterrows():
|
2022-04-02 17:35:49 +02:00
|
|
|
text = clean_text(str(row[7]))
|
2022-04-01 11:43:28 +02:00
|
|
|
words = word_tokenize(text)
|
|
|
|
if len(words) < 4:
|
2022-04-02 17:35:49 +02:00
|
|
|
prediction = 'the:0.3 be:0.2 to:0.2 of:0.2 :0.1'
|
2022-04-01 11:43:28 +02:00
|
|
|
else:
|
|
|
|
prediction = predict_probs(words[0], words[1])
|
|
|
|
file.write(prediction + '\n')
|
|
|
|
|
|
|
|
with open('test-A/out.tsv', 'w') as file:
|
|
|
|
for index, row in test_data.iterrows():
|
2022-04-02 17:35:49 +02:00
|
|
|
text = clean_text(str(row[7]))
|
2022-04-01 11:43:28 +02:00
|
|
|
words = word_tokenize(text)
|
|
|
|
if len(words) < 4:
|
2022-04-02 17:35:49 +02:00
|
|
|
prediction = 'the:0.3 be:0.2 to:0.2 of:0.2 :0.1'
|
2022-04-01 11:43:28 +02:00
|
|
|
else:
|
|
|
|
prediction = predict_probs(words[0], words[1])
|
|
|
|
file.write(prediction + '\n')
|