0

I have the code:

f = open("dict.txt", "r")
M = []
for line in f:
    l = line.split(" ")
    M.append(l)
print(M)
phone_letters = [
    ["a", "b", "c"],
    ["d", "e", "f"],
    ["g","h","i"],
    ["j","k","l"],
    ["m","n","o"],
    ["p","q","r","s"],
    ["t","u","v"],
    ["w","x","y","z"]
    ]
t = 0
k = 0
max = 100
for i in range(len(phone_letters)):
    for quantity in range(1, 20):
        if t == 1:  # > 9 - error7
            break
        if t == 2:
            break   # End of word
        if t == 4:
            break   #«MANUALLY».
        max = 100
        vvod_2 = int(input("Enter the next digit: "))
        if vvod_2 != 1:
            for i in range(len(phone_letters)):
                if (vvod_2 - 2) == i:
                    for i_2 in range(len(M)):
                        if len(M[i_2][0]) >= quantity:
                            if int(M[i_2][1][:-1]) < max:
                                for i_3 in range(len(phone_letters[i])):
                                    if M[i_2][0][(quantity - 1)] == phone_letters[i][i_3]:
                                        max = int(M[i_2][1][:-1])
                                        k = M[i_2][0][:quantity]
                                        t = 0
        quantity = quantity + 1
        print(k)
        if vvod_2 > 9:
            print("Error")
            t = 1
        if vvod_2 == 1:
            for i_last in range(len(M)):
                if str(k) == M[i_last][0]:
                    print("End of word.")
                    t = 2
                else:
                    t = 4

But this program doesn't work correct. I have the file "dict.txt", that looks like:

hello 27

play 32 

good 45

These numbers mean the probability of encountering the word. My program works, but it gives the results by every letter, not looking on the letters before.

For example, when we have: day 37 and after that we don't have another word, that starts with the same letter - after running the program by typing 3 - "d", typing again 3 - the result need to be "Manually", because my dictionary hasn't got a word, that starts on the same letter. The program above should find the word by the second letter in spite of the letters before. Help, please!

DanMan
  • 11,323
  • 4
  • 40
  • 61
  • You haven't explained what this code is supposed to do. – PM 2Ring May 15 '16 at 09:34
  • I don't understand what you're trying to do, reformulate if you want help. Meaningful variable names would help too. – polku May 15 '16 at 09:36
  • This is T9 system: when user types 2 - program will search through letters "d, e, f" like on old mobile phones. For example: when user types firstly: 4 - it will be "h", secondly : 3 - "he", 5 - "hel", 5 - "hell", 6 - "hello", 1 - is the end of the word. – Alexander Welbo May 15 '16 at 10:08
  • My problem is that program looks only at the current letter. If user will type firstly the number, which letter will be in my dictionary - program will show it (for example: "d"), secondly if he write the number that my dictionary doesn't have with first letter( for example: "de" - i haven't written the words, that starts like that), but has for example : "te" - my dict has the word "tea" - it will show "te", not looking through letters before. In right program it need to show: "Manually" – Alexander Welbo May 15 '16 at 10:15
  • 1
    Note that you are modifying `quantity` loop variable inside the loop. Unlike to C `for` loops in python you cannot do the trick, the variable gets reassined on this line: `for quantity in range(1, 20)`. Check, this may affect your algo. – robyschek May 15 '16 at 10:32
  • There is too much in this code I don't understand to be able to reason about it : why are you iterating over phone_letters in your main loop instead of just a while loop ? why not use a dict ? what is t ? why if vvod_2 > 9 is an error do you still execute the code before ? why using a loop 'for i in range(len(phone_letters)):' when after that you use 'if (vvod_2 - 2) == i:' so your loop is really run just once ? Try to cleanup and maybe you'll even solve your problem alone. – polku May 15 '16 at 11:15

1 Answers1

0

I recently wrote such an algorithm as part of training. I hope it may be useful. It work with system dictionary, and you should import pathlib

DEFAULT_PATH_TO_WORDS_FILE = '/usr/share/dict/words'
PHONE_KEYS_CONFORMITY = {'2': 'abc',
                         '3': 'def',
                         '4': 'ghi',
                         '5': 'jkl',
                         '6': 'mno',
                         '7': 'pqrs',
                         '8': 'tuv',
                         '9': 'wxyz'}


def get_system_words(path_to_file: str) -> Tuple:
    with pathlib.Path(path_to_file).open(mode='r') as stream:
        file_content = stream.read()
    words_tuple = tuple(word.strip() for word in file_content.split('\n'))
    return words_tuple


def my_t9(input_numbers: str) -> List[str]:
    system_words = get_system_words(DEFAULT_PATH_TO_WORDS_FILE)
    filtered_system_words_by_len = tuple(filter(
        lambda word: len(word) == len(input_numbers),
        system_words))
    filtered_system_words_by_input = []
    for word in filtered_system_words_by_len:
        word_corresponds = True
        for index, letter in enumerate(word):
            if letter not in PHONE_KEYS_CONFORMITY[f'{input_numbers[index]}']:
                word_corresponds = False
                break
        if word_corresponds:
            filtered_system_words_by_input.append(word)
    return filtered_system_words_by_input