要使用NLTK库提取关键词,可以按照以下步骤进行:
pip install nltk
import nltk
nltk.download('punkt')
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from collections import Counter
text = "Your text goes here."
# 分词
words = word_tokenize(text)
# 去除停用词
stop_words = set(stopwords.words('english'))
filtered_words = [word for word in words if word.lower() not in stop_words]
# 计算词频
word_freq = Counter(filtered_words)
# 获取前N个关键词
top_keywords = word_freq.most_common(N)
在上述代码中,首先对文本进行分词,然后去除停用词,接着计算词频并获取前N个关键词。可以根据具体需求调整代码中的参数和逻辑来实现更精确的关键词提取。