データフレーム内のTEDトークをクレンジングする
この演習では、第1章で扱ったTEDトークに戻ります。5本のTEDトークからなるデータフレーム ted が与えられています。これらのトークを、これまでに学んだ手法を用いてクレンジングします。具体的には、関数 preprocess を作成し、データフレームの transcript 特徴量に適用してください。
ストップワードのリストは stopwords として利用できます。
この演習はコースの一部です
Pythonで学ぶNLPの特徴量エンジニアリング
演習の手順
textに対して Doc オブジェクトを生成します。今はdisable引数は無視してかまいません。lemma_属性を使い、リスト内包表記でレンマを生成します。- if 条件で
isalpha()を使い、アルファベット以外の文字を取り除きます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Function to preprocess text
def preprocess(text):
# Create Doc object
doc = nlp(____, disable=['ner', 'parser'])
# Generate lemmas
lemmas = [token.____ for token in doc]
# Remove stopwords and non-alphabetic characters
a_lemmas = [lemma for lemma in lemmas
if lemma.____ and lemma not in stopwords]
return ' '.join(a_lemmas)
# Apply preprocess to ted['transcript']
ted['transcript'] = ted['transcript'].apply(____)
print(ted['transcript'])