437622 alpha=0.1
This commit is contained in:
parent
79dbf602ae
commit
27856836a3
13326
dev-0/out.tsv
13326
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
96
run.py
96
run.py
@ -4,62 +4,70 @@ import pandas as pd
|
|||||||
import csv
|
import csv
|
||||||
import regex as re
|
import regex as re
|
||||||
|
|
||||||
|
default_pred = 'to:0.02 be:0.02 the:0.02 or:0.01 not:0.01 and:0.01 a:0.01 :0.9'
|
||||||
|
|
||||||
|
|
||||||
def preprocess(text):
|
def preprocess(text):
|
||||||
text = text.lower().replace('-\\n', '').replace('\\n', ' ')
|
text = text.lower().replace('-\\n', '').replace('\\n', ' ')
|
||||||
return re.sub(r'\p{P}', '', text)
|
return re.sub(r'\p{P}', '', text)
|
||||||
|
|
||||||
|
|
||||||
def predict(before, after):
|
class Model():
|
||||||
prediction = dict(Counter(dict(trigram[before, after])).most_common(5))
|
def __init__(self, alpha, test_file_name):
|
||||||
result = ''
|
train_data = pd.read_csv('train/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE,
|
||||||
prob = 0.0
|
nrows=20000)
|
||||||
for key, value in prediction.items():
|
train_labels = pd.read_csv('train/expected.tsv', sep='\t', on_bad_lines='skip', header=None,
|
||||||
prob += value
|
quoting=csv.QUOTE_NONE, nrows=20000)
|
||||||
result += f'{key}:{value} '
|
train_data = train_data[[6, 7]]
|
||||||
if prob == 0.0:
|
train_data = pd.concat([train_data, train_labels], axis=1)
|
||||||
return 'to:0.02 be:0.02 the:0.02 or:0.01 not:0.01 and:0.01 a:0.01 :0.9'
|
train_data['line'] = train_data[6] + train_data[0] + train_data[7]
|
||||||
result += f':{max(1 - prob, 0.01)}'
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
self.file = train_data[['line']]
|
||||||
|
self.test_file_name = test_file_name
|
||||||
|
self.alpha = alpha;
|
||||||
|
self.model = defaultdict(lambda: defaultdict(lambda: 0))
|
||||||
|
|
||||||
def make_prediction(file):
|
def train(self):
|
||||||
data = pd.read_csv(f'{file}/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
|
rows = self.file.iterrows()
|
||||||
with open(f'{file}/out.tsv', 'w', encoding='utf-8') as file_out:
|
rows_len = len(self.file)
|
||||||
for _, row in data.iterrows():
|
for index, (_, row) in enumerate(rows):
|
||||||
before, after = word_tokenize(preprocess(str(row[6]))), word_tokenize(preprocess(str(row[7])))
|
|
||||||
if len(before) < 3 or len(after) < 3:
|
|
||||||
prediction = 'to:0.02 be:0.02 the:0.02 or:0.01 not:0.01 and:0.01 a:0.01 :0.9'
|
|
||||||
else:
|
|
||||||
prediction = predict(before[-1], after[0])
|
|
||||||
file_out.write(prediction + '\n')
|
|
||||||
|
|
||||||
|
|
||||||
train_data = pd.read_csv('train/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=20000)
|
|
||||||
train_labels = pd.read_csv('train/expected.tsv', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE, nrows=20000)
|
|
||||||
train_data = train_data[[6, 7]]
|
|
||||||
train_data = pd.concat([train_data, train_labels], axis=1)
|
|
||||||
train_data['line'] = train_data[6] + train_data[0] + train_data[7]
|
|
||||||
|
|
||||||
|
|
||||||
trigram = defaultdict(lambda: defaultdict(lambda: 0))
|
|
||||||
|
|
||||||
rows = train_data.iterrows()
|
|
||||||
rows_len = len(train_data)
|
|
||||||
for index, (_, row) in enumerate(rows):
|
|
||||||
text = preprocess(str(row['line']))
|
text = preprocess(str(row['line']))
|
||||||
words = word_tokenize(text)
|
words = word_tokenize(text)
|
||||||
for word_1, word_2, word_3 in trigrams(words, pad_right=True, pad_left=True):
|
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:
|
if word_1 and word_2 and word_3:
|
||||||
trigram[(word_1, word_3)][word_2] += 1
|
self.model[(word_1, word_3)][word_2] += 1
|
||||||
|
model_len = len(self.model)
|
||||||
|
for index, words_1_3 in enumerate(self.model):
|
||||||
|
count = sum(self.model[words_1_3].values())
|
||||||
|
for word_2 in self.model[words_1_3]:
|
||||||
|
self.model[words_1_3][word_2] += self.alpha
|
||||||
|
self.model[words_1_3][word_2] /= float(count + self.alpha + len(word_2))
|
||||||
|
|
||||||
model_len = len(trigram)
|
def predict(self, before, after):
|
||||||
for index, words_1_3 in enumerate(trigram):
|
prediction = dict(Counter(dict(self.model[before, after])).most_common(5))
|
||||||
count = sum(trigram[words_1_3].values())
|
result = []
|
||||||
for word_2 in trigram[words_1_3]:
|
prob = 0.0
|
||||||
trigram[words_1_3][word_2] += 0.25
|
for key, value in prediction.items():
|
||||||
trigram[words_1_3][word_2] /= float(count + 0.25 + len(word_2))
|
prob += value
|
||||||
|
result.append(f'{key}:{value} ')
|
||||||
|
if prob == 0.0:
|
||||||
|
return default_pred
|
||||||
|
result.append(f':{max(1 - prob, 0.01)}')
|
||||||
|
return ''.join(result)
|
||||||
|
|
||||||
|
def make_prediction(self):
|
||||||
|
data = pd.read_csv(f'{self.test_file_name}/in.tsv.xz', sep='\t', on_bad_lines='skip', header=None, quoting=csv.QUOTE_NONE)
|
||||||
|
with open(f'{self.test_file_name}/out.tsv', 'w', encoding='utf-8') as file_out:
|
||||||
|
for _, row in data.iterrows():
|
||||||
|
before, after = word_tokenize(preprocess(str(row[6]))), word_tokenize(preprocess(str(row[7])))
|
||||||
|
if len(before) < 3 or len(after) < 3:
|
||||||
|
prediction = default_pred
|
||||||
|
else:
|
||||||
|
prediction = self.predict(before[-1], after[0])
|
||||||
|
file_out.write(prediction + '\n')
|
||||||
|
|
||||||
|
|
||||||
make_prediction('test-A')
|
alpha = 0.1
|
||||||
make_prediction('dev-0')
|
model = Model(alpha, 'test-A')
|
||||||
|
model.train()
|
||||||
|
model.make_prediction()
|
||||||
|
9126
test-A/out.tsv
9126
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user