0

我正在编写一个拼写检查功能,我有一个看起来像这样的文本文件

teh the
cta cat
dgo dog
dya day
frmo from
memeber member

错误的拼写在左边(这将是我的钥匙),正确的拼写在右边(我的价值)。

def spell():
    corrections=open('autoCorrect.txt','r')
    dictCorrect={}
    for line in corrections:
        corrections[0]=[1]
        list(dictCorrect.items())

我知道我想让我的函数做什么,但不知道如何执行它。

4

2 回答 2

5

用这个:

with open('dictionary.txt') as f:
    d = dict(line.strip().split(None, 1) for line in f)

d是字典。

免责声明: 这适用于您上面说明的简单结构,对于更复杂的文件结构,您需要进行更复杂的解析。

于 2013-07-16T15:51:19.733 回答
0

您可能想使用 split 来获取单词,然后将拼写错误的单词映射到正确拼写的单词:

def spell():
  dictCorrect={}
  with open('autoCorrect.txt','r') as corrections:        
    for line in corrections:
      wrong, right = line.split(' ')
      dictCorrect[wrong] = right
  return dictCorrect
于 2013-07-16T15:52:47.440 回答