1
我是新來的Python文本處理,我試圖阻止詞在文本文件中,有大約5000行。詞幹與NLTK(python)
我寫了下面的腳本
from nltk.corpus import stopwords # Import the stop word list
from nltk.stem.snowball import SnowballStemmer
stemmer = SnowballStemmer('english')
def Description_to_words(raw_Description):
# 1. Remove HTML
Description_text = BeautifulSoup(raw_Description).get_text()
# 2. Remove non-letters
letters_only = re.sub("[^a-zA-Z]", " ", Description_text)
# 3. Convert to lower case, split into individual words
words = letters_only.lower().split()
stops = set(stopwords.words("english"))
# 5. Remove stop words
meaningful_words = [w for w in words if not w in stops]
# 5. stem words
words = ([stemmer.stem(w) for w in words])
# 6. Join the words back into one string separated by space,
# and return the result.
return(" ".join(meaningful_words))
clean_Description = Description_to_words(train["Description"][15])
但是當我測試的結果的話被未去梗,誰能幫助我知道什麼是問題,我做的「Description_to_words」功能不對勁
而且,當我像下面那樣單獨執行幹命令時,它就起作用了。
from nltk.tokenize import sent_tokenize, word_tokenize
>>> words = word_tokenize("MOBILE APP - Unable to add reading")
>>>
>>> for w in words:
... print(stemmer.stem(w))
...
mobil
app
-
unabl
to
add
read
這很簡單。非常感謝您的回覆。有用。我很高興:) – user3734568
只是一個問題,我們可以在詞形化詞中使用相同的邏輯.lemmatize()正確 – user3734568
@ user3734568是的,你可以,只需將'stemmer.stem(w)'改爲'lemmatizer.lemmatize(word) ' –