challenging-america-word-ga.../model.py

91 lines
3.2 KiB
Python
Raw Normal View History

2022-03-26 19:08:19 +01:00
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
2022-04-03 13:29:26 +02:00
import pandas as pd
import csv
2022-04-02 22:23:56 +02:00
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):
2022-04-03 13:29:26 +02:00
_text = str(text)
_text = _text.lower().replace("-\\n", "").replace('\\n', ' ').strip()
2022-04-02 22:23:56 +02:00
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 09:59:04 +02:00
prob_list = dict(Counter(model[(word_before, word_after)]).most_common(5)).items()
2022-04-03 01:09:06 +02:00
predictions = []
2022-04-03 09:59:04 +02:00
prob_sum = 0.0
2022-04-03 01:09:06 +02:00
for key, value in prob_list:
2022-04-03 09:59:04 +02:00
prob_sum += value
2022-04-03 01:09:06 +02:00
predictions.append(f'{key}:{value}')
2022-04-03 09:59:04 +02:00
if prob_sum == 0.0:
return 'the:0:2 be:0.2 to:0.2 of:0.15 and:0.15 :0.1'
elif prob_sum < 1.0:
2022-04-03 13:29:26 +02:00
predictions.append(f':{max(1 - prob_sum, 0.01)}')
2022-04-03 01:09:06 +02:00
return ' '.join(predictions)
2022-03-26 19:08:19 +01:00
2022-04-03 13:29:26 +02:00
file_in = pd.read_csv('train/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=200000)
file_expected = pd.read_csv('train/expected.tsv', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=200000)
for index, (line_in, expected) in enumerate(zip(file_in.iterrows(), file_expected.iterrows())):
if index % 1000 == 0:
print('zbieranie trigramów', index)
before = line_in[1][6]
after = line_in[1][7]
expected = expected[1][0]
before, expected, after = preprocess(before), preprocess(expected), preprocess(after)
words = before + expected + after
trigrams_list += trigrams(words, pad_right=True, pad_left=True)
length = len(trigrams_list)
trigrams_len = len(trigrams_list)
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 13:29:26 +02:00
print(f'uczenie modelu: {index / trigrams_len}')
if trigram[0] and trigram[1] and trigram[2]:
model[(trigram[0], trigram[2])][trigram[1]] += 1
2022-04-02 22:23:56 +02:00
2022-04-03 01:09:06 +02:00
2022-04-03 13:29:26 +02:00
model_len = len(model)
2022-04-03 01:09:06 +02:00
for index, words_1_3 in enumerate(model):
if index % 100000 == 0:
2022-04-03 13:29:26 +02:00
print(f'normalizacja: {index / model_len}')
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)
2022-04-03 13:29:26 +02:00
file_in = pd.read_csv('test-A/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
with open('test-A/out.tsv', 'w', encoding='utf-8') as file_out:
2022-04-03 01:24:36 +02:00
print('zapisywanie test-A')
2022-04-03 13:29:26 +02:00
for line_in in file_in.iterrows():
before = line_in[1][6]
after = line_in[1][7]
2022-04-03 01:24:36 +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 13:29:26 +02:00
file_in = pd.read_csv('dev-0/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
with open('dev-0/out.tsv', 'w', encoding='utf-8') as file_out:
2022-04-03 01:24:36 +02:00
print('zapisywanie dev-0')
2022-04-03 13:29:26 +02:00
for line_in in file_in.iterrows():
before = line_in[1][6]
after = line_in[1][7]
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')