查看文中找到的最常用的7个词,但整理出属于常用词的词
View the 7 most common words found in the text, but sorting out the words that are common words
真的需要一些帮助来解决这个问题,或者如果有人能指出正确的方法,谢谢!
查看文中找到的最常见的7个词,但整理出属于常用词的词。您可以在 common-words.txt.
中找到常用词列表
common-words.txt = 很多不同的词。
首先我找到了文本中最常见的 7 个单词,这就是我的代码的样子。
print("The 7 most frequently used words is:")
print("\n")
import re
from collections import Counter
with open("alice-ch1.txt") as f:
passage = f.read()
words = re.findall(r'\w+', passage)
cap_words = [word.upper() for word in words]
word_counts = Counter(cap_words).most_common(7)
print(word_counts)
这有效,我得到输出:
[('THE', 93), ('SHE', 80), ('TO', 75), ('IT', 67), ('AND', 65), ('WAS', 53), ('A', 52)]
现在我想比较这两个文本文件,如果我的 TEXTFILE.TXT 中的任何单词在 COMMON-WORDS.TXT 中,我希望它从答案中删除。
我已经尝试 运行 使用此代码:
dic_no_cw = dict(word_counts)
with open("common-words.txt", 'r') as cw:
commonwords = list(cw.read().split())
for key, value in list(dic_no_cw.items()):
for line in commonwords:
if key == line:
del dic_no_cw[key]
dict_copy = dict(dic_no_cw)
dic_no_cw7 = Counter(dic_no_cw).most_common(7)
sorted(dic_no_cw7)
print(dic_no_cw7)
我得到相同的输出:
[('THE', 93), ('SHE', 80), ('TO', 75), ('IT', 67), ('AND', 65), ('WAS', 53), ('A', 52)]
真的可以使用 som 帮助来解决这个问题或一些帮助,所以我也许可以自己解决。
谢谢,
我没有检查过,但我认为你可能只是在检查字典中的值(代表单词出现的次数)而不是检查键(实际单词本身) 与常用词列表中的词进行比较时:
我认为 if value == line:
应该读作 if key == line:
。
您能否尝试替换这些代码行:
for line in commonwords:
if key == line:
del dic_no_cw[key]
和
for line in commonwords:
if key.strip() == line.upper().strip():
del dic_no_cw[key]
break
真的需要一些帮助来解决这个问题,或者如果有人能指出正确的方法,谢谢!
查看文中找到的最常见的7个词,但整理出属于常用词的词。您可以在 common-words.txt.
中找到常用词列表common-words.txt = 很多不同的词。
首先我找到了文本中最常见的 7 个单词,这就是我的代码的样子。
print("The 7 most frequently used words is:")
print("\n")
import re
from collections import Counter
with open("alice-ch1.txt") as f:
passage = f.read()
words = re.findall(r'\w+', passage)
cap_words = [word.upper() for word in words]
word_counts = Counter(cap_words).most_common(7)
print(word_counts)
这有效,我得到输出:
[('THE', 93), ('SHE', 80), ('TO', 75), ('IT', 67), ('AND', 65), ('WAS', 53), ('A', 52)]
现在我想比较这两个文本文件,如果我的 TEXTFILE.TXT 中的任何单词在 COMMON-WORDS.TXT 中,我希望它从答案中删除。
我已经尝试 运行 使用此代码:
dic_no_cw = dict(word_counts)
with open("common-words.txt", 'r') as cw:
commonwords = list(cw.read().split())
for key, value in list(dic_no_cw.items()):
for line in commonwords:
if key == line:
del dic_no_cw[key]
dict_copy = dict(dic_no_cw)
dic_no_cw7 = Counter(dic_no_cw).most_common(7)
sorted(dic_no_cw7)
print(dic_no_cw7)
我得到相同的输出:
[('THE', 93), ('SHE', 80), ('TO', 75), ('IT', 67), ('AND', 65), ('WAS', 53), ('A', 52)]
真的可以使用 som 帮助来解决这个问题或一些帮助,所以我也许可以自己解决。
谢谢,
我没有检查过,但我认为你可能只是在检查字典中的值(代表单词出现的次数)而不是检查键(实际单词本身) 与常用词列表中的词进行比较时:
我认为 if value == line:
应该读作 if key == line:
。
您能否尝试替换这些代码行:
for line in commonwords:
if key == line:
del dic_no_cw[key]
和
for line in commonwords:
if key.strip() == line.upper().strip():
del dic_no_cw[key]
break