wygladzanie
This commit is contained in:
parent
dbeb347d77
commit
bd5acac80d
21038
dev-0/out.tsv
21038
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
121
run.py
121
run.py
@ -1,80 +1,87 @@
|
|||||||
|
from collections import defaultdict, Counter
|
||||||
from nltk import trigrams, word_tokenize
|
from nltk import trigrams, word_tokenize
|
||||||
import pandas as pd
|
|
||||||
import csv
|
import csv
|
||||||
import regex as re
|
import regex as re
|
||||||
from collections import Counter, defaultdict
|
import pandas as pd
|
||||||
|
|
||||||
|
X_train = pd.read_csv(
|
||||||
train_set = pd.read_csv(
|
|
||||||
'train/in.tsv.xz',
|
'train/in.tsv.xz',
|
||||||
sep='\t',
|
sep='\t',
|
||||||
on_bad_lines='skip',
|
|
||||||
header=None,
|
header=None,
|
||||||
quoting=csv.QUOTE_NONE,
|
quoting=csv.QUOTE_NONE,
|
||||||
nrows=50000)
|
nrows=70000,
|
||||||
|
on_bad_lines='skip')
|
||||||
|
|
||||||
|
Y_train = pd.read_csv(
|
||||||
train_labels = pd.read_csv(
|
|
||||||
'train/expected.tsv',
|
'train/expected.tsv',
|
||||||
sep='\t',
|
sep='\t',
|
||||||
on_bad_lines='skip',
|
|
||||||
header=None,
|
header=None,
|
||||||
quoting=csv.QUOTE_NONE,
|
quoting=csv.QUOTE_NONE,
|
||||||
nrows=50000)
|
nrows=70000,
|
||||||
|
on_bad_lines='skip')
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
def data_preprocessing(text):
|
def preprocess(row):
|
||||||
return re.sub(r'\p{P}', '', text.lower().replace('-\\n', '').replace('\\n', ' '))
|
return re.sub(r'\p{P}', '', row.lower().replace('-\\n', '').replace('\\n', ' '))
|
||||||
|
|
||||||
|
|
||||||
def predict(before, after):
|
def train(X_train, alpha):
|
||||||
prediction = dict(Counter(dict(trigram[before, after])).most_common(5))
|
model = defaultdict(lambda: defaultdict(lambda: 0))
|
||||||
result = ''
|
vocabulary = set()
|
||||||
prob = 0.0
|
|
||||||
for key, value in prediction.items():
|
for _, (_, row) in enumerate(X_train.iterrows()):
|
||||||
prob += value
|
text = preprocess(str(row['row']))
|
||||||
result += f'{key}:{value} '
|
words = word_tokenize(text)
|
||||||
if prob == 0.0:
|
for w1, w2, w3 in trigrams(words, pad_right=True, pad_left=True):
|
||||||
return 'to:0.015 be:0.015 the:0.015 not:0.01 and:0.02 a:0.02 :0.9'
|
if w1 and w2 and w3:
|
||||||
result += f':{max(1 - prob, 0.01)}'
|
model[(w1, w3)][w2] += 1
|
||||||
return result
|
vocabulary.add(w1)
|
||||||
|
vocabulary.add(w2)
|
||||||
|
vocabulary.add(w3)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def make_prediction(file):
|
def predict_word(before, after, model):
|
||||||
data = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
|
output = ''
|
||||||
with open(f'{file}/out.tsv', 'w', encoding='utf-8') as file_out:
|
p = 0.0
|
||||||
for _, row in data.iterrows():
|
Y_pred = dict(Counter(dict(model[before, after])).most_common(7))
|
||||||
before, after = word_tokenize(data_preprocessing(str(row[6]))), word_tokenize(data_preprocessing(str(row[7])))
|
|
||||||
if len(before) < 3 or len(after) < 3:
|
for key, value in Y_pred.items():
|
||||||
prediction = 'to:0.015 be:0.015 the:0.015 not:0.01 and:0.02 a:0.02 :0.9'
|
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)}'
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
prediction = predict(before[-1], after[0])
|
output = predict_word(before[-1], after[0], model)
|
||||||
file_out.write(prediction + '\n')
|
output_file.write(output + '\n')
|
||||||
|
|
||||||
|
|
||||||
train_set = train_set[[6, 7]]
|
model = train(X_train, 0.00002)
|
||||||
train_set = pd.concat([train_set, train_labels], axis=1)
|
|
||||||
train_set['line'] = train_set[6] + train_set[0] + train_set[7]
|
|
||||||
|
|
||||||
|
prediction('dev-0', model)
|
||||||
trigram = defaultdict(lambda: defaultdict(lambda: 0))
|
prediction('test-A', model)
|
||||||
|
|
||||||
rows = train_set.iterrows()
|
|
||||||
rows_len = len(train_set)
|
|
||||||
for index, (_, row) in enumerate(rows):
|
|
||||||
text = data_preprocessing(str(row['line']))
|
|
||||||
words = word_tokenize(text)
|
|
||||||
for word_1, word_2, word_3 in trigrams(words, pad_right=True, pad_left=True):
|
|
||||||
if word_1 and word_2 and word_3:
|
|
||||||
trigram[(word_1, word_3)][word_2] += 1
|
|
||||||
|
|
||||||
model_len = len(trigram)
|
|
||||||
for index, words_1_3 in enumerate(trigram):
|
|
||||||
count = sum(trigram[words_1_3].values())
|
|
||||||
for word_2 in trigram[words_1_3]:
|
|
||||||
trigram[words_1_3][word_2] += 0.25
|
|
||||||
trigram[words_1_3][word_2] /= float(count + 0.25 + len(word_2))
|
|
||||||
|
|
||||||
|
|
||||||
make_prediction('test-A')
|
|
||||||
make_prediction('dev-0')
|
|
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