簡體   English   中英

從大文本文件中過濾停用詞(使用包:nltk.corpus)

[英]Filtering stop words out of a large text file (using package: nltk.corpus)

我試圖在大文本文件中排列最常用的單詞 - 愛麗絲和仙境(公共領域)。 這是DropboxPastebin上的Alice和Wonderland。 它運行並且如預期的那樣,有1818個“the”實例和940個“and”實例。

但現在在我最新的腳本迭代中,我試圖過濾掉最常用的單詞,如“and”,“there”,“the”,“that”,“to”,“a”等等。搜索算法在那里尋找像這樣的單詞(在SEO術語中稱為停用詞 )並從查詢中排除它們。 我為此任務導入的Python庫是nltk.corpus

當我生成一個停用詞列表並調用過濾器時,“the”和“of”的所有實例都按預期過濾掉了,但它沒有捕獲“和”或“你”。 我不清楚為什么。

我已經嘗試通過手動和明確添加出現在輸出中不應該出現的單詞來強化停用詞列表。 我添加了“說”,“你”,“那個”等,但它們仍然是文本文件中最常見的10個單詞之一。

這是我的腳本:

from collections import Counter
from nltk.corpus import stopwords
import re

def open_file():
   with open('Alice.txt') as f:
       text = f.read().lower()
   return text

def main(text):
   stoplist = stopwords.words('english') # Bring in the default English NLTK stop words
   stoplist.extend(["said", "i", "it", "you", "and","that",])
   # print(stoplist)
   clean = [word for word in text.split() if word not in stoplist]
   clean_text = ' '.join(clean)
   words = re.findall('\w+', clean_text)
   top_10 = Counter(words).most_common(10)
   for word,count in top_10:
       print(f'{word!r:<4} {"-->":^4} {count:>4}')

if __name__ == "__main__":
   text = open_file()
   main(text)

這是我的實際輸出:

$ python script8.py

'alice' - > 403

'我' - > 283

'它' - > 205

's' - > 184

'小' - > 128

'你' - > 115

'和' - > 107

'一' - > 106

'gutenberg' - > 93

'那' - > 92

我期待的是,所有“i”,“it”和“you”的實例都被排除在此列表之外,但它們仍然出現,我不清楚為什么。

您的代碼執行此操作:

  1. 首先,使用text.split()在空白上拆分文本。 但由此產生的“話”名單仍包括標點符號,如as,head!' 'i (注意'用作引號和撇號)。

  2. 然后排除任何在stopwords詞中匹配的“單詞”。 這將排除i但不是'i

  3. 接下來,使用空格重新連接所有剩余的單詞。

  4. 然后你使用'\\w+'正則表達式來搜索字母序列(不包括標點符號):所以'i將匹配為i 這就是為什么is都出現在你的前10名了。

有幾種方法可以解決這個問題。 例如,您可以使用re.split()來拆分不僅僅是空格:

def main(text):
   stoplist = stopwords.words('english')
   stoplist.extend(["said"]) # stoplist already includes "i", "it", "you"
   clean = [word for word in re.split(r"\W+", text) if word not in stoplist]
   top_10 = Counter(clean).most_common(10)
   for word,count in top_10:
       print(f'{word!r:<4} {"-->":^4} {count:>4}')

輸出:

'alice' -->   403
'little' -->   128
'one' -->   106
'gutenberg' -->    93
'know' -->    88
'project' -->    87
'like' -->    85
'would' -->    83
'went' -->    83
'could' -->    78

請注意,這是分開處理帶連字符的短語:所以gutenberg-tm - > gutenbergtm 為了更好地控制這個,你可以按照Jay的建議看看nltk.tokenize 例如,nltk tokenizer知道收縮,所以don't - > do + n't

您還可以通過從文本中刪除Gutenberg許可條件來改進工作:)

例如:

"it's".split() >> [它是]

re.findall('\\w+', "it's") >> [它,s]

這就是為什么“停止列表”不會像你想的那樣。

固定:

def main(text):
    words = re.findall('\w+', text)
    counter = Counter(words)
    stoplist = stopwords.words('english')
    #stoplist.extend(["said", "i", "it", "you", "and", "that", ])
    stoplist.extend(["said", "i", "it", "you"])
    [stoplist.remove(keep_word) for keep_word in ['s', 'and', 'that']]
    for stop_word in stoplist:
        del counter[stop_word]
    for word, count in counter.most_common(10):
        print(f'{word!r:<4} {"-->":^4} {count:>4}')

產量

'and' -->   940
'alice' -->   403
'that' -->   330
's'  -->   219
'little' -->   128
'one' -->   106
'gutenberg' -->    93
'know' -->    88
'project' -->    86
'like' -->    85

注意: "i", "it" and "you" to be excluded from your list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM