【问题标题】:How to perform t9 search, using python?如何使用 python 执行 t9 搜索?
【发布时间】:2016-05-15 09:17:41
【问题描述】:

我有代码:

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 - 结果需要是“手动”,因为我的字典没有一个单词,它以同一个字母开头。尽管前面有字母,上面的程序应该通过第二个字母找到单词。请帮忙!

【问题讨论】:

  • 你还没有解释这段代码应该做什么。
  • 我不明白你在做什么,如果你需要帮助,请重新制定。有意义的变量名也会有所帮助。
  • 这是 T9 系统:当用户键入 2 - 程序会像在旧手机上一样搜索字母“d, e, f”。例如:当用户首先输入:4 - 它将是“h”,其次:3 - “he”,5 - “hel”,5 - “hell”,6 - “hello”,1 - 是结束字。
  • 我的问题是程序只查看当前字母。如果用户首先输入数字,哪个字母将在我的字典中 - 程序将显示它(例如:“d”),其次如果他写我的字典没有第一个字母的数字(例如:“ de” - 我没有写过这样开头的单词),但是有例如:“te” - 我的字典有“tea”这个词 - 它会显示“te”,而不是以前看的字母。在正确的程序中,它需要显示:“手动”
  • 请注意,您正在修改循环内的quantity 循环变量。与 python 中的 C for 循环不同,你无法做到这一点,变量在这一行被重新定义:for quantity in range(1, 20)。检查一下,这可能会影响您的算法。

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


【解决方案1】:

我最近写了一个这样的算法作为训练的一部分。我希望它可能有用。它适用于系统字典,您应该导入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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多