如何使用python执行t9搜索?

时间:2016-05-15 09:17:41

标签: python sorting python-3.x dictionary t9

我有代码:

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

但是这个程序不正确。我有文件“dict.txt”,看起来像:

hello 27

play 32 

good 45

这些数字表示遇到该单词的概率。我的程序有效,但它会按照每个字母给出结果,而不是查看之前的字母。

例如,当我们有:day 37之后我们没有另一个单词,以相同的字母开头 - 在运行程序后输入3 - “d”,再次输入3 - 结果需要是“手动”,因为我的字典没有一个字,从同一个字母开始。尽管之前的字母,上面的程序应该通过第二个字母找到单词。请帮忙!

0 个答案:

没有答案