清洗文本数据
现在您已经定义了停用词和标点,让我们用它们来进一步清洗数据框 df 中的 Enron 邮件。包含停用词和标点的列表分别位于 stop 和 exclude。在数据完全清洗前,还需要执行一些步骤,比如词的"词形还原"(lemmatization)和动词词干提取(stemming)。本练习中,邮件数据里的动词已经做过词干提取,词形还原也已为您处理完毕。
本练习是课程的一部分
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