Skip to content

UnboundLocalError with fuzzy matching #27

Description

@SLL-26

Description

Calling find_drugs() with is_fuzzy_match=True raises an UnboundLocalError instead of returning drug matches.

The error occurs when processing a tokenised sentence containing medication names and dosages. It appears that the variable lookup_name is referenced before being assigned within the package's find_drugs() implementation.

The issue occurs consistently with the example below and prevents fuzzy matching from being used. The same code works correctly when fuzzy matching is disabled (is_fuzzy_match=False).

Environment

  • Operating System: Windows
  • Python Version: 3.13.7
  • spaCy Version: 3.8.15
  • drug_named_entity_recognition Version: 2.0.9
  • Environment: Python virtual environment (venv)

How to Reproduce

  1. Create a spaCy document:
test = "Repeat prescription list still shows Mirtazapine 45mg Nocte and paracetomol 500mg OD"
test_doc = nlp(test)
  1. Run find_drugs() with fuzzy matching enabled:
matches = find_drugs(
    [t.text for t in test_doc],
    is_fuzzy_match=True
)
  1. Observe the exception:
UnboundLocalError: cannot access local variable 'lookup_name'
where it is not associated with a value

Expected Behaviour

find_drugs() should return drug matches when fuzzy matching is enabled. For example, it should identify:

  • Mirtazapine
  • paracetomol (via fuzzy matching to paracetamol, if supported)

Actual Behaviour

The function raises:

UnboundLocalError: cannot access local variable 'lookup_name'
where it is not associated with a value

Full Traceback

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)

matches = find_drugs([t.text for t in test_doc], is_fuzzy_match=True)

File ".../drug_named_entity_recognition/drugs_finder.py", line 268, in find_drugs
    match_data = get_molecular_weight(
        match_data, lookup_name, use_pub_chem_api
    )

UnboundLocalError: cannot access local variable 'lookup_name'
where it is not associated with a value

Additional Information

The issue does not occur when fuzzy matching is disabled:

matches = find_drugs(
    [t.text for t in test_doc],
    is_fuzzy_match=False
)

which returns results as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions