-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
91 lines (63 loc) · 2.16 KB
/
storage.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
import abc
from collections import defaultdict
class BaseStorage:
def __init__(self, size):
self.size = size
self.counter = 0
self.values_storage = defaultdict(int)
self.values_word = tuple(self.values_storage.keys())
def is_full(self):
return self.counter == self.size
@abc.abstractmethod
def add(self, word):
...
def __contains__(self, key):
return key in self.values_storage
def __len__(self):
return len(self.values_word)
def __repr__(self):
return self.values_word.__repr__()
def __getitem__(self, word):
return self.values_storage[word]
class Vocabulary(BaseStorage):
def __init__(self, v_size):
super().__init__(v_size)
def add(self, word_rep):
if not self.is_full():
self.values_storage[word_rep.word] = word_rep
self.counter += 1
self.values_word = tuple(self.values_storage.keys())
class Context(BaseStorage):
def __init__(self, c_size):
super().__init__(c_size)
def add(self, word):
if not self.is_full():
self.values_storage[word] = self.counter
self.counter += 1
self.values_word = tuple(self.values_storage.keys())
class WordRep:
def __init__(self, word, c_size):
self.word = word
self.c_size = c_size
self.c_counter = 0
# save counter between target and its contexts
self.contexts = defaultdict(int)
# for tracking number of tweets that appears
#self.num_tweets = 0
self.counter = 0
def is_empty(self):
return self.c_counter == 0
def is_full(self):
return self.c_counter == self.c_size
def add_context(self, context):
if not self.is_full() and context not in self.contexts:
self.c_counter += 1
self.contexts[context] += 1
elif context in self.contexts:
self.contexts[context] += 1
def __len__(self):
return len(self.contexts.keys())
def __repr__(self):
return self.word
def __getitem__(self, context):
return self.contexts[context]