開始使用免費開始

清理文字資料

現在你已經定義好停用詞與標點符號,接著用它們來進一步清理資料框 df 裡的 enron 電子郵件。包含停用詞與標點符號的清單分別位在 stop 與 exclude。在資料算是清理完成前,還有幾個步驟要做,例如字詞的詞形還原(lemmatization)與動詞詞幹提取(stemming)。這份郵件資料中的動詞已經做過詞幹提取,而本練習也已替你完成詞形還原。

本練習屬於課程

Fraud Detection in 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
編輯並執行程式碼