翻译使用字典

时间:2018-06-17 19:48:41

标签: python dictionary python-3.6

我正在努力解决Pieter Spronck在13.2.4节中的书 The Coder's Apprentice 中可以找到的问题。这是我到目前为止编写的代码:

english_dutch = {"last":"laatst", "week":"week", "the":"de", "royal":"koninklijk",
    "festival":"feast", "hall":"hal", "saw":"zaag", "first":"eerst", "performance":"optreden",
    "of":"van", "a":"een", "new":"nieuw", "symphony":"symphonie", "by":"bij",
    "one":"een", "world":"wereld", "leading":"leidend", "modern":"modern",
    "composer":"componist", "composers:componisten" "two":"twee", "shed":"schuur", "sheds":"schuren"}

text = "Last week The Royal Festival Hall saw the first \
 performance of a new symphony by one of the world's leading \
 modern composers, Arthur 'Two-Sheds' Jackson."


def clean(t):
    t = t.lower()
    t = t.split()
    new_t = ""
    for word in t:
        new_word = ""
        for letter in word:
            if "a" <= letter <= "z":
                new_word += letter
            if letter == "-":
                new_word += " "
            else:
                continue
        new_t += new_word + " "
    return new_t


def translate(t):
    translation = ""
    for word in t.split():
        if english_dutch.get(word):
            translation += english_dutch[word] + " "
        else:
            translation += word + " "
    return translation


def auto_correct():
    news = ""
    a = translate(clean(text)).split()
    for word in a:
        if len(word) > 1:
            news += word + " "
    print(news)

auto_correct()

它似乎工作正常,但是当我运行时,“作曲家”和“两个”这两个词并没有被翻译。

1 个答案:

答案 0 :(得分:5)

您忘记了单词composers和单词two之间的逗号。另外,你写了"composers:componisten"而不是"composers":"componisten"。像这样更改你的字典

 english_dutch = {"last":"laatst", "week":"week",
     "the":"de", "royal":"koninklijk",
     "festival":"feast", "hall":"hal",
     "saw":"zaag", "first":"eerst",
     "performance":"optreden",
     "of":"van", "a":"een",
     "new":"nieuw", "symphony":"symphonie",
     "by":"bij",
     "one":"een", "world":"wereld",
     "leading":"leidend", "modern":"modern",
     "composer":"componist",
     "composers":"componisten", "two":"twee",  # <- HERE
     "shed":"schuur", "sheds":"schuren"}

为什么它未被发现?检查一下:

>>> {"composers:componisten" "two":"twee"}
{'composers:componistentwo': 'twee'}

因为逗号丢失且冒号字符串中,所以python连接了字符串,创建了一个无用(但有效)的键/值对。

此行为记录为here

  

允许使用多个相邻的字符串文字(由空格分隔),可能使用不同的引用约定,并且它们的含义与它们的连接相同。因此,&#34;你好&#34; &#39;世界&#39;相当于&#34; helloworld&#34;。

相关问题