始める無料で始める

単語頻度の分析

おめでとうございます!あなたは PyBooks の一員になりました。PyBooks は本のレコメンドシステムを開発しており、推薦精度を高めるためにテキストからパターンや傾向を見つけたいと考えています。

まずは、与えられたテキスト内の単語の出現頻度を把握し、まれな単語を取り除きましょう。

実務のデータセットは、この例よりも一般的に大きいことに注意してください。

この演習はコースの一部です

PyTorch で学ぶテキストの Deep Learning

コースを見る

演習の手順

  • 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)
コードを編集して実行