识别与数字相关的单词

时间:2018-11-18 04:05:24

标签: 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(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!")

还是我需要调用该函数?

4 个答案:

答案 0 :(得分:1)

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

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())   # <----- ***

答案 1 :(得分:1)

首先,您应该创建一个从字母到数字的映射

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)

然后,您应该构建有效的digits字典

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

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')

答案 2 :(得分:0)

我的方法会有所不同。

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

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]

我将创建一个辅助函数以提高可读性

def intersect_lists(list1, list2):

    if [value for value in list1 if value in list2]:
        return True
    else:
        return False

将所有内容放在一起应与包含的所有可能单词匹配 例如:221-12183如果在'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!")

答案 3 :(得分:0)

这就是我要做的:

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

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

(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