Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

"""Util functions needed for nltk-based noun-phrase extractors (i.e. TextBlob)."""

from functools import lru_cache

import nltk


@lru_cache(maxsize=64)
def download_if_not_exists(resource_name) -> bool:
"""Download nltk resources if they haven't been already."""
# look under all possible categories
root_categories = [
# Precompute all category/resource_name paths for efficiency
root_categories = (
"corpora",
"tokenizers",
"taggers",
Expand All @@ -24,11 +27,11 @@ def download_if_not_exists(resource_name) -> bool:
"mt",
"sentiment",
"similarity",
]
for category in root_categories:
)
resource_paths = [f"{category}/{resource_name}" for category in root_categories]
for path in resource_paths:
try:
# if found, stop looking and avoid downloading
nltk.find(f"{category}/{resource_name}")
nltk.find(path)
return True # noqa: TRY300
except LookupError:
continue
Expand Down