Пакет NLTK не работает в производстве, но работает в разработке
Я создал веб-приложение, используя Django. В этом веб-приложении я хочу добавить функциональность для извлечения фраз из контента. Мой код отлично работает в разработке, но не работает в производстве. Используя пакет nltk
, я создал функцию, которая возвращает список фраз из контента. Ниже приведен мой код:
import nltk
from typing import List
def extract_phrases(content: str) -> List:
# Tokenize text into sentences
sentences = nltk.sent_tokenize(content)
# Initialize list to store phrases
phrases = set()
# Iterate through each sentence
for sentence in sentences:
# Tokenize sentence into words
words = nltk.word_tokenize(sentence)
# Get part-of-speech tags for words
pos_tags = nltk.pos_tag(words)
# Initialize list to store phrases in current sentence
current_phrase = []
# Iterate through each word and its part-of-speech tag
for word, pos_tag in pos_tags:
# If the word is a noun or an adjective, add it to the current phrase
if pos_tag.startswith("NN") or pos_tag.startswith("JJ"):
current_phrase.append(word)
# If the word is not a noun or adjective and the current phrase is not empty,
# add the current phrase to the list of phrases and reset it
elif current_phrase:
phrases.add(" ".join(current_phrase))
current_phrase = []
# Add the last phrase from the current sentence (if any)
if current_phrase:
phrases.add(" ".join(current_phrase))
return list(phrases)
В среде разработки функция и все веб-приложение работают правильно. Но в производственной среде, где используется модуль nltk, эта строка не выполняется.
Заметка
Я активировал свою виртуальную среду и запустил следующий код:
import nltk
nltk.download('all')
Мы должны указать путь к загруженным файлам nltk.
import nltk
NLTK_PATH = 'path of files'
nltk.data.path.append(NLTK_PATH)