テキストデータのクリーニング
すでにストップワードと句読点を定義しましたので、これらを使ってデータフレーム df 内の Enron のメールをさらにクリーニングしていきます。ストップワードと句読点を含むリストはそれぞれ stop と exclude に用意されています。完全にクリーンなデータにするには、単語の「レンマ化(lemmatization)」や動詞のステミングなど、いくつかの追加ステップが必要です。今回のメールデータでは動詞はすでにステミングされており、この演習ではレンマ化もあらかじめ実行済みです。
この演習はコースの一部です
Pythonで学ぶ不正検知
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import the lemmatizer from nltk
from nltk.stem.wordnet import WordNetLemmatizer
lemma = WordNetLemmatizer()
# Define word cleaning function
def clean(text, stop):
text = text.____()
# Remove stopwords
stop_free = " ".join([word for word in text.lower().split() if ((___ not in ___) and (not word.isdigit()))])
# Remove punctuations
punc_free = ''.join(word for word in stop_free if ___ not in ____)
# Lemmatize all words
normalized = " ".join(____.____(word) for word in punc_free.split())
return normalized