test 7 version
This commit is contained in:
parent
c65a03a0ad
commit
bf943014f3
21038
dev-0/out.tsv
21038
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
69
model.py
69
model.py
@ -1,8 +1,9 @@
|
|||||||
import lzma
|
|
||||||
from nltk.tokenize import word_tokenize
|
from nltk.tokenize import word_tokenize
|
||||||
from nltk import trigrams
|
from nltk import trigrams
|
||||||
import string
|
import string
|
||||||
from collections import defaultdict, Counter
|
from collections import defaultdict, Counter
|
||||||
|
import pandas as pd
|
||||||
|
import csv
|
||||||
|
|
||||||
|
|
||||||
trigrams_list = []
|
trigrams_list = []
|
||||||
@ -10,7 +11,8 @@ model = defaultdict(lambda: defaultdict(lambda: 0))
|
|||||||
|
|
||||||
|
|
||||||
def preprocess(text):
|
def preprocess(text):
|
||||||
_text = text.lower().replace('\\n', ' ').strip()
|
_text = str(text)
|
||||||
|
_text = _text.lower().replace("-\\n", "").replace('\\n', ' ').strip()
|
||||||
for character in _text:
|
for character in _text:
|
||||||
if character not in string.ascii_lowercase + ' ':
|
if character not in string.ascii_lowercase + ' ':
|
||||||
_text = _text.replace(character, '')
|
_text = _text.replace(character, '')
|
||||||
@ -21,7 +23,6 @@ def preprocess(text):
|
|||||||
|
|
||||||
|
|
||||||
def predict(word_before, word_after):
|
def predict(word_before, word_after):
|
||||||
return 'the:1.0'
|
|
||||||
prob_list = dict(Counter(model[(word_before, word_after)]).most_common(5)).items()
|
prob_list = dict(Counter(model[(word_before, word_after)]).most_common(5)).items()
|
||||||
predictions = []
|
predictions = []
|
||||||
prob_sum = 0.0
|
prob_sum = 0.0
|
||||||
@ -31,56 +32,58 @@ def predict(word_before, word_after):
|
|||||||
if prob_sum == 0.0:
|
if prob_sum == 0.0:
|
||||||
return 'the:0:2 be:0.2 to:0.2 of:0.15 and:0.15 :0.1'
|
return 'the:0:2 be:0.2 to:0.2 of:0.15 and:0.15 :0.1'
|
||||||
elif prob_sum < 1.0:
|
elif prob_sum < 1.0:
|
||||||
predictions.append(f':{1.0 - prob_sum}')
|
predictions.append(f':{max(1 - prob_sum, 0.01)}')
|
||||||
return ' '.join(predictions)
|
return ' '.join(predictions)
|
||||||
|
|
||||||
|
|
||||||
with open('train/in.tsv', 'w', encoding='utf-8') as file:
|
file_in = pd.read_csv('train/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=200000)
|
||||||
print('dekompresja pliku')
|
file_expected = pd.read_csv('train/expected.tsv', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=200000)
|
||||||
text = lzma.open('train/in.tsv.xz').read().decode('utf-8')
|
|
||||||
file.write(text)
|
|
||||||
|
|
||||||
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.iterrows(), file_expected.iterrows())):
|
||||||
for index, (line_in, expected) in enumerate(zip(file_in, file_expected)):
|
if index % 1000 == 0:
|
||||||
if index % 1000 == 0:
|
print('zbieranie trigramów', index)
|
||||||
print('zbieranie trigramów', index)
|
before = line_in[1][6]
|
||||||
_, _, _, _, _, _, before, after = line_in.split('\t')
|
after = line_in[1][7]
|
||||||
before, expected, after = preprocess(before), preprocess(expected), preprocess(after)
|
expected = expected[1][0]
|
||||||
words = before + expected + after
|
before, expected, after = preprocess(before), preprocess(expected), preprocess(after)
|
||||||
trigrams_list += trigrams(words, pad_right=True, pad_left=True)
|
words = before + expected + after
|
||||||
length = len(trigrams_list)
|
trigrams_list += trigrams(words, pad_right=True, pad_left=True)
|
||||||
print('zbieranie trigramów:', length)
|
length = len(trigrams_list)
|
||||||
if length > 1000000:
|
|
||||||
break
|
trigrams_len = len(trigrams_list)
|
||||||
|
|
||||||
for index, trigram in enumerate(trigrams_list):
|
for index, trigram in enumerate(trigrams_list):
|
||||||
if index % 100000 == 0:
|
if index % 100000 == 0:
|
||||||
print('uczenie modelu', index)
|
print(f'uczenie modelu: {index / trigrams_len}')
|
||||||
if not trigram[0] or not trigram[1] or not trigram[2]:
|
if trigram[0] and trigram[1] and trigram[2]:
|
||||||
continue
|
model[(trigram[0], trigram[2])][trigram[1]] += 1
|
||||||
model[(trigram[0], trigram[2])][trigram[1]] += 1
|
|
||||||
if index == 999999:
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
|
model_len = len(model)
|
||||||
|
|
||||||
for index, words_1_3 in enumerate(model):
|
for index, words_1_3 in enumerate(model):
|
||||||
if index % 100000 == 0:
|
if index % 100000 == 0:
|
||||||
print('normalizacja', index)
|
print(f'normalizacja: {index / model_len}')
|
||||||
count = sum(model[words_1_3].values())
|
count = sum(model[words_1_3].values())
|
||||||
for word_2 in model[words_1_3]:
|
for word_2 in model[words_1_3]:
|
||||||
model[words_1_3][word_2] /= float(count)
|
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:
|
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:
|
||||||
print('zapisywanie test-A')
|
print('zapisywanie test-A')
|
||||||
for line_in in file_in:
|
for line_in in file_in.iterrows():
|
||||||
_, _, _, _, _, _, before, after = line_in.split('\t')
|
before = line_in[1][6]
|
||||||
|
after = line_in[1][7]
|
||||||
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
||||||
file_out.write(predict(word_before_in, word_after_in) + '\n')
|
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:
|
|
||||||
|
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:
|
||||||
print('zapisywanie dev-0')
|
print('zapisywanie dev-0')
|
||||||
for line_in in file_in:
|
for line_in in file_in.iterrows():
|
||||||
_, _, _, _, _, _, before, after = line_in.split('\t')
|
before = line_in[1][6]
|
||||||
|
after = line_in[1][7]
|
||||||
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
word_before_in, word_after_in = preprocess(before)[-1], preprocess(after)[0]
|
||||||
file_out.write(predict(word_before_in, word_after_in) + '\n')
|
file_out.write(predict(word_before_in, word_after_in) + '\n')
|
||||||
|
|
||||||
|
14828
test-A/out.tsv
14828
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user