Spaces:
Running
Running
File size: 10,682 Bytes
73c6377 |
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 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 |
"""
Medical Terminology Module for HBV (Hepatitis B Virus)
This module provides intelligent handling of HBV medical linguistic variability including:
- Synonyms and alternate terms
- Abbreviations and acronyms (with context awareness)
- Regional spelling variations (US/UK/International)
- Specialty-specific terminology
- Dynamic learning from corpus
"""
import re
import json
from typing import List, Dict, Set, Tuple, Optional
from collections import defaultdict
from pathlib import Path
from .config import logger
# ============================================================================
# CORE HBV MEDICAL TERMINOLOGY MAPPINGS
# ============================================================================
# Common HBV medical abbreviations with context-aware expansions
MEDICAL_ABBREVIATIONS = {
# HBV Terminology
"hbv": ["hepatitis b virus", "hepatitis b"],
"hbsag": ["hepatitis b surface antigen", "hbs antigen"],
"hbeag": ["hepatitis b e antigen", "hbe antigen"],
"hbcag": ["hepatitis b core antigen"],
"anti-hbs": ["antibody to hepatitis b surface antigen", "anti-hbs antibody"],
"anti-hbe": ["antibody to hepatitis b e antigen"],
"anti-hbc": ["antibody to hepatitis b core antigen"],
"hbv dna": ["hepatitis b virus dna", "hbv viral load"],
# Liver Disease Terms
"alt": ["alanine aminotransferase", "alanine transaminase", "sgpt"],
"ast": ["aspartate aminotransferase", "aspartate transaminase", "sgot"],
"alp": ["alkaline phosphatase"],
"ggt": ["gamma-glutamyl transferase", "gamma glutamyl transpeptidase"],
"inr": ["international normalized ratio"],
"pt": ["prothrombin time"],
"apri": ["ast to platelet ratio index"],
"fib-4": ["fibrosis-4 index"],
# Fibrosis Staging
"f0": ["no fibrosis"],
"f1": ["mild fibrosis", "portal fibrosis"],
"f2": ["moderate fibrosis"],
"f3": ["severe fibrosis", "advanced fibrosis"],
"f4": ["cirrhosis", "liver cirrhosis"],
# Necroinflammatory Activity
"a0": ["no activity"],
"a1": ["mild activity"],
"a2": ["moderate activity"],
"a3": ["severe activity"],
# Treatment Terms
"etv": ["entecavir"],
"tdf": ["tenofovir disoproxil fumarate", "tenofovir df"],
"taf": ["tenofovir alafenamide"],
"lam": ["lamivudine", "3tc"],
"adv": ["adefovir", "adefovir dipivoxil"],
"ldv": ["telbivudine"],
"peg-ifn": ["pegylated interferon", "peginterferon"],
"ifn": ["interferon"],
# Complications
"hcc": ["hepatocellular carcinoma", "liver cancer"],
"dc": ["decompensated cirrhosis"],
"cc": ["compensated cirrhosis"],
"esld": ["end-stage liver disease"],
"alf": ["acute liver failure"],
"aclf": ["acute-on-chronic liver failure"],
# Coinfections
"hiv": ["human immunodeficiency virus"],
"hcv": ["hepatitis c virus", "hepatitis c"],
"hdv": ["hepatitis d virus", "hepatitis delta"],
"hav": ["hepatitis a virus", "hepatitis a"],
# Clinical Terms
"uln": ["upper limit of normal"],
"iu/ml": ["international units per milliliter"],
"log": ["logarithm", "log10"],
"svr": ["sustained virological response"],
"vr": ["virological response"],
"br": ["biochemical response"],
"sr": ["serological response"],
}
# Synonym mappings for HBV medical terms
MEDICAL_SYNONYMS = {
# HBV terminology
"hepatitis b": ["hbv", "hepatitis b virus", "hep b", "hbv infection"],
"chronic hepatitis b": ["chb", "chronic hbv", "chronic hbv infection"],
"acute hepatitis b": ["ahb", "acute hbv"],
"hbv dna": ["viral load", "hbv viral load", "serum hbv dna"],
# Serological markers
"hbsag positive": ["hbsag+", "hbs antigen positive"],
"hbeag positive": ["hbeag+", "hbe antigen positive"],
"hbsag negative": ["hbsag-", "hbs antigen negative"],
"hbeag negative": ["hbeag-", "hbe antigen negative"],
# Liver disease stages
"cirrhosis": ["f4", "liver cirrhosis", "hepatic cirrhosis"],
"fibrosis": ["liver fibrosis", "hepatic fibrosis"],
"compensated cirrhosis": ["cc", "child-pugh a", "child-pugh b"],
"decompensated cirrhosis": ["dc", "child-pugh c"],
# Treatment terms
"antiviral therapy": ["antiviral treatment", "nucleos(t)ide analogue", "na therapy"],
"entecavir": ["etv", "baraclude"],
"tenofovir": ["tdf", "taf", "viread", "vemlidy"],
"interferon": ["ifn", "pegylated interferon", "peg-ifn"],
# Clinical outcomes
"treatment response": ["virological response", "biochemical response"],
"viral suppression": ["undetectable hbv dna", "hbv dna < lloq"],
"alt normalization": ["alt normal", "alt within normal limits"],
# Complications
"hepatocellular carcinoma": ["hcc", "liver cancer", "primary liver cancer"],
"liver failure": ["hepatic failure", "end-stage liver disease", "esld"],
"portal hypertension": ["esophageal varices", "ascites", "splenomegaly"],
# Special populations
"pregnant women": ["pregnancy", "pregnant patients"],
"immunosuppressed": ["immunocompromised", "on immunosuppression"],
"coinfection": ["co-infection", "dual infection"],
}
# Regional spelling variations (US/UK/International)
SPELLING_VARIATIONS = {
"fibrosis": ["fibrosis"],
"cirrhosis": ["cirrhosis"],
"anaemia": ["anemia"],
"haemorrhage": ["hemorrhage"],
"oesophageal": ["esophageal"],
}
# Context-specific term preferences
CONTEXT_PREFERENCES = {
"treatment": ["antiviral", "therapy", "regimen", "medication"],
"diagnosis": ["hbsag", "hbeag", "hbv dna", "serology"],
"monitoring": ["alt", "hbv dna", "liver function", "fibrosis"],
"complications": ["hcc", "cirrhosis", "decompensation", "liver failure"],
}
# ============================================================================
# DYNAMIC TERMINOLOGY LEARNING
# ============================================================================
class MedicalTerminologyExpander:
"""
Dynamically learns and expands medical terminology from corpus.
Handles abbreviations, synonyms, and context-specific variations for HBV.
"""
def __init__(self, corpus_path: Optional[Path] = None):
"""Initialize with optional corpus for dynamic learning."""
self.abbreviations = MEDICAL_ABBREVIATIONS.copy()
self.synonyms = MEDICAL_SYNONYMS.copy()
self.spelling_vars = SPELLING_VARIATIONS.copy()
self.learned_terms = defaultdict(set)
if corpus_path and corpus_path.exists():
self._learn_from_corpus(corpus_path)
def expand_query(self, query: str, context: Optional[str] = None) -> List[str]:
"""
Expand a query with medical synonyms and abbreviations.
Args:
query: Original query string
context: Optional context hint (e.g., 'treatment', 'diagnosis')
Returns:
List of expanded query variations
"""
expansions = [query]
query_lower = query.lower()
# Expand abbreviations
for abbrev, full_forms in self.abbreviations.items():
if abbrev in query_lower:
for full_form in full_forms:
expansions.append(query_lower.replace(abbrev, full_form))
# Expand synonyms
for term, synonyms in self.synonyms.items():
if term in query_lower:
for synonym in synonyms:
expansions.append(query_lower.replace(term, synonym))
# Add context-specific preferences
if context and context in CONTEXT_PREFERENCES:
for pref_term in CONTEXT_PREFERENCES[context]:
if pref_term not in query_lower:
expansions.append(f"{query} {pref_term}")
# Remove duplicates while preserving order
seen = set()
unique_expansions = []
for exp in expansions:
if exp not in seen:
seen.add(exp)
unique_expansions.append(exp)
return unique_expansions
def normalize_term(self, term: str) -> str:
"""
Normalize a medical term to its canonical form.
Args:
term: Medical term to normalize
Returns:
Normalized canonical form
"""
term_lower = term.lower().strip()
# Check if it's an abbreviation
if term_lower in self.abbreviations:
return self.abbreviations[term_lower][0]
# Check if it's a synonym
for canonical, synonyms in self.synonyms.items():
if term_lower in synonyms or term_lower == canonical:
return canonical
# Check spelling variations
for canonical, variations in self.spelling_vars.items():
if term_lower in variations:
return canonical
return term
def _learn_from_corpus(self, corpus_path: Path):
"""Learn new terminology patterns from corpus."""
try:
# Implementation for dynamic learning from HBV guidelines
logger.info(f"Learning terminology from corpus: {corpus_path}")
# This would analyze the corpus and extract new term relationships
except Exception as e:
logger.warning(f"Could not learn from corpus: {e}")
def get_related_terms(self, term: str, max_terms: int = 5) -> List[str]:
"""
Get related medical terms for a given term.
Args:
term: Medical term
max_terms: Maximum number of related terms to return
Returns:
List of related terms
"""
related = set()
term_lower = term.lower()
# Find synonyms
for canonical, synonyms in self.synonyms.items():
if term_lower == canonical or term_lower in synonyms:
related.update(synonyms)
related.add(canonical)
# Find abbreviations
if term_lower in self.abbreviations:
related.update(self.abbreviations[term_lower])
# Remove the original term
related.discard(term_lower)
return list(related)[:max_terms]
# Global instance for easy access
_global_expander = None
def get_terminology_expander() -> MedicalTerminologyExpander:
"""Get or create the global terminology expander instance."""
global _global_expander
if _global_expander is None:
_global_expander = MedicalTerminologyExpander()
return _global_expander
|