440058 plusalpha
This commit is contained in:
parent
d1010d6e98
commit
8b1c86b21e
10519
dev-0/out.tsv
Normal file
10519
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
102
run.py
Normal file
102
run.py
Normal file
@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# In[1]:
|
||||
|
||||
|
||||
from collections import defaultdict, Counter
|
||||
import csv
|
||||
import regex as re
|
||||
import pandas as pd
|
||||
from nltk import trigrams, word_tokenize
|
||||
|
||||
|
||||
# In[26]:
|
||||
|
||||
def prepare_data():
|
||||
x_train = pd.read_csv('train/in.tsv.xz', sep='\t', header=None,
|
||||
quoting=csv.QUOTE_NONE, nrows=70000, on_bad_lines='skip')
|
||||
y_train = pd.read_csv('train/expected.tsv', sep='\t', header=None,
|
||||
quoting=csv.QUOTE_NONE, nrows=70000, on_bad_lines='skip')
|
||||
x_train = x_train[[6, 7]]
|
||||
x_train = pd.concat([x_train, y_train], axis=1)
|
||||
x_train['l'] = x_train[6] + x_train[0] + x_train[7]
|
||||
|
||||
return x_train, y_train
|
||||
|
||||
|
||||
x_train, y_train = prepare_data()
|
||||
|
||||
|
||||
# In[39]:
|
||||
|
||||
|
||||
def train(x_train):
|
||||
model = defaultdict(lambda: defaultdict(lambda: 0))
|
||||
setOf = set()
|
||||
alpha = 0.02
|
||||
count = x_train.iterrows()
|
||||
for i, (_, row) in enumerate(count):
|
||||
text = re.sub(r'\p{P}', '', str(row['l']).lower().replace(
|
||||
'-\\n', '').replace('\\n', ' '))
|
||||
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:
|
||||
model[(word_1, word_3)][word_2] += 1
|
||||
setOf.add(word_1)
|
||||
setOf.add(word_2)
|
||||
setOf.add(word_3)
|
||||
|
||||
for words in model:
|
||||
num_n_grams = float(sum(model[words].values()))
|
||||
for word in model[words]:
|
||||
model[words][word] = (model[words][word] + alpha) / \
|
||||
(num_n_grams + alpha * len(setOf))
|
||||
|
||||
return model
|
||||
|
||||
|
||||
# In[41]:
|
||||
|
||||
|
||||
def predict(before, after):
|
||||
result = ''
|
||||
p = 0.0
|
||||
pred = dict(Counter(dict(model[before, after])).most_common(7))
|
||||
for key, value in pred.items():
|
||||
p += value
|
||||
result += f'{key}:{value} '
|
||||
if p == 0.0:
|
||||
result = 'to:0.02 the:0.02 be:0.02 and:0.01 or:0.01 and:0.01 a:0.01 :0.9'
|
||||
return result
|
||||
result += f':{max(1 - p, 0.01)}'
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# In[42]:
|
||||
def preprocess(text):
|
||||
text = text.lower().replace('-\\n', '').replace('\\n', ' ')
|
||||
return re.sub(r'\p{P}', '', text)
|
||||
|
||||
|
||||
def gap_predict(file):
|
||||
X_test = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', header=None,
|
||||
quoting=csv.QUOTE_NONE, error_bad_lines=False)
|
||||
with open(f'{file}/out.tsv', 'w', encoding='utf-8') as result_file:
|
||||
for _, row in X_test.iterrows():
|
||||
before, after = word_tokenize(preprocess(
|
||||
str(row[6]))), word_tokenize(preprocess(str(row[7])))
|
||||
if len(before) < 3 or len(after) < 3:
|
||||
result = 'to:0.02 the:0.02 be:0.02 and:0.01 or:0.01 and:0.01 a:0.01 :0.9'
|
||||
else:
|
||||
result = predict(before[-1], after[0])
|
||||
result_file.write(result + '\n')
|
||||
|
||||
|
||||
# In[43]:
|
||||
|
||||
|
||||
model = train(x_train)
|
||||
gap_predict('dev-0')
|
||||
gap_predict('test-A')
|
7414
test-A/out.tsv
Normal file
7414
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user