medcat.components.linking.vector_context_model
Classes:
-
ContextModel–Used to learn embeddings for concepts and calculate similarities
-
DisambPreprocessor– -
PerDocumentTokenCache–
Functions:
Attributes:
-
logger–
ContextModel
ContextModel(cui2info: dict[str, CUIInfo], name2info: dict[str, NameInfo], weighted_average_function: Callable[[int], float], vocab: Vocab, config: Linking, name_separator: str, disamb_preprocessors: list[DisambPreprocessor] = [])
Bases: AbstractSerialisable
Used to learn embeddings for concepts and calculate similarities in new documents.
Parameters:
-
(cui2infodict[str, CUIInfo]) –The CUI to info mapping.
-
(name2infodict[str, NameInfo]) –The name to info mapping.
-
(weighted_average_functionCallable[[int], float]) –The weighted average function.
-
(vocabVocab) –The vocabulary
-
(configLinking) –The config to be used
-
(name_separatorstr) –The name separator
Methods:
-
disambiguate– -
get_all_similarities– -
get_context_tokens–Get context tokens for an entity, this will skip anything that
-
get_context_vectors–Given an entity and the document it will return the context
-
similarity–Calculate the similarity between the learnt context for this CUI
-
train–Update the context representation for this CUI, given it's correct
-
train_using_negative_sampling–
Attributes:
-
config– -
cui2info– -
name2info– -
name_separator– -
vocab– -
weighted_average_function–
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
disambiguate
disambiguate(cuis: list[str], entity: MutableEntity, name: str, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> tuple[Optional[str], float]
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
275 276 277 278 279 280 281 | |
get_all_similarities
get_all_similarities(cuis: list[str], entity: MutableEntity, name: str, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> tuple[Union[list[str], list[None]], list[float], int]
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |
get_context_tokens
get_context_tokens(entity: MutableEntity, doc: MutableDocument, size: int, per_doc_valid_token_cache: PerDocumentTokenCache) -> tuple[list[MutableToken], list[MutableToken], list[MutableToken]]
Get context tokens for an entity, this will skip anything that is marked as skip in token._.to_skip
Parameters:
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document look in.
-
(sizeint) –The size of the entity.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for token validation.
Returns:
-
tuple[list[MutableToken], list[MutableToken], list[MutableToken]]–tuple[list[BaseToken], list[BaseToken], list[BaseToken]]: The tokens on the left, centre, and right.
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |
get_context_vectors
get_context_vectors(entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache, cui: Optional[str] = None) -> dict[str, ndarray]
Given an entity and the document it will return the context representation for the given entity.
Parameters:
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document to look in.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per documnet cache for token validation
-
(cuiOptional[str], default:None) –The CUI or None if not specified.
Returns:
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |
similarity
similarity(cui: str, entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> float
Calculate the similarity between the learnt context for this CUI
and the context in the given doc.
Parameters:
-
(cuistr) –The CUI.
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document to look in.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for valid tokens
Returns:
-
float(float) –The similarity.
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
train
train(cui: str, entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache, negative: bool = False, names: Union[list[str], dict] = []) -> None
Update the context representation for this CUI, given it's correct location (entity) in a document (doc).
Parameters:
-
(cuistr) –The CUI to train.
-
(entityBaseEntity) –The entity we're at.
-
(docBaseDocument) –The document within which we're working.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for token validation.
-
(negativebool, default:False) –Whether or not the example is negative. Defaults to False.
-
(nameslist[str] / dict, default:[]) –Optionally used to update the
statusof a name-cui pair in the CDB.
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
train_using_negative_sampling
train_using_negative_sampling(cui: str) -> None
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
DisambPreprocessor
Bases: Protocol
PerDocumentTokenCache
Bases: dict[MutableToken, bool]
get_lr_linking
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
424 425 426 427 428 429 430 431 432 | |
get_similarity
get_similarity(cur_vectors: dict[str, ndarray], other: dict[str, ndarray], weights: dict[str, float], cui: str, cui2info: dict[str, CUIInfo]) -> float
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
get_updated_average_confidence
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
503 504 505 | |
update_context_vectors
update_context_vectors(to_update: dict[str, ndarray], cui: str, new_vecs: dict[str, ndarray], lr: float, negative: bool) -> None
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |