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
- Create a spaCy document:
test = "Repeat prescription list still shows Mirtazapine 45mg Nocte and paracetomol 500mg OD"
test_doc = nlp(test)
- Run
find_drugs() with fuzzy matching enabled:
matches = find_drugs(
[t.text for t in test_doc],
is_fuzzy_match=True
)
- 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.
Description
Calling
find_drugs()withis_fuzzy_match=Trueraises anUnboundLocalErrorinstead of returning drug matches.The error occurs when processing a tokenised sentence containing medication names and dosages. It appears that the variable
lookup_nameis referenced before being assigned within the package'sfind_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
drug_named_entity_recognitionVersion: 2.0.9venv)How to Reproduce
find_drugs()with fuzzy matching enabled:Expected Behaviour
find_drugs()should return drug matches when fuzzy matching is enabled. For example, it should identify:Actual Behaviour
The function raises:
Full Traceback
Additional Information
The issue does not occur when fuzzy matching is disabled:
which returns results as expected.