我正在尝试使用lemmatizer
预处理一个字符串,然后删除标点符号和数字。我正在使用下面的代码来执行此操作。我没有收到任何错误,但文本没有被适当地预处理。只有停用词被删除,但词汇化不起作用,标点和数字也保留。Python:预处理文本
from nltk.stem import WordNetLemmatizer
import string
import nltk
tweets = "This is a beautiful day16~. I am; working on an exercise45.^^^45 text34."
lemmatizer = WordNetLemmatizer()
tweets = lemmatizer.lemmatize(tweets)
data=[]
stop_words = set(nltk.corpus.stopwords.words('english'))
words = nltk.word_tokenize(tweets)
words = [i for i in words if i not in stop_words]
data.append(' '.join(words))
corpus = " ".join(str(x) for x in data)
p = string.punctuation
d = string.digits
table = str.maketrans(p, len(p) * " ")
corpus.translate(table)
table = str.maketrans(d, len(d) * " ")
corpus.translate(table)
print(corpus)
最终输出我得到的是:
This beautiful day16~ . I ; working exercise45.^^^45 text34 .
和预期的输出应该是这样的:
This beautiful day I work exercise text
我会使用正则表达式来摆脱噪音,调用lemmatizer之前。 –
谢谢你的建议。但是,上面的代码不应该像我期待的那样工作。我以前使用过相同的代码,但它工作正常,但不知道为什么这次不工作。 – Alex