簡體   English   中英

在不使用 nltk 的情況下刪除文本文件中的停用詞

[英]remove stop words in a text file without using nltk

大家好,我想在不使用 nltk 的情況下刪除文本文件中的停用詞。 我有一個文本文件,其中包含用於停止的停用詞列表,我想使用上面提到的停用詞列表。 謝謝你

雖然很難理解確切的要求,但我會做以下事情:

with open("stopwords.txt") as f:
    stopwords = f.read().splitlines() # Contains "and" and "or" on different lines

text = "Foo and bar or foo"
tokens = text.split() # Split into list of words
for word in tokens: 
    if word.lower() in stopwords: # If word in stopwords remove it
        tokens.remove(word)
clean_text = " ".join(word for word in tokens) # Join words into a string
print(clean_text) # Outputs: "Foo bar foo"

暫無
暫無

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

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