Python 查找“的总数”;停止语;归档
我正在尝试创建一个Python程序,它读取两个文本文件,一个包含一篇文章,另一个包含“停止词”(每行一个词)列表。我想确定我正在使用的包含文章的特定文本文件(每个“停止词”出现频率的累计总和)中有多少“停止词” 我尝试创建嵌套的Python 查找“的总数”;停止语;归档,python,for-loop,readfile,readlines,Python,For Loop,Readfile,Readlines,我正在尝试创建一个Python程序,它读取两个文本文件,一个包含一篇文章,另一个包含“停止词”(每行一个词)列表。我想确定我正在使用的包含文章的特定文本文件(每个“停止词”出现频率的累计总和)中有多少“停止词” 我尝试创建嵌套的for循环,以便在包含文章的文件的每一行中循环(外部为循环),在每一行中有一个for循环(内部为循环),循环“停止字”列表,并查看“停止字”是否在当前行中,如果是,频率如何。最后,我将单词在当前行中的频率添加到累加器中,累加器将跟踪在包含文章的文件中找到的停止词的累计总量
for
循环,以便在包含文章的文件的每一行中循环(外部为循环),在每一行中有一个for循环(内部为循环),循环“停止字”列表,并查看“停止字”是否在当前行中,如果是,频率如何。最后,我将单词在当前行中的频率添加到累加器中,累加器将跟踪在包含文章的文件中找到的停止词的累计总量
目前,当我运行它时,它说文件中有0个停止字,这是不正确的
import string
def main():
analyzed_file = open('LearnToCode_LearnToThink.txt', 'r')
stop_word_file = open('stopwords.txt', 'r')
stop_word_accumulator = 0
for analyzed_line in analyzed_file.readlines():
formatted_line = remove_punctuation(analyzed_line)
for stop_word_line in stop_word_file.readlines():
stop_formatted_line = create_stopword_list(stop_word_line)
if stop_formatted_line in formatted_line:
stop_word_frequency = formatted_line.count(stop_formatted_line)
stop_word_accumulator += stop_word_frequency
print("there are ",stop_word_accumulator, " words")
stop_word_file.close()
analyzed_file.close()
def create_stopword_list(stop_word_text):
clean_words = [] # create an empty list
stop_word_text = stop_word_text.rstrip() # remove trailing whitespace characters
new_words = stop_word_text.split() # create a list of words from the text
for word in new_words: # normalize and add to list
clean_words.append(word.strip(string.punctuation).lower())
return clean_words
def remove_punctuation(text):
clean_words = [] # create an empty list
text = text.rstrip() # remove trailing whitespace characters
words = text.split() # create a list of words from the text
for word in words: # normalize and add to list
clean_words.append(word.strip(string.punctuation).lower())
return clean_words
main()
你有很多问题:
readlines
只工作一次-之后,您位于文件末尾,它将返回一个空字符串另一个列表中的一个列表
和一个列表。计数(另一个列表)
不要做你认为他们会做的事stop_words = get_stop_word_list(stop_words_file_name)
stop_word_count = 0
with open(other_file_name) as other_file: # note 'context manager' file handling
for line in other_file:
cleaned_line = clean(line)
for stop_word in stop_words:
if stop_word in cleaned_line:
stop_word_count += cleaned_line.count(stop_word)
有更有效的方法(例如使用
set
s和collections.Counter
s),但这应该可以让您开始学习。您可以使用NLTK检查停止字并计数:
from nltk.corpus import stopwords
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
nltk.download('punkt')
x = r"['Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura, ché la
diritta via era smarrita.Ahi quanto a dir qual era è cosa dura esta selva selvaggia
e aspra e forte che nel pensier rinova la paura! Tant' è amara che poco è più morte;
ma per trattar del ben ch'i' vi trovai, dirò de l altre cose chi v ho scorte.']"
word_tokens = word_tokenize(x) #splitta i pezzi
stopwords_x = [w for w in word_tokens if w in stopWords]
len(stopwords_x) / len(word_tokens) * 100
我建议将内部for循环替换为
stop\u word\u count+=sum(map(cleaned\u line.count,stop\u words))
(可能将map
替换为imap
)。您在调用count
之前检查单词是否存在有什么原因吗?好的,我会在@jornsharpe尝试一下,让您知道这是否有效,如果无效,我会发布我修改过的代码to@AlexHall主要是为了让它合理地接近OP目前试图做的事情!它的效率比你建议的要低一些,而且如果你使用字典,每行只需一次就可以做到。@heyyo9028你在开玩笑吧?缩进在Python中很重要,我们应该如何在注释框中阅读它?好吧,我会把我现在的东西放在我的原始帖子里“stopwords\ux=[w代表w,如果w代表stopwords.words('english)]”对我有用