-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocab.py
111 lines (80 loc) · 2.85 KB
/
vocab.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from river.utils import VectorDict
class Vocab:
def __init__(self, max_size: int=1_000_000):
self.max_size = max_size
self.size = 0
self.word2idx = VectorDict()
self.idx2word = VectorDict()
self.free_idxs = set()
self.counter = VectorDict()
self.first_full = False
def add(self, word: str) -> int:
if word not in self.word2idx.keys() and not self.is_full():
if not self.first_full:
word_idx = self.size
else:
word_idx = self.free_idxs.pop()
self.word2idx[word] = word_idx
self.idx2word[word_idx] = word
self.counter[word_idx] = 1
self.size += 1
if self.is_full():
self.first_full = True
return word_idx
elif word in self.word2idx.keys():
word_idx = self.word2idx[word]
self.counter[word_idx] += 1
return word_idx
def add_tokens(self, tokens: list[str]):
for token in tokens:
self.add(token)
def is_full(self) -> bool:
return self.size == self.max_size
def __len__(self) -> int:
return len(self.word2idx)
def __contains__(self, word: str) -> bool:
return word in self.word2idx
def __getitem__(self, word: str) -> int:
if word in self.word2idx:
word_idx = self.word2idx[word]
return word_idx
return -1
def delete(self, idx: int):
self.free_idxs.add(idx)
word = self.idx2word[idx]
del self.word2idx[word]
del self.idx2word[idx]
del self.counter[idx]
self.size -= 1
# class Vocab:
# def __init__(self, max_size):
# self.max_size = max_size
# self.current_size = 0
# self.word2idx = dict()
# def add(self, word):
# if word not in self.word2idx and not self.is_full():
# word_index = self.current_size
# self.word2idx[word] = word_index
# self.current_size += 1
# return word_index
# elif word in self.word2idx:
# word_index = self.word2idx[word]
# return word_index
# else:
# return -1
# def add_tokens(self, tokens):
# for token in tokens:
# self.add(token)
# def is_full(self):
# return self.current_size == self.max_size
# def is_empty(self):
# return self.current_size == 0
# def __len__(self):
# return len(self.word2idx.keys())
# def __getitem__(self, word: str):
# if word in self.word2idx:
# word_index = self.word2idx[word]
# return word_index
# return -1
# def __contains__(self, word):
# return word in self.word2idx.keys()