Update engine/parser_rules.py
Browse files- engine/parser_rules.py +717 -694
engine/parser_rules.py
CHANGED
|
@@ -1,699 +1,722 @@
|
|
| 1 |
-
# engine/parser_rules.py
|
| 2 |
-
# ------------------------------------------------------------
|
| 3 |
-
# Rule-based core parser for microbiology descriptions.
|
| 4 |
-
#
|
| 5 |
-
# Stage
|
| 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 |
-
seg
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
#
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
_set_if_stronger(parsed, "
|
| 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 |
-
_set_if_stronger(parsed, "Haemolysis Type", "
|
| 192 |
-
_set_if_stronger(parsed, "Haemolysis", "Negative")
|
| 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 |
-
except Exception:
|
| 266 |
-
pass
|
| 267 |
-
|
| 268 |
-
# explicit negative phrasing: "does not grow in 7% NaCl"
|
| 269 |
if re.search(
|
| 270 |
r"does (not|n't) grow\s+(in|at)\s*(\d+(?:\.\d+)?)\s*%?\s*nacl",
|
| 271 |
text_lc,
|
| 272 |
):
|
| 273 |
-
_set_if_stronger(parsed,
|
| 274 |
-
|
| 275 |
-
#
|
| 276 |
-
for m in re.finditer(
|
| 277 |
-
r"\bin\s*(\d+(?:\.\d+)?)\s*%?\s*nacl",
|
| 278 |
-
text_lc,
|
| 279 |
-
):
|
| 280 |
-
try:
|
| 281 |
-
conc = float(m.group(1))
|
| 282 |
-
if conc >= 6.0 and "does not" not in text_lc and "no growth" not in text_lc:
|
| 283 |
-
_set_if_stronger(parsed,
|
| 284 |
-
except Exception:
|
| 285 |
-
pass
|
| 286 |
-
|
| 287 |
-
# Nitrate: "reduces nitrate" / "does not reduce nitrate"
|
| 288 |
-
if re.search(r"reduces nitrate", text_lc):
|
| 289 |
-
_set_if_stronger(parsed, "Nitrate Reduction", "Positive")
|
| 290 |
-
if re.search(r"does (not|n't) reduce nitrate", text_lc):
|
| 291 |
-
_set_if_stronger(parsed, "Nitrate Reduction", "Negative")
|
| 292 |
-
|
| 293 |
-
# H2S: "produces H2S", "H2S production", "does not produce H2S"
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
# ------
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
if (
|
| 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 |
-
|
| 380 |
-
|
| 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 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
""
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
if
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 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 |
-
|
| 462 |
-
|
| 463 |
-
|
| 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 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
if
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
#
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
}
|
|
|
|
| 1 |
+
# engine/parser_rules.py
|
| 2 |
+
# ------------------------------------------------------------
|
| 3 |
+
# Rule-based core parser for microbiology descriptions.
|
| 4 |
+
#
|
| 5 |
+
# Stage 11F (Option A ranges + fixes):
|
| 6 |
+
# - Always store Growth Temperature as "low//high"
|
| 7 |
+
# • single: 37 → "37//37"
|
| 8 |
+
# • two temps in text: min//max (e.g. "4 °C but not 45 °C" → "4//45")
|
| 9 |
+
# - DNase robust parsing (DNase / DNase test, DNase activity, etc.)
|
| 10 |
+
# - Non–spore-forming → Spore Formation = Negative (regex + early return)
|
| 11 |
+
# - "non-H2S producing" → H2S = Negative
|
| 12 |
+
# - "aerobically" / "anaerobically" → Aerobic / Anaerobic
|
| 13 |
+
# - NaCl tolerance phrases improved
|
| 14 |
+
# - Colony morphology from "colonies dry, white and irregular on nutrient agar"
|
| 15 |
+
# ------------------------------------------------------------
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import re
|
| 20 |
+
from typing import Dict, Any, List
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
UNKNOWN = "Unknown"
|
| 24 |
+
|
| 25 |
+
# ------------------------------------------------------------
|
| 26 |
+
# Core fields and sugar mapping
|
| 27 |
+
# ------------------------------------------------------------
|
| 28 |
+
|
| 29 |
+
# Sugar name → core DB column
|
| 30 |
+
SUGAR_FIELDS: Dict[str, str] = {
|
| 31 |
+
"glucose": "Glucose Fermentation",
|
| 32 |
+
"lactose": "Lactose Fermentation",
|
| 33 |
+
"sucrose": "Sucrose Fermentation",
|
| 34 |
+
"maltose": "Maltose Fermentation",
|
| 35 |
+
"mannitol": "Mannitol Fermentation",
|
| 36 |
+
"sorbitol": "Sorbitol Fermentation",
|
| 37 |
+
"xylose": "Xylose Fermentation",
|
| 38 |
+
"rhamnose": "Rhamnose Fermentation",
|
| 39 |
+
"arabinose": "Arabinose Fermentation",
|
| 40 |
+
"raffinose": "Raffinose Fermentation",
|
| 41 |
+
"trehalose": "Trehalose Fermentation",
|
| 42 |
+
"inositol": "Inositol Fermentation",
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
CORE_BOOL_FIELDS: Dict[str, List[str]] = {
|
| 46 |
+
# field: [keywords to recognise the test name]
|
| 47 |
+
"Catalase": ["catalase"],
|
| 48 |
+
"Oxidase": ["oxidase"],
|
| 49 |
+
"Indole": ["indole"],
|
| 50 |
+
"Urease": ["urease"],
|
| 51 |
+
"Citrate": ["citrate"],
|
| 52 |
+
# MR: include "mr"
|
| 53 |
+
"Methyl Red": ["methyl red", "mr test", "mr"],
|
| 54 |
+
"VP": ["voges-proskauer", "vp test", "vp"],
|
| 55 |
+
# H2S (includes H₂S → normalised to H2S in _clean_text)
|
| 56 |
+
"H2S": ["h2s", "hydrogen sulfide"],
|
| 57 |
+
# DNase: broaden patterns
|
| 58 |
+
"DNase": [
|
| 59 |
+
"dnase",
|
| 60 |
+
"dnase test",
|
| 61 |
+
"dnase activity",
|
| 62 |
+
"dnase production",
|
| 63 |
+
"dnaase",
|
| 64 |
+
"dna hydrolysis",
|
| 65 |
+
],
|
| 66 |
+
"ONPG": ["onpg"],
|
| 67 |
+
"Coagulase": ["coagulase"],
|
| 68 |
+
"Lipase Test": ["lipase"],
|
| 69 |
+
"Nitrate Reduction": ["nitrate reduction", "nitrate"],
|
| 70 |
+
"NaCl Tolerant (>=6%)": ["6% nacl", "7% nacl", "nacl tolerant"],
|
| 71 |
+
"Lysine Decarboxylase": ["lysine decarboxylase", "lysine decarb"],
|
| 72 |
+
"Ornitihine Decarboxylase": ["ornithine decarboxylase", "ornithine decarb"],
|
| 73 |
+
"Arginine dihydrolase": ["arginine dihydrolase"],
|
| 74 |
+
"Gelatin Hydrolysis": ["gelatin hydrolysis", "gelatinase"],
|
| 75 |
+
# Esculin Hydrolysis: also match plain "esculin"
|
| 76 |
+
"Esculin Hydrolysis": ["esculin hydrolysis", "esculin"],
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# ------------------------------------------------------------
|
| 80 |
+
# Generic helpers
|
| 81 |
+
# ------------------------------------------------------------
|
| 82 |
+
|
| 83 |
+
def _clean_text(text: str) -> str:
|
| 84 |
+
"""
|
| 85 |
+
Normalise a few unicode oddities and collapse whitespace.
|
| 86 |
+
Also:
|
| 87 |
+
- strip degree symbols
|
| 88 |
+
- normalise subscript ₂ → 2 for H₂S
|
| 89 |
+
"""
|
| 90 |
+
if not text:
|
| 91 |
+
return ""
|
| 92 |
+
s = text.replace("°", "").replace("º", "")
|
| 93 |
+
# normalise subscript 2 (H₂S → H2S)
|
| 94 |
+
s = s.replace("₂", "2")
|
| 95 |
+
# keep dashes as-is; regexes handle - and – explicitly
|
| 96 |
+
return " ".join(s.split())
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _norm(s: str) -> str:
|
| 100 |
+
return s.strip().lower()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _set_if_stronger(parsed: Dict[str, str], field: str, value: str) -> None:
|
| 104 |
+
"""
|
| 105 |
+
Write value to parsed[field] if:
|
| 106 |
+
- field not present, or
|
| 107 |
+
- we are replacing Unknown with a concrete value
|
| 108 |
+
"""
|
| 109 |
+
if not value:
|
| 110 |
+
return
|
| 111 |
+
if field not in parsed or parsed[field] == UNKNOWN:
|
| 112 |
+
parsed[field] = value
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _value_from_pnv_context(segment: str) -> str | None:
|
| 116 |
+
"""
|
| 117 |
+
Interpret a short phrase as Positive / Negative / Variable.
|
| 118 |
+
"""
|
| 119 |
+
seg = _norm(segment)
|
| 120 |
+
if seg in ["positive", "pos", "+"]:
|
| 121 |
+
return "Positive"
|
| 122 |
+
if seg in ["negative", "neg", "-"]:
|
| 123 |
+
return "Negative"
|
| 124 |
+
if seg in ["variable", "var", "v"]:
|
| 125 |
+
return "Variable"
|
| 126 |
+
return None
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# ------------------------------------------------------------
|
| 130 |
+
# Gram stain and shape
|
| 131 |
+
# ------------------------------------------------------------
|
| 132 |
+
|
| 133 |
+
def _parse_gram_and_shape(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 134 |
+
# Gram stain
|
| 135 |
+
if "gram-positive" in text_lc or "gram positive" in text_lc:
|
| 136 |
+
_set_if_stronger(parsed, "Gram Stain", "Positive")
|
| 137 |
+
elif "gram-negative" in text_lc or "gram negative" in text_lc:
|
| 138 |
+
_set_if_stronger(parsed, "Gram Stain", "Negative")
|
| 139 |
+
elif "gram variable" in text_lc:
|
| 140 |
+
_set_if_stronger(parsed, "Gram Stain", "Variable")
|
| 141 |
+
|
| 142 |
+
# Shape
|
| 143 |
+
# Prefer "short rods" over generic rods
|
| 144 |
+
if "short rods" in text_lc:
|
| 145 |
+
_set_if_stronger(parsed, "Shape", "Short Rods")
|
| 146 |
+
|
| 147 |
+
# Cocci and variants (diplococci, tetracocci, etc.)
|
| 148 |
+
if re.search(r"\bcocci\b", text_lc):
|
| 149 |
+
_set_if_stronger(parsed, "Shape", "Cocci")
|
| 150 |
+
if re.search(r"\b(diplococci|tetracocci|streptococci|staphylococci)\b", text_lc):
|
| 151 |
+
_set_if_stronger(parsed, "Shape", "Cocci")
|
| 152 |
+
|
| 153 |
+
# Rods / bacilli
|
| 154 |
+
if re.search(r"\brods?\b", text_lc) or "bacilli" in text_lc:
|
| 155 |
+
_set_if_stronger(parsed, "Shape", "Rods")
|
| 156 |
+
|
| 157 |
+
# Spiral
|
| 158 |
+
if "spiral" in text_lc or "spirochete" in text_lc:
|
| 159 |
+
_set_if_stronger(parsed, "Shape", "Spiral")
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# ------------------------------------------------------------
|
| 163 |
+
# Haemolysis
|
| 164 |
+
# ------------------------------------------------------------
|
| 165 |
+
|
| 166 |
+
def _parse_haemolysis(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 167 |
+
"""
|
| 168 |
+
Handle haemolysis phrasing:
|
| 169 |
+
- beta-haemolytic / beta hemolytic / beta-haemolysis / etc.
|
| 170 |
+
- alpha- / gamma- / non-haemolytic
|
| 171 |
+
"""
|
| 172 |
+
# Beta
|
| 173 |
+
if re.search(r"beta[- ]?(haemolytic|hemolytic|haemolysis|hemolysis)", text_lc):
|
| 174 |
+
_set_if_stronger(parsed, "Haemolysis Type", "Beta")
|
| 175 |
+
_set_if_stronger(parsed, "Haemolysis", "Positive")
|
| 176 |
+
|
| 177 |
+
# Alpha
|
| 178 |
+
if re.search(r"alpha[- ]?(haemolytic|hemolytic|haemolysis|hemolysis)", text_lc):
|
| 179 |
+
_set_if_stronger(parsed, "Haemolysis Type", "Alpha")
|
| 180 |
+
_set_if_stronger(parsed, "Haemolysis", "Positive")
|
| 181 |
+
|
| 182 |
+
# Gamma / non-haemolytic
|
| 183 |
+
if re.search(r"gamma[- ]?(haemolytic|hemolytic|haemolysis|hemolysis)", text_lc):
|
| 184 |
+
_set_if_stronger(parsed, "Haemolysis Type", "Gamma")
|
| 185 |
+
_set_if_stronger(parsed, "Haemolysis", "Negative")
|
| 186 |
+
if (
|
| 187 |
+
"non-haemolytic" in text_lc
|
| 188 |
+
or "non hemolytic" in text_lc
|
| 189 |
+
or "non-hemolytic" in text_lc
|
| 190 |
+
):
|
| 191 |
+
_set_if_stronger(parsed, "Haemolysis Type", "None")
|
| 192 |
+
_set_if_stronger(parsed, "Haemolysis", "Negative")
|
| 193 |
+
|
| 194 |
+
# Variable phrasing
|
| 195 |
+
if "variable haemolysis" in text_lc or "variable hemolysis" in text_lc:
|
| 196 |
+
_set_if_stronger(parsed, "Haemolysis Type", "Variable")
|
| 197 |
+
_set_if_stronger(parsed, "Haemolysis", "Variable")
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# ------------------------------------------------------------
|
| 201 |
+
# Boolean test parser (core enzyme tests etc.)
|
| 202 |
+
# ------------------------------------------------------------
|
| 203 |
+
|
| 204 |
+
def _parse_core_bool_tests(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 205 |
+
"""
|
| 206 |
+
For each test in CORE_BOOL_FIELDS, look for patterns like:
|
| 207 |
+
"catalase positive", "positive for catalase", etc.
|
| 208 |
+
Also handles:
|
| 209 |
+
- NaCl tolerance with % values
|
| 210 |
+
- Nitrate reduction text
|
| 211 |
+
- H2S production / non-production
|
| 212 |
+
- DNase universal coverage
|
| 213 |
+
"""
|
| 214 |
+
for field, keywords in CORE_BOOL_FIELDS.items():
|
| 215 |
+
for kw in keywords:
|
| 216 |
+
# "... catalase positive"
|
| 217 |
+
m1 = re.search(
|
| 218 |
+
rf"{re.escape(kw)}[ \-]?(positive|negative|variable|pos|neg|\+|\-)",
|
| 219 |
+
text_lc,
|
| 220 |
+
)
|
| 221 |
+
if m1:
|
| 222 |
+
val = _value_from_pnv_context(m1.group(1))
|
| 223 |
+
if val:
|
| 224 |
+
_set_if_stronger(parsed, field, val)
|
| 225 |
+
break
|
| 226 |
+
|
| 227 |
+
# "positive for catalase"
|
| 228 |
+
m2 = re.search(
|
| 229 |
+
rf"(positive|negative|variable|pos|neg|\+|\-)\s+(for\s+)?{re.escape(kw)}",
|
| 230 |
+
text_lc,
|
| 231 |
+
)
|
| 232 |
+
if m2:
|
| 233 |
+
val = _value_from_pnv_context(m2.group(1))
|
| 234 |
+
if val:
|
| 235 |
+
_set_if_stronger(parsed, field, val)
|
| 236 |
+
break
|
| 237 |
+
|
| 238 |
+
# Special-case NaCl tolerance with explicit percentages
|
| 239 |
+
if field == "NaCl Tolerant (>=6%)":
|
| 240 |
+
# e.g. "grows in 6.5% NaCl", "grows at 10% NaCl"
|
| 241 |
+
for m in re.finditer(
|
| 242 |
+
r"(grows|growth)\s+(in|at)\s*(\d+(?:\.\d+)?)\s*%?\s*nacl",
|
| 243 |
+
text_lc,
|
| 244 |
+
):
|
| 245 |
+
try:
|
| 246 |
+
conc = float(m.group(3))
|
| 247 |
+
if conc >= 6.0:
|
| 248 |
+
_set_if_stronger(parsed, field, "Positive")
|
| 249 |
+
except Exception:
|
| 250 |
+
pass
|
| 251 |
+
|
| 252 |
+
# e.g. "NaCl tolerant up to 10%"
|
| 253 |
+
for m in re.finditer(
|
| 254 |
+
r"nacl\s+tolerant\s+(?:to|up to)?\s*(\d+(?:\.\d+)?)\s*%?",
|
| 255 |
+
text_lc,
|
| 256 |
+
):
|
| 257 |
+
try:
|
| 258 |
+
conc = float(m.group(1))
|
| 259 |
+
if conc >= 6.0:
|
| 260 |
+
_set_if_stronger(parsed, field, "Positive")
|
| 261 |
+
except Exception:
|
| 262 |
+
pass
|
| 263 |
+
|
| 264 |
+
# explicit negative phrasing: "does not grow in 7% NaCl"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
if re.search(
|
| 266 |
r"does (not|n't) grow\s+(in|at)\s*(\d+(?:\.\d+)?)\s*%?\s*nacl",
|
| 267 |
text_lc,
|
| 268 |
):
|
| 269 |
+
_set_if_stronger(parsed, "NaCl Tolerant (>=6%)", "Negative")
|
| 270 |
+
|
| 271 |
+
# general "in 6.5% NaCl" → assume tolerance if no explicit "no growth"
|
| 272 |
+
for m in re.finditer(
|
| 273 |
+
r"\bin\s*(\d+(?:\.\d+)?)\s*%?\s*nacl",
|
| 274 |
+
text_lc,
|
| 275 |
+
):
|
| 276 |
+
try:
|
| 277 |
+
conc = float(m.group(1))
|
| 278 |
+
if conc >= 6.0 and "does not" not in text_lc and "no growth" not in text_lc:
|
| 279 |
+
_set_if_stronger(parsed, "NaCl Tolerant (>=6%)", "Positive")
|
| 280 |
+
except Exception:
|
| 281 |
+
pass
|
| 282 |
+
|
| 283 |
+
# Nitrate: "reduces nitrate" / "does not reduce nitrate"
|
| 284 |
+
if re.search(r"reduces nitrate", text_lc):
|
| 285 |
+
_set_if_stronger(parsed, "Nitrate Reduction", "Positive")
|
| 286 |
+
if re.search(r"does (not|n't) reduce nitrate", text_lc):
|
| 287 |
+
_set_if_stronger(parsed, "Nitrate Reduction", "Negative")
|
| 288 |
+
|
| 289 |
+
# H2S: "produces H2S", "H2S production", "does not produce H2S",
|
| 290 |
+
# "non-H2S producing"
|
| 291 |
+
if re.search(r"(produces|production of)\s+h2s", text_lc):
|
| 292 |
+
_set_if_stronger(parsed, "H2S", "Positive")
|
| 293 |
+
if (
|
| 294 |
+
re.search(r"does (not|n't) produce\s+h2s", text_lc)
|
| 295 |
+
or re.search(r"no h2s production", text_lc)
|
| 296 |
+
or re.search(r"non[- ]h2s producing", text_lc)
|
| 297 |
+
):
|
| 298 |
+
_set_if_stronger(parsed, "H2S", "Negative")
|
| 299 |
+
|
| 300 |
+
# --- DNase universal coverage ---
|
| 301 |
+
# Positive forms
|
| 302 |
+
if re.search(r"\bdnase(\s+test|\s+activity|\s+production)?\s*(positive|pos|\+)\b", text_lc):
|
| 303 |
+
_set_if_stronger(parsed, "DNase", "Positive")
|
| 304 |
+
|
| 305 |
+
if re.search(r"\b(positive|pos|\+)\s+dnase(\s+test|\s+activity|\s+production)?\b", text_lc):
|
| 306 |
+
_set_if_stronger(parsed, "DNase", "Positive")
|
| 307 |
+
|
| 308 |
+
# Negative forms
|
| 309 |
+
if re.search(r"\bdnase(\s+test|\s+activity|\s+production)?\s*(negative|neg|\-)\b", text_lc):
|
| 310 |
+
_set_if_stronger(parsed, "DNase", "Negative")
|
| 311 |
+
|
| 312 |
+
if re.search(r"\b(negative|neg|\-)\s+dnase(\s+test|\s+activity|\s+production)?\b", text_lc):
|
| 313 |
+
_set_if_stronger(parsed, "DNase", "Negative")
|
| 314 |
+
|
| 315 |
+
# non-DNase-producing
|
| 316 |
+
if re.search(r"\bnon[- ]?dnase[- ]?producing\b", text_lc):
|
| 317 |
+
_set_if_stronger(parsed, "DNase", "Negative")
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# ------------------------------------------------------------
|
| 321 |
+
# Motility / Capsule / Spores
|
| 322 |
+
# ------------------------------------------------------------
|
| 323 |
+
|
| 324 |
+
def _parse_motility_capsule_spores(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 325 |
+
# Motility
|
| 326 |
+
if (
|
| 327 |
+
re.search(r"\bmotile\b", text_lc)
|
| 328 |
+
and not re.search(r"\bnon[- ]?motile\b", text_lc)
|
| 329 |
+
and "nonmotile" not in text_lc
|
| 330 |
+
and "immotile" not in text_lc
|
| 331 |
+
):
|
| 332 |
+
_set_if_stronger(parsed, "Motility", "Positive")
|
| 333 |
+
|
| 334 |
+
if (
|
| 335 |
+
"non-motile" in text_lc
|
| 336 |
+
or "non motile" in text_lc
|
| 337 |
+
or "nonmotile" in text_lc
|
| 338 |
+
or "immotile" in text_lc
|
| 339 |
+
):
|
| 340 |
+
_set_if_stronger(parsed, "Motility", "Negative")
|
| 341 |
+
|
| 342 |
+
# Specific motility phrases: tumbling, swarming, corkscrew
|
| 343 |
+
if (
|
| 344 |
+
"tumbling motility" in text_lc
|
| 345 |
+
or "swarming motility" in text_lc
|
| 346 |
+
or "corkscrew motility" in text_lc
|
| 347 |
+
or ("swarming" in text_lc and "non-swarming" not in text_lc)
|
| 348 |
+
):
|
| 349 |
+
_set_if_stronger(parsed, "Motility", "Positive")
|
| 350 |
+
|
| 351 |
+
# Capsule (including "capsule positive/negative")
|
| 352 |
+
if (
|
| 353 |
+
"capsulated" in text_lc
|
| 354 |
+
or "encapsulated" in text_lc
|
| 355 |
+
or "capsule present" in text_lc
|
| 356 |
+
or re.search(r"capsule[ \-]?(positive|pos|\+)", text_lc)
|
| 357 |
+
):
|
| 358 |
+
_set_if_stronger(parsed, "Capsule", "Positive")
|
| 359 |
+
|
| 360 |
+
if (
|
| 361 |
+
"non-capsulated" in text_lc
|
| 362 |
+
or "no capsule" in text_lc
|
| 363 |
+
or re.search(r"capsule[ \-]?(negative|neg|\-)", text_lc)
|
| 364 |
+
):
|
| 365 |
+
_set_if_stronger(parsed, "Capsule", "Negative")
|
| 366 |
+
|
| 367 |
+
# Spore formation
|
| 368 |
+
# NEGATIVE FIRST with strict boundaries, then early-return
|
| 369 |
+
if (
|
| 370 |
+
re.search(r"\bnon[-\s]?spore[-\s]?forming\b", text_lc)
|
| 371 |
+
or "no spores" in text_lc
|
| 372 |
+
):
|
| 373 |
+
_set_if_stronger(parsed, "Spore Formation", "Negative")
|
| 374 |
+
return # prevent any positive overwrite
|
| 375 |
+
|
| 376 |
+
# POSITIVE (must not match the negative form)
|
| 377 |
+
if (
|
| 378 |
+
re.search(r"\bspore[-\s]?forming\b", text_lc)
|
| 379 |
+
or "forms spores" in text_lc
|
| 380 |
+
):
|
| 381 |
+
_set_if_stronger(parsed, "Spore Formation", "Positive")
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
# ------------------------------------------------------------
|
| 385 |
+
# Oxygen requirement
|
| 386 |
+
# ------------------------------------------------------------
|
| 387 |
+
|
| 388 |
+
def _parse_oxygen(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 389 |
+
"""
|
| 390 |
+
Robust oxygen parsing:
|
| 391 |
+
- Handle facultative first
|
| 392 |
+
- Avoid "aerobic" accidentally matching inside "anaerobic"
|
| 393 |
+
- Include "aerobically" / "anaerobically"
|
| 394 |
+
"""
|
| 395 |
+
# Facultative first
|
| 396 |
+
if re.search(r"facultative(ly)? anaerob", text_lc):
|
| 397 |
+
_set_if_stronger(parsed, "Oxygen Requirement", "Facultative Anaerobe")
|
| 398 |
+
|
| 399 |
+
# Strict anaerobic (before aerobic)
|
| 400 |
+
if (
|
| 401 |
+
re.search(r"\bobligate anaerob", text_lc)
|
| 402 |
+
or (re.search(r"\banaerobic\b", text_lc) and "facultative" not in text_lc)
|
| 403 |
+
or re.search(r"\banaerobically\b", text_lc)
|
| 404 |
+
):
|
| 405 |
+
_set_if_stronger(parsed, "Oxygen Requirement", "Anaerobic")
|
| 406 |
+
|
| 407 |
+
# Now handle purely aerobic, avoiding "anaerobic"
|
| 408 |
+
if (
|
| 409 |
+
re.search(r"\bobligate aerobe\b", text_lc)
|
| 410 |
+
or (
|
| 411 |
+
re.search(r"\baerobic\b", text_lc)
|
| 412 |
+
and "anaerobic" not in text_lc
|
| 413 |
+
)
|
| 414 |
+
or (
|
| 415 |
+
re.search(r"\baerobically\b", text_lc)
|
| 416 |
+
and "anaerobically" not in text_lc
|
| 417 |
+
)
|
| 418 |
+
):
|
| 419 |
+
_set_if_stronger(parsed, "Oxygen Requirement", "Aerobic")
|
| 420 |
+
|
| 421 |
+
if "microaerophilic" in text_lc or "microaerophile" in text_lc:
|
| 422 |
+
_set_if_stronger(parsed, "Oxygen Requirement", "Microaerophilic")
|
| 423 |
+
|
| 424 |
+
if "capnophilic" in text_lc or "co2" in text_lc:
|
| 425 |
+
_set_if_stronger(parsed, "Oxygen Requirement", "Capnophilic")
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
# ------------------------------------------------------------
|
| 429 |
+
# Growth temperature
|
| 430 |
+
# ------------------------------------------------------------
|
| 431 |
+
|
| 432 |
+
def _parse_growth_temperature(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 433 |
+
"""
|
| 434 |
+
Look for explicit temperatures like "grows at 37 °C" or ranges like "4–45 °C".
|
| 435 |
+
We ALWAYS store as "low//high":
|
| 436 |
+
- true ranges: "4-45 °C" → "4//45"
|
| 437 |
+
- two temps in text: min//max (Option A), e.g.:
|
| 438 |
+
"grows at 4 °C but not at 45 °C" → "4//45"
|
| 439 |
+
"grows at 42 °C but not at 25 °C" → "25//42"
|
| 440 |
+
- single temps: "37 °C" → "37//37"
|
| 441 |
+
"""
|
| 442 |
+
# 1) Explicit ranges like "4-45 °C" or "10–40 °C"
|
| 443 |
+
range_pattern = re.compile(
|
| 444 |
+
r"(\d+)\s*[-–/]\s*(\d+)\s*(?:c|°c|degrees c|degrees celsius)"
|
| 445 |
+
)
|
| 446 |
+
m_range = range_pattern.search(text_lc)
|
| 447 |
+
if m_range:
|
| 448 |
+
low = m_range.group(1)
|
| 449 |
+
high = m_range.group(2)
|
| 450 |
+
_set_if_stronger(parsed, "Growth Temperature", f"{low}//{high}")
|
| 451 |
+
return
|
| 452 |
+
|
| 453 |
+
# 2) Option A: any two explicit temps → min//max
|
| 454 |
+
temps = re.findall(r"(\d+)\s*(?:c|°c|degrees c|degrees celsius)", text_lc)
|
| 455 |
+
if len(temps) >= 2:
|
| 456 |
+
nums = [int(t) for t in temps]
|
| 457 |
+
low = min(nums)
|
| 458 |
+
high = max(nums)
|
| 459 |
+
_set_if_stronger(parsed, "Growth Temperature", f"{low}//{high}")
|
| 460 |
+
return
|
| 461 |
+
|
| 462 |
+
# 3) Single temps like "grows at 37 c"
|
| 463 |
+
single_pattern = re.compile(
|
| 464 |
+
r"(grows|growth|optimum|optimal)\s+(?:at\s+)?(\d+)\s*(?:c|°c|degrees c|degrees celsius)"
|
| 465 |
+
)
|
| 466 |
+
m_single = single_pattern.search(text_lc)
|
| 467 |
+
if m_single:
|
| 468 |
+
temp = m_single.group(2)
|
| 469 |
+
_set_if_stronger(parsed, "Growth Temperature", f"{temp}//{temp}")
|
| 470 |
+
return
|
| 471 |
+
|
| 472 |
+
# 4) Simplified: "grows at 37" (no explicit °C)
|
| 473 |
+
m_simple_num = re.search(r"grows at (\d+)\b", text_lc)
|
| 474 |
+
if m_simple_num:
|
| 475 |
+
temp = m_simple_num.group(1)
|
| 476 |
+
_set_if_stronger(parsed, "Growth Temperature", f"{temp}//{temp}")
|
| 477 |
+
return
|
| 478 |
+
|
| 479 |
+
# 5) Fallback: plain "37c" somewhere in the text
|
| 480 |
+
m_plain = re.search(
|
| 481 |
+
r"\b(\d+)\s*(?:c|°c|degrees c|degrees celsius)\b",
|
| 482 |
+
text_lc,
|
| 483 |
+
)
|
| 484 |
+
if m_plain:
|
| 485 |
+
temp = m_plain.group(1)
|
| 486 |
+
_set_if_stronger(parsed, "Growth Temperature", f"{temp}//{temp}")
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
# ------------------------------------------------------------
|
| 490 |
+
# Media grown on (coarse mapping)
|
| 491 |
+
# ------------------------------------------------------------
|
| 492 |
+
|
| 493 |
+
MEDIA_KEYWORDS = {
|
| 494 |
+
"Blood Agar": [
|
| 495 |
+
"blood agar",
|
| 496 |
+
"blood-agar",
|
| 497 |
+
],
|
| 498 |
+
"MacConkey Agar": [
|
| 499 |
+
"macconkey agar",
|
| 500 |
+
"mac conkey agar",
|
| 501 |
+
"macconkey",
|
| 502 |
+
],
|
| 503 |
+
"Chocolate Agar": [
|
| 504 |
+
"chocolate agar",
|
| 505 |
+
"chocolate-agar",
|
| 506 |
+
],
|
| 507 |
+
"Nutrient Agar": [
|
| 508 |
+
"nutrient agar",
|
| 509 |
+
"nutrient-agar",
|
| 510 |
+
],
|
| 511 |
+
"XLD Agar": [
|
| 512 |
+
"xld agar",
|
| 513 |
+
],
|
| 514 |
+
"TCBS Agar": [
|
| 515 |
+
"tcbs agar",
|
| 516 |
+
"tcbs",
|
| 517 |
+
],
|
| 518 |
+
"ALOA": [
|
| 519 |
+
"aloa agar",
|
| 520 |
+
"aloa",
|
| 521 |
+
],
|
| 522 |
+
"BCYE Agar": [
|
| 523 |
+
"bcye agar",
|
| 524 |
+
"bcye",
|
| 525 |
+
],
|
| 526 |
+
"MRS Agar": [
|
| 527 |
+
"mrs agar",
|
| 528 |
+
],
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
def _parse_media(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 533 |
+
found_media: List[str] = []
|
| 534 |
+
for media_name, patterns in MEDIA_KEYWORDS.items():
|
| 535 |
+
for p in patterns:
|
| 536 |
+
if p in text_lc and media_name not in found_media:
|
| 537 |
+
found_media.append(media_name)
|
| 538 |
+
|
| 539 |
+
if found_media:
|
| 540 |
+
_set_if_stronger(parsed, "Media Grown On", "; ".join(found_media))
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
# ------------------------------------------------------------
|
| 544 |
+
# Sugar fermentation parsing
|
| 545 |
+
# ------------------------------------------------------------
|
| 546 |
+
|
| 547 |
+
def _parse_sugars(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 548 |
+
"""
|
| 549 |
+
Handles patterns like:
|
| 550 |
+
- "glucose positive, mannitol negative"
|
| 551 |
+
- "ferments glucose, mannitol and sucrose but not lactose"
|
| 552 |
+
- "does not ferment lactose"
|
| 553 |
+
- "non-lactose fermenter"
|
| 554 |
+
- global non-fermenter phrases
|
| 555 |
+
"""
|
| 556 |
+
|
| 557 |
+
# 0) Simple "glucose positive / negative" style
|
| 558 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 559 |
+
m_simple = re.search(
|
| 560 |
+
rf"{sugar_key}\s+(positive|negative|variable|pos|neg|\+|\-)",
|
| 561 |
+
text_lc,
|
| 562 |
+
)
|
| 563 |
+
if m_simple:
|
| 564 |
+
val = _value_from_pnv_context(m_simple.group(1))
|
| 565 |
+
if val:
|
| 566 |
+
_set_if_stronger(parsed, field, val)
|
| 567 |
+
|
| 568 |
+
# 1) "ferments X, Y and Z but not A, B"
|
| 569 |
+
ferments_pattern = re.compile(r"ferments\s+([a-z0-9 ,;/&\-]+)")
|
| 570 |
+
for m in ferments_pattern.finditer(text_lc):
|
| 571 |
+
seg = m.group(1)
|
| 572 |
+
# Split positive vs negative part on "but not"
|
| 573 |
+
neg_split = re.split(r"\bbut not\b", seg, maxsplit=1)
|
| 574 |
+
pos_part = neg_split[0]
|
| 575 |
+
neg_part = neg_split[1] if len(neg_split) > 1 else ""
|
| 576 |
+
|
| 577 |
+
# Positive sugars from pos_part
|
| 578 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 579 |
+
if re.search(rf"\b{sugar_key}\b", pos_part):
|
| 580 |
+
_set_if_stronger(parsed, field, "Positive")
|
| 581 |
+
|
| 582 |
+
# Negative sugars from neg_part
|
| 583 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 584 |
+
if re.search(rf"\b{sugar_key}\b", neg_part):
|
| 585 |
+
_set_if_stronger(parsed, field, "Negative")
|
| 586 |
+
|
| 587 |
+
# 2) "does not ferment X"
|
| 588 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 589 |
+
if re.search(rf"does (not|n't) ferment {sugar_key}\b", text_lc):
|
| 590 |
+
_set_if_stronger(parsed, field, "Negative")
|
| 591 |
+
|
| 592 |
+
# 3) "non-lactose fermenter"
|
| 593 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 594 |
+
if re.search(rf"non[- ]{sugar_key} ferment(ing|er)?", text_lc):
|
| 595 |
+
_set_if_stronger(parsed, field, "Negative")
|
| 596 |
+
|
| 597 |
+
# 4) "X fermentation positive/negative"
|
| 598 |
+
for sugar_key, field in SUGAR_FIELDS.items():
|
| 599 |
+
# "glucose fermentation positive"
|
| 600 |
+
m1 = re.search(
|
| 601 |
+
rf"{sugar_key}\s+fermentation[ \-]?(positive|negative|variable|pos|neg|\+|\-)",
|
| 602 |
+
text_lc,
|
| 603 |
+
)
|
| 604 |
+
if m1:
|
| 605 |
+
val = _value_from_pnv_context(m1.group(1))
|
| 606 |
+
if val:
|
| 607 |
+
_set_if_stronger(parsed, field, val)
|
| 608 |
+
continue
|
| 609 |
+
|
| 610 |
+
# "positive for glucose fermentation"
|
| 611 |
+
m2 = re.search(
|
| 612 |
+
rf"(positive|negative|variable|pos|neg|\+|\-)\s+(for\s+)?{sugar_key}\s+fermentation",
|
| 613 |
+
text_lc,
|
| 614 |
+
)
|
| 615 |
+
if m2:
|
| 616 |
+
val = _value_from_pnv_context(m2.group(1))
|
| 617 |
+
if val:
|
| 618 |
+
_set_if_stronger(parsed, field, val)
|
| 619 |
+
continue
|
| 620 |
+
|
| 621 |
+
# 5) Global non-fermenter patterns
|
| 622 |
+
if (
|
| 623 |
+
re.search(r"does (not|n't) ferment (carbohydrates|sugars)", text_lc)
|
| 624 |
+
or re.search(r"non[- ]ferment(er|ing|ative)", text_lc)
|
| 625 |
+
):
|
| 626 |
+
for field in SUGAR_FIELDS.values():
|
| 627 |
+
if field not in parsed or parsed[field] == UNKNOWN:
|
| 628 |
+
_set_if_stronger(parsed, field, "Negative")
|
| 629 |
+
|
| 630 |
+
|
| 631 |
+
# ------------------------------------------------------------
|
| 632 |
+
# Colony morphology (coarse, optional)
|
| 633 |
+
# ------------------------------------------------------------
|
| 634 |
+
|
| 635 |
+
def _parse_colony(text_lc: str, parsed: Dict[str, str]) -> None:
|
| 636 |
+
"""
|
| 637 |
+
Very coarse mapping for colony morphology. We try:
|
| 638 |
+
- "colonies are yellow, mucoid"
|
| 639 |
+
- "colonies dry, white and irregular on nutrient agar"
|
| 640 |
+
- "forming green colonies", "forms mucoid colonies"
|
| 641 |
+
"""
|
| 642 |
+
# Pattern 1: "colonies are ..."
|
| 643 |
+
m = re.search(r"colon(y|ies) (are|is)\s+([a-z0-9 ,;\-]+)", text_lc)
|
| 644 |
+
if m:
|
| 645 |
+
desc = m.group(3).strip()
|
| 646 |
+
if desc:
|
| 647 |
+
pretty = "; ".join(
|
| 648 |
+
[s.strip().capitalize() for s in re.split(r"[;,]", desc) if s.strip()]
|
| 649 |
+
)
|
| 650 |
+
if pretty:
|
| 651 |
+
_set_if_stronger(parsed, "Colony Morphology", pretty)
|
| 652 |
+
return
|
| 653 |
+
|
| 654 |
+
# Pattern 2: "colonies dry, white and irregular on nutrient agar"
|
| 655 |
+
m2 = re.search(
|
| 656 |
+
r"colonies\s+([a-z0-9 ,;\-]+?)(?:\s+on\b|\.|,)",
|
| 657 |
+
text_lc,
|
| 658 |
+
)
|
| 659 |
+
if m2:
|
| 660 |
+
desc = m2.group(1).strip()
|
| 661 |
+
if desc:
|
| 662 |
+
pretty = "; ".join(
|
| 663 |
+
[s.strip().capitalize() for s in re.split(r"[;,]", desc) if s.strip()]
|
| 664 |
+
)
|
| 665 |
+
if pretty:
|
| 666 |
+
_set_if_stronger(parsed, "Colony Morphology", pretty)
|
| 667 |
+
return
|
| 668 |
+
|
| 669 |
+
# Pattern 3: "forming green colonies", "forms mucoid colonies"
|
| 670 |
+
m3 = re.search(
|
| 671 |
+
r"(forming|forms|produces)\s+([a-z0-9 ,;\-]+?)\s+colonies",
|
| 672 |
+
text_lc,
|
| 673 |
+
)
|
| 674 |
+
if m3:
|
| 675 |
+
desc = m3.group(2).strip()
|
| 676 |
+
if desc:
|
| 677 |
+
pretty = "; ".join(
|
| 678 |
+
[s.strip().capitalize() for s in re.split(r"[;,]", desc) if s.strip()]
|
| 679 |
+
)
|
| 680 |
+
if pretty:
|
| 681 |
+
_set_if_stronger(parsed, "Colony Morphology", pretty)
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
# ------------------------------------------------------------
|
| 685 |
+
# PUBLIC API
|
| 686 |
+
# ------------------------------------------------------------
|
| 687 |
+
|
| 688 |
+
def parse_text_rules(text: str) -> Dict[str, Any]:
|
| 689 |
+
"""
|
| 690 |
+
Main entry point.
|
| 691 |
+
"""
|
| 692 |
+
original = text or ""
|
| 693 |
+
text_clean = _clean_text(original)
|
| 694 |
+
text_lc = text_clean.lower()
|
| 695 |
+
|
| 696 |
+
parsed: Dict[str, str] = {}
|
| 697 |
+
|
| 698 |
+
try:
|
| 699 |
+
_parse_gram_and_shape(text_lc, parsed)
|
| 700 |
+
_parse_haemolysis(text_lc, parsed)
|
| 701 |
+
_parse_core_bool_tests(text_lc, parsed)
|
| 702 |
+
_parse_motility_capsule_spores(text_lc, parsed)
|
| 703 |
+
_parse_oxygen(text_lc, parsed)
|
| 704 |
+
_parse_growth_temperature(text_lc, parsed)
|
| 705 |
+
_parse_media(text_lc, parsed)
|
| 706 |
+
_parse_sugars(text_lc, parsed)
|
| 707 |
+
_parse_colony(text_lc, parsed)
|
| 708 |
+
|
| 709 |
+
return {
|
| 710 |
+
"parsed_fields": parsed,
|
| 711 |
+
"source": "rule_parser",
|
| 712 |
+
"raw": original,
|
| 713 |
+
}
|
| 714 |
+
|
| 715 |
+
except Exception as e:
|
| 716 |
+
# Fail-safe: never crash the app, just report an error
|
| 717 |
+
return {
|
| 718 |
+
"parsed_fields": parsed,
|
| 719 |
+
"source": "rule_parser",
|
| 720 |
+
"raw": original,
|
| 721 |
+
"error": f"{type(e).__name__}: {e}",
|
| 722 |
}
|