我正在嘗試使用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