亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在邏輯條件中包含更多停用詞列表以過濾單詞

在邏輯條件中包含更多停用詞列表以過濾單詞

慕的地6264312 2023-10-31 21:42:28
我需要在清理數據時添加更多條件,包括刪除停用詞、星期幾和月份。對于星期幾和月份,我創建了一個單獨的列表(我不知道 python 中是否有一些已經內置的包來包含它們)。對于數字,我會考慮 isdigit。所以像這樣:days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']# need to put into lower casemonths=['January','February','March', 'April','May','June','July','August','September','October','November','December']# need to put into lower casecleaned = [w for w in remove_punc.split() if w.lower() not in stopwords.words('english')]我怎樣才能包含在上面的代碼中?我知道這是需要考慮額外的 if 語句,但我正在努力解決這個問題。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您可以將所有列表轉換為集合,并將它們的并集作為最終集合。然后只需要檢查你的單詞在集合中的成員資格。像下面這樣的東西會起作用:


# existing code

from nltk.corpus import stopwords


days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

# need to put into lower case

months=['January','February','March', 'April','May','June','July','August','September','October','November','December']

# need to put into lower case


# add these lines

stop_words = set(stopwords.words('english'))

lowercase_days = {item.lower() for item in days}

lowercase_months = {item.lower() for item in months}


exclusion_set = lowercase_days.union(lowercase_months).union(stop_words)


# now do the final check

cleaned = [w for w in remove_punc.split() if w.lower() not in exclusion_set and not w.isdigit()]



查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號