开始使用免费开始使用

词频分析

恭喜您加入 PyBooks!PyBooks 正在开发一本图书推荐系统,他们希望通过发现文本中的模式与趋势来改进推荐效果。

首先,您需要了解给定文本中的词频,并移除任何低频词。

请注意,真实世界的数据集通常会比本示例大得多。

本练习是课程的一部分

使用 PyTorch 的文本深度学习

查看课程

练习说明

  • torchtext 导入 get_tokenizer,从 nltk 库导入 FreqDist
  • 为英语初始化分词器,并对给定的 text 进行分词。
  • 计算 tokens 的频率分布,并使用列表推导式移除低频词。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the necessary functions
from torchtext.data.utils import ____
from nltk.probability import ____

text = "In the city of Dataville, a data analyst named Alex explores hidden insights within vast data. With determination, Alex uncovers patterns, cleanses the data, and unlocks innovation. Join this adventure to unleash the power of data-driven decisions."

# Initialize the tokenizer and tokenize the text
tokenizer = ____("basic_english")
tokens = tokenizer(____)

threshold = 1
# Remove rare words and print common tokens
freq_dist = ____(____)
common_tokens = [token for token in tokens if ____[token] > ____]
print(common_tokens)
编辑并运行代码