bigrams
This commit is contained in:
parent
a2b50f54aa
commit
cb4a1f6a4f
11628
dev-1/out.tsv
Normal file
11628
dev-1/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
14132
test-A/out.tsv
Normal file
14132
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,7 +3,7 @@ import sys
|
|||||||
from math import log
|
from math import log
|
||||||
import regex as re
|
import regex as re
|
||||||
|
|
||||||
def get_prob(bigrams, unigrams):
|
def count_prob(bigrams, unigrams):
|
||||||
prob = (bigrams + 1.0) / (unigrams + 1)
|
prob = (bigrams + 1.0) / (unigrams + 1)
|
||||||
if prob > 1.0:
|
if prob > 1.0:
|
||||||
return 1.0
|
return 1.0
|
||||||
@ -25,9 +25,9 @@ def main():
|
|||||||
pre_ngram = tuple(left_word + [word])
|
pre_ngram = tuple(left_word + [word])
|
||||||
post_ngram = tuple([word] + right_word)
|
post_ngram = tuple([word] + right_word)
|
||||||
#print(pre_ngram)
|
#print(pre_ngram)
|
||||||
pre_ngram_prob = get_prob(ngrams[2].get(pre_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
pre_ngram_prob = count_prob(ngrams[2].get(pre_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
||||||
#if pre_ngram_prob>0:
|
#if pre_ngram_prob>0:
|
||||||
post_ngram_prob = get_prob(ngrams[2].get(post_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
post_ngram_prob = count_prob(ngrams[2].get(post_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
||||||
|
|
||||||
probabilities.append((word, pre_ngram_prob * post_ngram_prob))
|
probabilities.append((word, pre_ngram_prob * post_ngram_prob))
|
||||||
probabilities = sorted(probabilities, key=lambda t: t[1], reverse=True)[:50]
|
probabilities = sorted(probabilities, key=lambda t: t[1], reverse=True)[:50]
|
||||||
|
76
train.py
76
train.py
@ -1,37 +1,57 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import regex as re
|
|
||||||
import pickle
|
import pickle
|
||||||
|
import sys
|
||||||
|
from math import log
|
||||||
|
import regex as re
|
||||||
|
|
||||||
|
def count_prob(bigrams, unigrams):
|
||||||
def into_words(sentence):
|
prob = (bigrams + 1.0) / (unigrams + 1)
|
||||||
return re.findall(r'\p{P}|[^\p{P}\s]+', sentence.lower())
|
if prob > 1.0:
|
||||||
|
return 1.0
|
||||||
|
else:
|
||||||
|
return prob
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
ngrams = {1: {}, 2: {}}
|
ngrams = pickle.load(open('ngrams_2.pkl', 'rb'))
|
||||||
lowest_ngram = 1
|
vocabulary_size = len(ngrams[1])
|
||||||
highest_ngram = 2
|
for line in sys.stdin:
|
||||||
|
words = re.findall(r'.*\t.*\t.* (.*?)\t(.*?) ', line.lower())[0]
|
||||||
|
#print(words)
|
||||||
|
left_word = [str(words[0])]
|
||||||
|
right_word = [str(words[1])]
|
||||||
|
|
||||||
|
probabilities = []
|
||||||
|
for word in ngrams[1].keys():
|
||||||
|
word = str(word[0])
|
||||||
|
pre_ngram = tuple(left_word + [word])
|
||||||
|
post_ngram = tuple([word] + right_word)
|
||||||
|
#print(pre_ngram)
|
||||||
|
pre_ngram_prob = count_prob(ngrams[2].get(pre_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
||||||
|
#if pre_ngram_prob>0:
|
||||||
|
post_ngram_prob = count_prob(ngrams[2].get(post_ngram, 0), ngrams[1].get(word[0],0) + vocabulary_size * 1000)
|
||||||
|
|
||||||
|
probabilities.append((word, pre_ngram_prob * post_ngram_prob))
|
||||||
|
probabilities = sorted(probabilities, key=lambda t: t[1], reverse=True)[:50]
|
||||||
|
probability = 1.0
|
||||||
|
text = ''
|
||||||
counter = 0
|
counter = 0
|
||||||
for line in sys.stdin: #dla kazdej linii z pliku
|
has_log_prob0 = False
|
||||||
line = line.split('\t')[4] # podziel na 4
|
for p in probabilities:
|
||||||
tokens = into_words(line) #na slowa
|
word = p[0]
|
||||||
number_of_tokens = len(tokens) #ile slow?
|
prob = p[1]
|
||||||
for n in range(lowest_ngram, highest_ngram+1): #dla kazdego ngram
|
if counter == 0 and (probability - prob <= 0.0):
|
||||||
for i in range(0, number_of_tokens-n+1): #i tyle ile jest slow -n gram + 1
|
text = word + ':' + str(log(0.95)) + ' :' + str(log(0.05))
|
||||||
ngram = tuple(tokens[i:i+n])
|
has_log_prob0 = True
|
||||||
if ngram in ngrams[n]:
|
break
|
||||||
ngrams[n][ngram] += 1
|
if counter > 0 and (probability - prob <= 0.0):
|
||||||
else:
|
text += ':' + str(log(probability))
|
||||||
ngrams[n][ngram] = 1
|
has_log_prob0 = True
|
||||||
if counter % 1000 == 0:
|
break
|
||||||
print('counter = ', counter)
|
text += word + ':' + str(log(prob)) + ' '
|
||||||
|
probability -= prob
|
||||||
counter += 1
|
counter += 1
|
||||||
ngrams[1] = dict(sorted(ngrams[1].items(), key=lambda item: ngrams[1][item[0]], reverse=True)[:1000])
|
if not has_log_prob0:
|
||||||
ngrams[2] = dict(sorted(ngrams[2].items(), key=lambda item: ngrams[2][item[0]], reverse=True)[:100000])
|
text += ':' + str(log(0.0001))
|
||||||
#ngrams[3] = dict(sorted(ngrams[3].items(), key=lambda item: ngrams[3][item[0]], reverse=True)[:120000])
|
print(text)
|
||||||
pickle.dump(ngrams, open('ngrams_2.pkl', 'wb'))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user