views:

42

answers:

2

I'm trying to understand an iterative function that that takes a string "12345" and returns all the possible misspellings based upon a dictionary of keys close to each character in the string.

outerDic = {}
Dict1 = {'1':'2','2':'q'}
outerDic['1'] = Dict1
Dict1 = {'1':'1','2':'q','3':'w','4':'3'}
outerDic['2'] = Dict1
Dict1 = {'1':'2','2':'w','3':'e','4':'4'}
outerDic['3'] = Dict1
Dict1 = {'1':'3','2':'e','3':'r','4':'5' }
outerDic['4'] = Dict1
Dict1 = {'1':'4','2':'r','3':'t','4':'6' }
outerDic['5'] = Dict1
outerDic

The output should return a list of strings

12345
22345
q2345
11345
1q345
13345
12245
12e45
12445

and so on...

I've set the function up as follows:

def split_line(text):
 words = text.split()
 for current_word in words:
  getWordsIterations()

I'd like to understand how to set up the getWordsIterations () function to go through the dictionary and systematically replace the characters.

Thanks in advance, kind of new to pythong.

+1  A: 

I'm not sure what the inner dicts, all with keys '1', '2', etc, signify -- are they basically just stand-ins for lists presenting possible typos? But then some (but not all) would also include the "right" character... a non-typo...?! Sorry, but you're really being extremely confusing in this presentation -- the example doesn't help much (why is there never a "w" in the second position, which is supposed to be a possible typo there if I understand your weird data structure...? etc, etc).

So, while awaiting clarification, let me assume that all you want is to represent for each input character all possible single-character typos for it -- lists would be fine but strings are more compact in this case, and essentially equivalent:

possible_typos = {
  '1': '2q',
  '2': '1qw3',
  '3': '2we4',
  '4': '3er5',
  '5': '4rt6',
}

now if you only care about cases with exactly 1 mis-spelling:

def one_typo(word):
  L = list(word)
  for i, c in enumerate(L):
    for x in possible_typos[c]:
      L[i] = x
      yield ''.join(L)
    L[i] = c

so for example, for w in one_typo("12345"): print w emits:

22345
q2345
11345
1q345
1w345
13345
12245
12w45
12e45
12445
12335
123e5
123r5
12355
12344
1234r
1234t
12346

"Any number of typos" would produce an enormous list -- is that what you want? Or "0 to 2 typos"? Or what else exactly...?

Alex Martelli
A: 

Thanks Alex, that was exactly what I was looking for. I was making the problem overly complex!

Jason Vance