【问题标题】:Using a dictionary to assign misspelled words to its line number使用字典将拼写错误的单词分配给其行号
【发布时间】:2010-05-23 12:05:45
【问题描述】:

这是我目前拥有的代码:

from collections import defaultdict

goodwords = set()

with open("soccer.txt", "rt") as f:
     for word in f.readlines():
        goodwords.add(word.strip())

badwords = defaultdict(list)

with open("soccer.txt", "rt") as f:
    for line_no, line in enumerate(f):
        for word in line.split():
            if word not in text:
                badwords[word].append(line_no)

print(badwords)

如何修复我的代码,以便打印存储在 words 列表中的错误单词和行号?

例如,如果单词 togeher 在第 5 行和第 7 行拼写错误,则会打印如下内容:

togeher 5 7

【问题讨论】:

  • 您需要格式化代码,以便我们可以运行它 - 我做了一个简单的格式,但这缺少一些缩进
  • 不计算行数;使用len(words)
  • 如果您遇到错误并想询问相关问题,请告诉我们您遇到的什么错误。
  • 我得到的错误是 d[word].append(counter) KeyError: 'a'

标签: python dictionary spell-checking


【解决方案1】:

将新的counter 插入d 时,首先检查word 是否包含在words 中。可能您想检查word 是否已包含在d 中:

if word not in d:
    d[word] = [counter]
else:
    d[word].append(counter)

检查word 是否包含在wordsline 中应该是单独的if

您还可以使用 dicts setdefault() 方法简化此逻辑:

d.setdefault(word, []).append(counter)

或者您将d 设为defaultdict,这样可以更加简化分配:

from collections import defaultdict
d = defaultdict(list)
...
d[word].append(counter)

关于一般算法请注意,在您第一次遍历所有行以递增计数器时,当计数器已经达到最大值时,开始检查拼写错误的单词。可能您应该检查循环中增加计数器的每一行。

【讨论】:

  • 文本文件实际上叫做soccer.txt,但我使用sys.argv 我只编程了2个月,所以我不明白一切。我将 if word not in words 更改为 if words not in d 但我仍然收到错误 print(word, d[counter]) keyerror: 329
  • 我有一个不正确单词的列表,并希望将我的 txt 文件中不正确单词的行号打印到一个集合中,然后它会打印出 helo 5 8 # 5 和 8 是txt 文件中的行号,尽管有任何关于如何做到这一点的建议 plzzzz
【解决方案2】:

形成你正在做的事情,我怀疑以下内容会非常适合你:

from collections import defaultdict

text = ( "cat", "dog", "rat", "bat", "rat", "dog",
         "man", "woman", "child", "child") #

d = defaultdict(list)

for lineno, word in enumerate(text):
    d[word].append(lineno)

print d

这会给你一个输出:

defaultdict(<type 'list'>, {'bat': [3], 'woman': [7], 'dog': [1, 5],
                            'cat': [0], 'rat': [2, 4], 'child': [8, 9],
                            'man': [6]})

这只是设置一个空的默认字典,其中包含您访问的每个项目的列表,这样您就不必担心创建条目,然后在单词列表中枚举它,所以您不需要需要跟踪行号。

由于您没有正确拼写的列表,这实际上不会检查单词是否拼写正确,只是构建一个包含文本文件中所有单词的字典。

要将字典转换为一组单词,请尝试:

all_words = set(d.keys())
print all_words

产生:

set(['bat', 'woman', 'dog', 'cat', 'rat', 'child', 'man'])

或者,只是打印单词:

for word in d.keys():
    print word

编辑 3:

我认为这可能是最终版本: 这是一个(故意)非常粗糙但几乎完整的拼写检查器。

from collections import defaultdict

# Build a set of all the words we know, assuming they're one word per line
good_words = set() # Use a set, as this will have the fastest look-up time.
with open("words.txt", "rt") as f:
    for word in f.readlines():
        good_words.add(word.strip())

bad_words = defaultdict(list)

with open("text_to_check.txt", "rt") as f:
    # For every line of text, get the line number, and the text.
    for line_no, line in enumerate(f):
        # Split into seperate words - note there is an issue with punctuation,
        # case sensitivitey, etc..
        for word in line.split():
            # If the word is not recognised, record the line where it occurred.
            if word not in good_words:
                bad_words[word].append(line_no)

最后,bad_words 将是一个字典,其中未识别的单词作为键,单词所在的行号作为匹配值条目。

【讨论】:

  • 我实际上确实有一个名为 dictset = [] 的正确拼写列表,它是一个包含许多单词的字典,但是我试试这个,谢谢这是我有一个包含单词的 txtfile 一个不正确单词的列表和我只想将不正确单词的 lne 编号相互附加
  • 你说的有效,但我希望它作为一个集合打印我有一个集合,但我只能将单词作为一个集合打印
  • for inwords in wrongwords: print(inwords) 这会打印出一组我不正确的单词,但是我该如何处理你给我看的代码呢?欢呼
  • 我感谢你所做的一切,并相信如果我再添加一件事情,它应该可以工作,而不是打印不正确单词的行号我想打印位于 txt 中的不正确单词的行号文件我会添加什么?我试图在 txtfile 中添加 if 字:???
  • 更新为一个最小但完整的示例
猜你喜欢
  • 2018-12-07
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2010-10-16
  • 2018-11-25
  • 1970-01-01
  • 2018-03-31
相关资源
最近更新 更多