Kenlm model
This commit is contained in:
parent
dc61acc340
commit
a0c4ca1217
21038
dev-0/out.tsv
21038
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
5
kenlm.sh
Executable file
5
kenlm.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
KENLM_BUILD_PATH='/home/zary/Desktop/kenlm/build'
|
||||||
|
$KENLM_BUILD_PATH/bin/lmplz -o 3 < input_train.txt > model.arpa
|
||||||
|
$KENLM_BUILD_PATH/bin/build_binary model.arpa model.binary
|
84
run.py
84
run.py
@ -1,68 +1,74 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import csv
|
import csv
|
||||||
from collections import Counter, defaultdict
|
|
||||||
from nltk.tokenize import RegexpTokenizer
|
from nltk.tokenize import RegexpTokenizer
|
||||||
|
from english_words import english_words_set
|
||||||
from nltk import trigrams
|
from nltk import trigrams
|
||||||
|
import os
|
||||||
|
import kenlm
|
||||||
|
from math import log10
|
||||||
|
|
||||||
|
|
||||||
class WordGapPrediction:
|
class WordGapPrediction:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tokenizer = RegexpTokenizer(r"\w+")
|
self.tokenizer = RegexpTokenizer(r"\w+")
|
||||||
self.model = defaultdict(lambda: defaultdict(lambda: 0))
|
self.model = None
|
||||||
self.vocab = set()
|
self.vocab = set()
|
||||||
self.alpha = 0.001
|
self.alpha = 0.6
|
||||||
|
|
||||||
def read_train_data(self, file):
|
def read_train_data(self, file):
|
||||||
data = pd.read_csv(file, sep="\t", error_bad_lines=False, index_col=0, header=None)
|
data = pd.read_csv(file, sep="\t", error_bad_lines=False, index_col=0, header=None)
|
||||||
for index, row in data[:100000].iterrows():
|
with open('input_train.txt', 'w') as f:
|
||||||
text = str(row[6]) + ' ' + str(row[7])
|
for index, row in data[:500000].iterrows():
|
||||||
tokens = self.tokenizer.tokenize(text)
|
first_part = str(row[6])
|
||||||
for w1, w2, w3 in trigrams(tokens, pad_right=True, pad_left=True):
|
sec_part = str(row[7])
|
||||||
if w1 and w2 and w3:
|
if first_part != 'nan':
|
||||||
self.model[(w2, w3)][w1] += 1
|
f.write(first_part + '\n')
|
||||||
self.model[(w1, w2)][w3] += 1
|
if sec_part != 'nan':
|
||||||
self.vocab.add(w1)
|
f.write(sec_part + '\n')
|
||||||
self.vocab.add(w2)
|
os.system('sh ./kenlm.sh')
|
||||||
self.vocab.add(w3)
|
self.model = kenlm.Model("model.binary")
|
||||||
|
|
||||||
for word_pair in self.model:
|
|
||||||
num_n_grams = float(sum(self.model[word_pair].values()))
|
|
||||||
for word in self.model[word_pair]:
|
|
||||||
self.model[word_pair][word] = (self.model[word_pair][word] + self.alpha) / (num_n_grams + self.alpha*len(self.vocab))
|
|
||||||
|
|
||||||
def generate_outputs(self, input_file, output_file):
|
def generate_outputs(self, input_file, output_file):
|
||||||
data = pd.read_csv(input_file, sep='\t', error_bad_lines=False, index_col=0, header=None, quoting=csv.QUOTE_NONE)
|
data = pd.read_csv(input_file, sep='\t', error_bad_lines=False, index_col=0, header=None, quoting=csv.QUOTE_NONE)
|
||||||
with open(output_file, 'w') as f:
|
with open(output_file, 'w') as f:
|
||||||
for index, row in data.iterrows():
|
for index, row in data.iterrows():
|
||||||
text = str(row[7])
|
first_context = row[6]
|
||||||
tokens = self.tokenizer.tokenize(text)
|
sec_context = row[7]
|
||||||
if len(tokens) < 4:
|
first_context_tokens = self.tokenizer.tokenize(first_context)
|
||||||
|
sec_context_tokens = self.tokenizer.tokenize(sec_context)
|
||||||
|
if len(first_context_tokens) + len(sec_context_tokens) < 4:
|
||||||
prediction = 'the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1'
|
prediction = 'the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1'
|
||||||
else:
|
else:
|
||||||
prediction = word_gap_prediction.predict_probs(tokens[0], tokens[1])
|
prediction = word_gap_prediction.predict_probs(first_context_tokens[-1], sec_context_tokens[0])
|
||||||
f.write(prediction + '\n')
|
f.write(prediction + '\n')
|
||||||
|
|
||||||
def predict_probs(self, word1, word2):
|
def predict_probs(self, word1, word2):
|
||||||
predictions = dict(self.model[word1, word2])
|
|
||||||
most_common = dict(Counter(predictions).most_common(6))
|
|
||||||
|
|
||||||
total_prob = 0.0
|
predictions = []
|
||||||
str_prediction = ''
|
for word in english_words_set:
|
||||||
|
sentence = word1 + ' ' + word + ' ' + word2
|
||||||
|
text_score = self.model.score(sentence, bos=False, eos=False)
|
||||||
|
|
||||||
for word, prob in most_common.items():
|
if len(predictions) < 12:
|
||||||
total_prob += prob
|
predictions.append((word, text_score))
|
||||||
str_prediction += f'{word}:{prob} '
|
else:
|
||||||
|
worst_score = None
|
||||||
if total_prob == 0.0:
|
for score in predictions:
|
||||||
return 'the:0.2 be:0.2 to:0.2 of:0.1 and:0.1 a:0.1 :0.1'
|
if not worst_score:
|
||||||
|
worst_score = score
|
||||||
if 1 - total_prob >= 0.01:
|
else:
|
||||||
str_prediction += f":{1-total_prob}"
|
if worst_score[1] > score[1]:
|
||||||
else:
|
worst_score = score
|
||||||
str_prediction += f":0.01"
|
if worst_score[1] < text_score:
|
||||||
|
predictions.remove(worst_score)
|
||||||
return str_prediction
|
predictions.append((word, text_score))
|
||||||
|
probs = sorted(predictions, key=lambda tup: tup[1], reverse=True)
|
||||||
|
pred_str = ''
|
||||||
|
for word, prob in probs:
|
||||||
|
pred_str += f'{word}:{prob} '
|
||||||
|
pred_str += f':{log10(0.99)}'
|
||||||
|
return pred_str
|
||||||
|
|
||||||
word_gap_prediction = WordGapPrediction()
|
word_gap_prediction = WordGapPrediction()
|
||||||
word_gap_prediction.read_train_data('./train/in.tsv')
|
word_gap_prediction.read_train_data('./train/in.tsv')
|
||||||
|
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