-
Notifications
You must be signed in to change notification settings - Fork 0
/
unigram_table.py
32 lines (28 loc) · 1.04 KB
/
unigram_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
import numpy as np
class UnigramTable:
def __init__(self, max_size) -> None:
self.max_size = max_size
self.current_size = 0
self.table = np.zeros(int(self.max_size))
self.weight_sum = 0
def sample(self, rand):
rand_num = int(rand.uniform(0, self.current_size))
output = self.table[rand_num]
return output
def update(self, word_index, weight, rand):
self.weight_sum += weight
if self.current_size < self.max_size:
#print(weight)
new_size = min(rand.round(weight) + self.current_size, self.max_size)
#print(new_size)
#print(self.current_size, new_size)
for i in range(self.current_size, new_size):
self.table[i] = word_index
self.current_size = new_size
#print(self.current_size)
else:
n = rand.round(weight / self.weight_sum) * self.max_size
#print(f"n {n}")
for i in range(n):
self.table[i] = word_index