Большое обновление всего проекта

This commit is contained in:
2024-01-15 03:45:18 +03:00
parent 827674ba4c
commit 026cc9b6bb
36 changed files with 760 additions and 617 deletions
@@ -0,0 +1,26 @@
class NgramInfo:
def __init__(self, id):
self.id = id
self.frequency = 1
class NgramsDictionary:
def __init__(self, ngram_size):
self.ngrams = {}
self.ngram_size = ngram_size
self.last_id = 0
def update(self, words):
for last_word_index in range(self.ngram_size, len(words) + 1):
ngram = ""
for current_word_index in range(0, self.ngram_size):
ngram += words[last_word_index - self.ngram_size + current_word_index] + " "
self.addNgram(ngram[:-1])
def addNgram(self, ngram):
if ngram in self.ngrams:
self.ngrams[ngram].frequency += 1
else:
word_info = NgramInfo(self.last_id)
self.last_id += 1
self.ngrams[ngram] = word_info