始める無料で始める

テキストデータのクリーニング

すでにストップワードと句読点を定義しましたので、これらを使ってデータフレーム df 内の Enron のメールをさらにクリーニングしていきます。ストップワードと句読点を含むリストはそれぞれ stopexclude に用意されています。完全にクリーンなデータにするには、単語の「レンマ化(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
コードを編集して実行