![](/img/trans.png)
[英]nltk stopwords - AttributeError: 'function' object has no attribute 'words'
[英]object has no attribute when removing stop words with NLTK
我正在嘗試從由Python 3中的文本數據行組成的pandas DataFrame的NLTK停用詞集合中刪除停用詞:
import pandas as pd
from nltk.corpus import stopwords
file_path = '/users/rashid/desktop/webtext.csv'
doc = pd.read_csv(file_path, encoding = "ISO-8859-1")
texts = doc['text']
filter = texts != ""
dfNew = texts[filter]
stop = stopwords.words('english')
dfNew.apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))
我收到此錯誤:
'float' object has no attribute 'split'
聽起來您的文字中有一些數字,它們使熊貓變得有點聰明。 添加dtype
選項pandas.read_csv()
以確保在列,一切text
導入為一個字符串:
doc = pd.read_csv(file_path, encoding = "ISO-8859-1", dtype={'text':str})
一旦代碼開始工作,您可能會注意到它很慢:在列表中查找內容效率很低。 將您的停用詞放在這樣的集合中,您將對加速感到驚訝。 ( in
運算符可同時使用集合和列表,但是速度差異很大。)
stop = set(stopwords.words('english'))
最后,將x.split()
更改為nltk.word_tokenize(x)
。 如果您的數據包含真實文本,這會將標點符號與單詞分開,並允許您正確匹配停用詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.