Python 识别与数字相关的单词

Python 识别与数字相关的单词,python,python-3.x,Python,Python 3.x,我需要这项任务的帮助: 在本作业中,您将识别与电话号码相关的单词 通过使用字典。您将阅读三个和四个字母的单词,然后让用户提供一个数字。使用该数字,您将确定与该数字关联的单词 我不确定要创建什么函数来完成此代码。这就是我所拥有的: def helper(a, b): result = [] for i in a: for j in b: result.append(i+j) return result def helper2(nu

我需要这项任务的帮助: 在本作业中,您将识别与电话号码相关的单词 通过使用字典。您将阅读三个和四个字母的单词,然后让用户提供一个数字。使用该数字,您将确定与该数字关联的单词

我不确定要创建什么函数来完成此代码。这就是我所拥有的:

def helper(a, b):
    result = []
    for i in a:
        for j in b:
            result.append(i+j)

    return result

def helper2(num,look_up):
    result = [""]
    for x in num:
        result = helper(result, list(look_up[x]))
    return result

words = []
with open("words.txt") as file:
    for word in file.readlines():
        word = word.strip()
        if len(word) == 3 or len(word) == 4:
            words.append(word)

look_up = {'0': ['O'],
           '1': ['I', 'L'],
           '2': ['A', 'B', 'C'],
           '3': ['D', 'E', 'F'],
           '4': ['G', 'H', 'I'], 
           '5': ['J', 'K', 'L'],
           '6': ['M', 'N', 'O'], 
           '7': ['P', 'Q', 'R', 'S'],
           '8': ['T', 'U', 'V'], 
           '9': ['W', 'X', 'Y', 'Z']}

response = "Y"
while response.upper() == 'Y':
    phone = input("Please enter a phone number: ")
    #7phone = "223-5653"
    number = ""
    for letter in phone:
        if letter.isdigit():
                number += letter

    num = number[:3]
    result3 = helper2(num, look_up)

    num = number[3:]
    result4 = helper2(num, look_up)

    print("Results include...")
    for a in result3:
        for b in result4:
            if a in words and b in words:
                print("{}-{}".format(a, b))

    response = input("Try another (Y/N)? ")

print("Good Bye!")

还是我需要调用该函数?

也许,您必须将字母转换为大写以便进行比较:

with open("words.txt") as file:
    for word in file.readlines():
        word = word.strip()    
        if len(word) == 3 or len(word) == 4:
            words.append(word.upper())   # <----- ***
打开(“words.txt”)作为文件:
对于文件.readlines()中的word:
word=word.strip()
如果len(字)==3或len(字)==4:

words.append(word.upper())#首先,您应该创建一个从字母到数字的映射

letter2digit = {'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 2} # etc..
# Extend to uppercase
letter2digit = dict(list(letter2digit.items()) + [(l.upper(), d) for l,d in letter2digit.items()])
# create function to convert a word
word2digits = lambda word: ''.join(str(letter2digit[l]) for l in word)
然后,您应该构建有效的
数字
字典

我正在使用
collections
模块中的
defaultdict
函数,它非常有用

from collections import defaultdict

# Read the words from the file
words = []
with open("words.txt", "r") as f:
    words = [l.strip() for l in f.readlines()]

# Translate the words to their corresponding digits
digits = defaultdict(set)
for word in words:
    digits[word2digits(word)].add(word)
最后,查找如下所示:

# All possible words of 1155
print (digits["1155"])
# Check if 'ball' is associated with 1155
if "ball" in digits["1155"]:
    print ('you typed ball')

我的方法会有点不同

首先创建给定单词到相应数字的映射

import itertools


def word_to_number(word: str):
    look_up = {'0': ['O'],
               '1': ['I', 'L'],
               '2': ['A', 'B', 'C'],
               '3': ['D', 'E', 'F'],
               '4': ['G', 'H', 'I'],
               '5': ['J', 'K', 'L'],
               '6': ['M', 'N', 'O'],
               '7': ['P', 'Q', 'R', 'S'],
               '8': ['T', 'U', 'V'],
               '9': ['W', 'X', 'Y', 'Z']}

    numeric_representation = []
    for letter in word:
        value = [k for k in look_up if letter.upper() in look_up[k]]
        numeric_representation.append(value)

    # the mapping number to letter is not one-to-one
    # word "blue" can be 2183 or 2583
    cartesian_product = itertools.product(*numeric_representation)
    numeric_values = ["".join(i) for i in cartesian_product]

    return numeric_values
电话号码将被拆分为所有连续的3、4(等)位数字组,仅包含:

def parse_number(phone_number: str, word_lengths: set):
    digits_only = [c for c in phone_number if c.isdigit()]
    possible_values = []

    # this can accommodate longer words lookup
    for length in word_lengths:
        i = 0
        while i < len(digits_only) - (length - 1):
            value = digits_only[i:length+i]
            possible_values.append(value)
            i += 1

    return ["".join(val) for val in possible_values]
把它们放在一起应该匹配所有可能包含的单词 例:221-12183将匹配“blue”和“ball”,如果它们存在于“words.txt”中

if __name__ == "__main__":

    # create a dict of {word_from_document: [possible_values]}
    given_words = {}

    with open("words.txt", "r") as file:
        for word in file.readlines():
            word = word.strip()
            if len(word) in [3, 4]:
                given_words[word] = word_to_number(word)

    given_words_lengths = set(len(w) for w in given_words)

    response = "Y"
    while response.upper() == 'Y':
        phone = input("Please enter a phone number: ")
        phone_values = parse_number(phone, given_words_lengths)

        matches = [word for word in given_words
                   if intersect_lists(given_words[word], phone_values)]

        print("Results include... \n\t{results}".format(
              results="\n\t".join(matches)))

        response = input("Try another (Y/N)? ")

    print("Good Bye!")

下面是我要做的:

(1) 根据您提供的查找词典,生成由一串数字生成的所有可能的字母组合

(2) 将words.txt中的单词分为三个字母的单词和四个字母的单词

def number_to_words (number):
    # Step 1
    threeLetterWordCandidates = genCombinations(number[:3])
    fourLetterWordCandidates = genCombinations(number[3:])

    # Step 2
    threeLetterWords = loadWords(word_length = 3)
    fourLetterWords = loadWords(word_length = 4)

    # Step 3
    threeLetterWordMatches = genMatches(threeLetterWordCandidates, threeLetterWords)
    fourLetterWordMatches = genMatches(fourLetterWordCandidates, fourLetterWords)

    # Step 4
    allWordCombinations = []
    for threeLetterWord in threeLetterWordMatches:
        for fourLetterWord in fourLetterWordMatches:
            allWordCombinations.append(threeLetterWord + '-' + fourLetterWord)
    return allWordCombinations
(3) 创建步骤1中生成的字符串列表,这些字符串与words.txt中有效单词列表中的任何单词匹配

(4) 最后:遍历匹配列表,生成三个字母和四个字母单词的所有可能组合

def number_to_words (number):
    # Step 1
    threeLetterWordCandidates = genCombinations(number[:3])
    fourLetterWordCandidates = genCombinations(number[3:])

    # Step 2
    threeLetterWords = loadWords(word_length = 3)
    fourLetterWords = loadWords(word_length = 4)

    # Step 3
    threeLetterWordMatches = genMatches(threeLetterWordCandidates, threeLetterWords)
    fourLetterWordMatches = genMatches(fourLetterWordCandidates, fourLetterWords)

    # Step 4
    allWordCombinations = []
    for threeLetterWord in threeLetterWordMatches:
        for fourLetterWord in fourLetterWordMatches:
            allWordCombinations.append(threeLetterWord + '-' + fourLetterWord)
    return allWordCombinations
对该函数的调用可能与您提供的类似:

response = "Y"
while response.upper() == 'Y':
    phone = input("Please enter a phone number: ")
    number = ""
    for letter in phone:
        if letter.isdigit():
                number += letter

    print("Results include...")
    print(number_to_words(number))

    response = input("Try another (Y/N)? ")

print("Good Bye!")
下面的帮助器函数说明了如何执行以下每个步骤:

look_up = {'0':['O'],
           '1':['I','L'],
           '2':['A','B','C'],
           '3':['D','E','F'],
           '4':['G','H','I'],
           '5':['J', 'K', 'L'],
           '6':['M','N','O'],
           '7':['P','Q','R','S'],
           '8':['T','U','V'],
           '9':['W','X','Y','Z']}


def loadWords (word_length):
    words = []
    file = open('words.txt', 'r');
    for line in file.readlines():
        if len(line) == word_length + 1:
            words.append(line[:-1])
    file.close()
    return words

def genCombinations (num_str):
    def recGenCombinations (combinations, num_str):
        if num_str == "":
            return combinations
        else:
            newCombinations = []
            letters = look_up[num_str[0]]
            for letter in letters:
                for str in combinations:
                    newCombinations.append((str+letter).lower())
            return recGenCombinations(newCombinations, num_str[1:])
    combinations = []
    for letter in look_up[num_str[0]]:
        combinations.append(letter)
    return recGenCombinations(combinations,num_str[1:])


def genMatches (combinations, valid_words):
    matches = []
    for str in combinations:
        if str in valid_words:
            matches.append(str)
    return matches