Skip to content

Commit 245df2f

Browse files
committed
Perform a lazy import of optional dep nltk
1 parent 32a5444 commit 245df2f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

redisvl/query/aggregate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Any, Dict, List, Optional, Set, Tuple, Union
22

3-
import nltk
4-
from nltk.corpus import stopwords as nltk_stopwords
53
from redis.commands.search.aggregation import AggregateRequest, Desc
64

75
from redisvl.query.filter import FilterExpression
@@ -164,6 +162,10 @@ def _set_stopwords(self, stopwords: Optional[Union[str, Set[str]]] = "english"):
164162
if not stopwords:
165163
self._stopwords = set()
166164
elif isinstance(stopwords, str):
165+
# Lazy import because nltk is an optional dependency
166+
import nltk
167+
from nltk.corpus import stopwords as nltk_stopwords
168+
167169
try:
168170
nltk.download("stopwords", quiet=True)
169171
self._stopwords = set(nltk_stopwords.words(stopwords))

redisvl/query/query.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from enum import Enum
22
from typing import Any, Dict, List, Optional, Set, Tuple, Union
33

4-
import nltk
5-
from nltk.corpus import stopwords as nltk_stopwords
64
from redis.commands.search.query import Query as RedisQuery
75

86
from redisvl.query.filter import FilterExpression
@@ -812,6 +810,10 @@ def _set_stopwords(self, stopwords: Optional[Union[str, Set[str]]] = "english"):
812810
if not stopwords:
813811
self._stopwords = set()
814812
elif isinstance(stopwords, str):
813+
# Lazy import because nltk is an optional dependency
814+
import nltk
815+
from nltk.corpus import stopwords as nltk_stopwords
816+
815817
try:
816818
nltk.download("stopwords", quiet=True)
817819
self._stopwords = set(nltk_stopwords.words(stopwords))

0 commit comments

Comments
 (0)