wcm
Implementation of the Incremental SPPMI algorithm.
WordContextMatrix
Bases: IWVBase
The WordContextMatrix model is a counting-based method that constructs a word-context matrix of size 𝑉 × 𝐶, where 𝑉 is the number of words contained in the vocabulary and 𝐶 is the number of contexts around the target words. Each relation between a target word and a context corresponds to a smoothed PPMI score.
Our implementation must deal with the following considerations:
- Each line of text or tweet can only be seen one time. After that, it must discard.
- The PPMI’s probabilities are calculated incrementally, which means they are saved in memory.
- The algorithm adapt the vocabulary and contexts to a streaming setting because in principle are unknown.
References
- Bravo-Marquez, F., Khanchandani, A., & Pfahringer, B. (2022). Incremental Word Vectors for Time-Evolving Sentiment Lexicon Induction. Cognitive Computation, 14(1), 425-441.
- Montiel, J., Halford, M., Mastelini, S. M., Bolmier, G., Sourty, R., Vaysse, R., ... & Bifet, A. (2021). River: machine learning for streaming data in Python.
Examples:
>>> from rivertext.models.wcm import WordContextMatrix
>>> from torch.utils.data import DataLoader
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to/tweets.txt")
>>> wcm = WordContextMatrix(5, 1, 3)
>>> dataloader = DataLoader(ts, batch_size=5)
>>> for batch in dataloader:
>>> wcm.learn_many(batch)
>>> wcm.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> wcm.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/wcm.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
__init__(vocab_size=1000000, window_size=3, context_size=500, emb_size=300, reduce_emb_dim=True, on=None, strip_accents=True, lowercase=True, preprocessor=None, tokenizer=None, ngram_range=(1, 1))
An instance of WCM class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vocab_size |
int
|
The size of the vocabulary. |
1000000
|
window_size |
int
|
The size of the window. |
3
|
context_size |
int
|
The size of the contexts. |
500
|
emb_size |
int
|
The size of the embeddings. |
300
|
reduce_emb_dim |
bool
|
, by default True |
True
|
on |
str
|
The name of the feature that contains the text to vectorize. If |
None
|
strip_accents |
bool
|
Whether or not to strip accent characters, by default True. lowercase: Whether or not to convert all characters to lowercase by default True. |
True
|
preprocessor |
Callable
|
An optional preprocessing function which overrides the
|
None
|
tokenizer |
Callable[[str], List[str]]
|
A function used to convert preprocessed text into a |
None
|
ngram_range |
Tuple[int, int]
|
The lower and upper boundary of the range n-grams to be
extracted. All values of n such that |
(1, 1)
|
Source code in rivertext/models/wcm.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
get_embeddings(idxs)
Obtain a list of embedding given by a list of indexes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idxs |
List[int]
|
List of indexes. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
List of embeddings vector. |
Source code in rivertext/models/wcm.py
250 251 252 253 254 255 256 257 258 259 260 |
|
learn_many(X, y=None, **kwargs)
Train a mini-batch of text features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
List[str]
|
A list of sentence features. |
required |
y |
A series of target values, by default None. |
None
|
Examples:
>>> from rivertext.models.wcm import WordContextMatrix
>>> from torch.utils.data import DataLoader
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to/tweets.txt")
>>> wcm = WordContextMatrix(5, 1, 3)
>>> dataloader = DataLoader(ts, batch_size=5)
>>> for batch in dataloader:
>>> wcm.learn_many(batch)
>>> wcm.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> wcm.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/wcm.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
learn_one(x, **kwargs)
Train one instance using SPMMI method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
str
|
one line of text. |
required |
Examples:
>>> from rivertext.models.wcm import WordContextMatrix
>>> from torch.utils.data import DataLoader
>>> from rivertext.utils import TweetStream
>>> ts = TweetStream("/path/to/tweets.txt")
>>> wcm = WordContextMatrix(5, 1, 3)
>>> dataloader = DataLoader(ts)
>>> for tweet in dataloader:
>>> wcm.learn_one(tweet)
>>> wcm.vocab2dict()
{'hello': [0.77816248, 0.99913448, 0.14790398],
'are': [0.86127345, 0.24901696, 0.28613529],
'you': [0.64463917, 0.9003653 , 0.26000987],
'this': [0.97007572, 0.08310498, 0.61532574],
'example': [0.74144294, 0.77877194, 0.67438642]
}
>>> wcm.transform_one('hello')
[0.77816248, 0.99913448, 0.14790398]
Source code in rivertext/models/wcm.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
reduce_vocab()
Reduce the number of words in the vocabulary.
Source code in rivertext/models/wcm.py
236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
transform_one(x)
Obtain the vector embedding of a word.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
str
|
word to obtain the embedding. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
The vector embedding of the word. |
Source code in rivertext/models/wcm.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
vocab2dict()
Converts the vocabulary in a dictionary of embeddings.
Returns:
Type | Description |
---|---|
Dict[str, np.ndarray]
|
An dict where the words are the keys, and their values are the embedding vectors. |
Source code in rivertext/models/wcm.py
303 304 305 306 307 308 309 310 311 312 313 |
|