medcat.vocab
Classes:
-
Vocab–Vocabulary used to store word embeddings for context similarity
Attributes:
-
WordDescriptor– -
logger–
WordDescriptor
module-attribute
WordDescriptor = TypedDict('WordDescriptor', {'vector': Optional[ndarray], 'count': int, 'index': int})
Vocab
Vocab()
Bases: AbstractSerialisable
Vocabulary used to store word embeddings for context similarity calculation. Also used by the spell checker - but not for fixing the spelling only for checking is something correct.
Properties
vocab (dict[str, WordDescriptor]):
Map from word to attributes, e.g. {'house':
{'vector':
Methods:
-
add_vec–Add vector to a word.
-
add_word–Add a word to the vocabulary
-
add_words–Adds words to the vocab from a file, the file
-
count– -
get_negative_samples–Get N negative samples.
-
get_vectors– -
inc_or_add–Add a word or increase its count.
-
inc_wc–Incraese word count by cnt.
-
init_cumsums–Initialise cumulative sums.
-
item– -
load– -
remove_all_vectors–Remove all stored vector representations.
-
remove_words_below_cnt–Remove all words with frequency below cnt.
-
reset_counts–Reset the count for all word to cnt.
-
save–Save Vocab at path.
-
update_counts–Given a list of tokens update counts for words in the vocab.
-
vec–
Attributes:
-
cum_probs(ndarray) – -
index2word(dict[int, str]) – -
vec_index2word(dict[int, str]) – -
vocab(dict[str, WordDescriptor]) –
Source code in medcat-v2/medcat/vocab.py
41 42 43 44 45 46 | |
cum_probs
instance-attribute
cum_probs: ndarray = array([])
add_vec
Add vector to a word.
Parameters:
-
(wordstr) –To which word to add the vector.
-
(vecndarray) –The vector to add.
Source code in medcat-v2/medcat/vocab.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
add_word
Add a word to the vocabulary
Parameters:
-
(wordstr) –The word to be added, it should be lemmatized and lowercased
-
(cntint, default:1) –Count of this word in your dataset (Default value = 1)
-
(vecOptional[ndarray], default:None) –The vector representation of the word (Default value = None)
-
(replacebool, default:True) –Will replace old vector representation (Default value = True)
Source code in medcat-v2/medcat/vocab.py
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 | |
add_words
Adds words to the vocab from a file, the file
is required to have the following format (vec being optional):
e.g. one line: the word house with 3 dimensional vectors house 34444 0.3232 0.123213 1.231231
Parameters:
-
(pathstr) –path to the file with words and vectors
-
(replacebool, default:True) –existing words in the vocabulary will be replaced. Defaults to True.
Source code in medcat-v2/medcat/vocab.py
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 | |
count
Source code in medcat-v2/medcat/vocab.py
281 282 | |
get_negative_samples
Get N negative samples.
Parameters:
-
(nint, default:6) –How many words to return (Default value = 6)
-
(ignore_punct_and_numbool, default:False) –Whether to ignore punctuation and numbers. Defaults to False.
Raises:
-
Exception–If no unigram table is present.
Returns:
Source code in medcat-v2/medcat/vocab.py
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 | |
get_vectors
Source code in medcat-v2/medcat/vocab.py
271 272 273 | |
inc_or_add
Add a word or increase its count.
Parameters:
-
(wordstr) –Word to be added
-
(cntint, default:1) –By how much should the count be increased, or to what should it be set if a new word. (Default value = 1)
-
(vecOptional[ndarray], default:None) –Word vector (Default value = None)
Source code in medcat-v2/medcat/vocab.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
inc_wc
Incraese word count by cnt.
Parameters:
-
(wordstr) –For which word to increase the count
-
(cntint, default:1) –By how muhc to increase the count (Default value = 1)
Source code in medcat-v2/medcat/vocab.py
98 99 100 101 102 103 104 105 106 107 | |
init_cumsums
init_cumsums() -> None
Initialise cumulative sums.
This is in place of the unigram table. But similarly to it, this approach allows generating a list of indices that match the probabilistic distribution expected as per the word counts of each word.
Source code in medcat-v2/medcat/vocab.py
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 | |
item
item(word: str) -> WordDescriptor
Source code in medcat-v2/medcat/vocab.py
284 285 | |
load
classmethod
Source code in medcat-v2/medcat/vocab.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
remove_all_vectors
remove_all_vectors() -> None
Remove all stored vector representations.
Source code in medcat-v2/medcat/vocab.py
66 67 68 69 70 71 | |
remove_words_below_cnt
Remove all words with frequency below cnt.
Parameters:
-
(cntint) –Word count limit.
Source code in medcat-v2/medcat/vocab.py
73 74 75 76 77 78 79 80 81 82 83 84 85 | |
reset_counts
Reset the count for all word to cnt.
Parameters:
-
(cntint, default:1) –New count for all words in the vocab. (Default value = 1)
Source code in medcat-v2/medcat/vocab.py
124 125 126 127 128 129 130 131 132 | |
save
save(save_path: str, serialiser: Union[str, AvailableSerialisers] = dill, overwrite: bool = False, as_zip: Union[bool, Literal['auto']] = 'auto') -> None
Save Vocab at path.
Parameters:
-
(save_pathstr) –The path to save at.
-
(serialiserUnion[str, AvailableSerialisers], default:dill) –The serialiser. Defaults to AvailableSerialisers.dill.
-
(overwritebool, default:False) –Whether to allow overwriting existing files. Defaults to False.
-
(as_zipUnion[bool, Literal['auto']], default:'auto') –Whether to serialise the CDB as a zip.
Source code in medcat-v2/medcat/vocab.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
update_counts
Given a list of tokens update counts for words in the vocab.
Parameters:
Source code in medcat-v2/medcat/vocab.py
134 135 136 137 138 139 140 141 142 143 | |