2022-03-26 19:08:19 +01:00
|
|
|
import lzma
|
|
|
|
from nltk.tokenize import word_tokenize
|
2022-04-02 22:23:56 +02:00
|
|
|
from nltk import trigrams
|
|
|
|
import string
|
|
|
|
from collections import defaultdict, Counter
|
|
|
|
|
|
|
|
|
|
|
|
trigrams_list = []
|
|
|
|
model = defaultdict(lambda: defaultdict(lambda: 0))
|
2022-03-26 19:08:19 +01:00
|
|
|
|
|
|
|
|
2022-04-02 22:23:56 +02:00
|
|
|
def preprocess(text):
|
|
|
|
_text = text.lower().replace('\\n', ' ').strip()
|
|
|
|
for character in _text:
|
|
|
|
if character not in string.ascii_lowercase + ' ':
|
|
|
|
_text = _text.replace(character, '')
|
2022-04-03 01:24:36 +02:00
|
|
|
words = word_tokenize(_text)
|
|
|
|
if len(words):
|
|
|
|
return words
|
|
|
|
return ['']
|
2022-03-26 19:08:19 +01:00
|
|
|
|
2022-04-02 22:23:56 +02:00
|
|
|
|
|
|
|
def predict(word_before, word_after):
|
2022-04-03 01:09:06 +02:00
|
|
|
prob_list = model[(word_before, word_after)].items()
|
|
|
|
predictions = []
|
|
|
|
total = 0.0
|
|
|
|
for key, value in prob_list:
|
|
|
|
total += value
|
|
|
|
predictions.append(f'{key}:{value}')
|
|
|
|
if total == 0.0:
|
|
|
|
return 'the:1.0'
|
|
|
|
return ' '.join(predictions)
|
2022-03-26 19:08:19 +01:00
|
|
|
|
|
|
|
|
2022-04-02 22:23:56 +02:00
|
|
|
with open('train/in.tsv', 'w', encoding='utf-8') as file:
|
2022-04-03 01:09:06 +02:00
|
|
|
print('dekompresja pliku')
|
2022-04-02 22:23:56 +02:00
|
|
|
text = lzma.open('train/in.tsv.xz').read().decode('utf-8')
|
2022-03-26 19:08:19 +01:00
|
|
|
file.write(text)
|
|
|
|
|
2022-04-02 22:23:56 +02:00
|
|
|
with open('train/in.tsv', encoding='utf-8') as file_in, open('train/expected.tsv', encoding='utf-8') as file_expected:
|
|
|
|
for index, (line_in, expected) in enumerate(zip(file_in, file_expected)):
|
|
|
|
if index % 1000 == 0:
|
2022-04-03 01:09:06 +02:00
|
|
|
print('zbieranie trigramów', index)
|
2022-04-02 22:23:56 +02:00
|
|
|
_, _, _, _, _, _, before, after = line_in.split('\t')
|
|
|
|
before, expected, after = preprocess(before), preprocess(expected), preprocess(after)
|
|
|
|
words = before + expected + after
|
|
|
|
trigrams_list += trigrams(words, pad_right=True, pad_left=True)
|
2022-04-03 01:24:36 +02:00
|
|
|
length = len(trigrams_list)
|
|
|
|
print('zbieranie trigramów:', length)
|
|
|
|
if length > 1000000:
|
|
|
|
break
|
2022-04-02 22:23:56 +02:00
|
|
|
|
2022-04-03 01:09:06 +02:00
|
|
|
for index, trigram in enumerate(trigrams_list):
|
|
|
|
if index % 100000 == 0:
|
2022-04-03 01:24:36 +02:00
|
|
|
print('uczenie modelu', index)
|
2022-04-02 22:23:56 +02:00
|
|
|
if not trigram[0] or not trigram[1] or not trigram[2]:
|
|
|
|
continue
|
|
|
|
model[(trigram[0], trigram[2])][trigram[1]] += 1
|
2022-04-03 01:09:06 +02:00
|
|
|
if index == 999999:
|
|
|
|
break
|
2022-04-02 22:23:56 +02:00
|
|
|
|
2022-04-03 01:09:06 +02:00
|
|
|
|
|
|
|
for index, words_1_3 in enumerate(model):
|
|
|
|
if index % 100000 == 0:
|
|
|
|
print('normalizacja', index)
|
2022-04-02 22:23:56 +02:00
|
|
|
count = sum(model[words_1_3].values())
|
|
|
|
for word_2 in model[words_1_3]:
|
|
|
|
model[words_1_3][word_2] /= float(count)
|
|
|
|
|
|
|
|
with open('test-A/in.tsv', encoding='utf-8') as file_in, open('test-A/out.tsv', 'w', encoding='utf-8') as file_out:
|
2022-04-03 01:24:36 +02:00
|
|
|
print('zapisywanie test-A')
|
|
|
|
for line_in in file_in:
|
|
|
|
_, _, _, _, _, _, before, after = line_in.split('\t')
|
|
|
|
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
|
|
|
file_out.write(predict(word_before_in, word_after_in) + '\n')
|
|
|
|
|
|
|
|
with open('dev-0/in.tsv', encoding='utf-8') as file_in, open('dev-0/out.tsv', 'w', encoding='utf-8') as file_out:
|
|
|
|
print('zapisywanie dev-0')
|
2022-03-26 19:08:19 +01:00
|
|
|
for line_in in file_in:
|
|
|
|
_, _, _, _, _, _, before, after = line_in.split('\t')
|
2022-04-02 22:23:56 +02:00
|
|
|
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
|
|
|
file_out.write(predict(word_before_in, word_after_in) + '\n')
|
2022-04-03 01:09:06 +02:00
|
|
|
|
|
|
|
print('koniec')
|