440058 plusalpha
This commit is contained in:
parent
32ad4fd656
commit
d1010d6e98
10519
dev-0/out.tsv
10519
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
97
run.py
97
run.py
@ -1,97 +0,0 @@
|
||||
#!/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=10000, error_bad_lines=False)
|
||||
y_train = pd.read_csv('train/expected.tsv', sep='\t', header=None,
|
||||
quoting=csv.QUOTE_NONE, nrows=10000, error_bad_lines=False)
|
||||
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))
|
||||
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
|
||||
|
||||
for i, words_1_3 in enumerate(model):
|
||||
count = sum(model[words_1_3].values())
|
||||
for word_2 in model[words_1_3]:
|
||||
model[words_1_3][word_2] += 0.25
|
||||
model[words_1_3][word_2] /= float(count + 0.25 + len(word_2))
|
||||
|
||||
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
7414
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user