Skip to content

Instantly share code, notes, and snippets.

@omri374
Last active May 6, 2021 09:08
Show Gist options
  • Save omri374/750a63b5d3efd53763088fd3aaf19c83 to your computer and use it in GitHub Desktop.
Save omri374/750a63b5d3efd53763088fd3aaf19c83 to your computer and use it in GitHub Desktop.
def names_recall(nlp: spacy.lang.en.English, names: List[str], template: str):
"""
Run the spaCy NLP model on the template + name,
calculate recall for detecting the "PERSON" entity
and return a detailed list of detection
:param nlp: spaCy nlp model
:param names: list of names to run model on
:param template: sentence with placeholder for name (e.g. "He calls himself {}")
"""
results = {}
for name in names:
doc = nlp(template.format(name))
name_token = [token for token in doc if token.text == name][0]
results[name] = name_token.ent_type_ == "PERSON"
recall = sum(results.values()) / len(results)
print(f"Recall: {recall:.2f}\n")
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment