【问题标题】:How should I go about making my verb conjugation study tool in Python?我应该如何在 Python 中制作我的动词变位学习工具?
【发布时间】:2017-04-22 23:44:26
【问题描述】:

感谢您花时间看我的帖子,我现在是西班牙语班的学生,我想制作一个学习工具来练习西班牙语动词变位。

在与代码学院顾问交谈后,他建议使用嵌套字典,您可以在下面的代码中看到。

  • 1 级键应该是包含的所有动词的不定式。
  • 2 级键应该是动词的时态。
  • 3 级键应该是人称代词,3 级值应该是我们要学习的以人称和数字标记的对应动词。

我不熟悉这些词典,需要帮助!代码学院顾问在轮班结束前简要介绍了随机化。以下是我在游戏中想要的 3 个主要内容:

  1. 让程序随机选择动词、时态和冠词。

  2. 让程序要求用户输入并提出问题,例如“(动词)的(时态)(冠词)形式是什么?”

  3. 让程序响应,说“正确!”或“错了!”

一些例子:

  • 示例 1:

    • 程序问题:hacer的现在yo形式是什么?

    • 用户输入:Hago

    • 程序响应“正确!”

  • 示例 2:

    • 程序问题:hacer的现在tu形式是什么?

    • 用户输入:Haces

    • 程序响应“正确!”

  • 示例 3:

    • 程序问题:hacer 的旧约形式是什么?

    • 用户输入:Hece

    • 程序响应“不正确!”

这是我现在的字典

import random  
verbs = {
'hacer': {
'present':{
    'yo': 'hago',
    'tu': 'haces',
    'elellausted': 'hace',
    'nosotros': 'hacemos',
    'ellosellasuds': 'hacen'
}
, 'preterite':{
    'yo': 'hice',
    'tu': 'hiciste',
    'elellausted': 'hizo',
    'nosotros': 'hicimos',
    'ellosellasuds': 'hicieron'
}
}
'tener': {
'present':{
    'yo': 'tengo',
    'tu': 'tienes',
    'elellaud':'tiene',
    'nosotros':'tenemos' ,
    'ellosellasuds':'tienen'
}
, 'preterite':{
    'yo': 'tuve',
    'tu': 'tuviste',
    'elellausted': 'tuvo',
    'nosotros': 'tuvimos',
    'ellosellasuds': 'tuvieron'
}
}
}

感谢任何可以帮助我的人!我刚开始编程,但我已经做了大约 6 个月的网站设计。我愿意学习,任何帮助将不胜感激。

如果您想建立某种形式的 Skype 通话或就此进行聊天,我们将不胜感激,我将非常愿意!

再次感谢您的阅读。

【问题讨论】:

  • 我建议您阅读有关在 python 中使用字典的更多信息。一旦你理解了简单的字典,嵌套的字典就会更有意义。

标签: python dictionary random


【解决方案1】:

我有一些时间,所以这里有一些代码可以帮助你开始。它首先创建可能的冠词、动词和时态的列表(这些可以使用 for 循环找到,或者由您手动输入)。然后它使用random 模块从这些列表中选择一个随机条目。然后我们询问用户的答案,如果他们做对了就给他们一个新问题,否则允许他们再试一次。 如果有什么不明白的,请告诉我。

import random  
verbs = {
'hacer': {
'present':{
    'yo': 'hago',
    'tu': 'haces',
    'elellausted': 'hace',
    'nosotros': 'hacemos',
    'ellosellasuds': 'hacen'
}
, 'preterite':{
    'yo': 'hice',
    'tu': 'hiciste',
    'elellausted': 'hizo',
    'nosotros': 'hicimos',
    'ellosellasuds': 'hicieron'
}
}, # ADDED A MISSING COMMA HERE
'tener': {
'present':{
    'yo': 'tengo',
    'tu': 'tienes',
    'elellaud':'tiene',
    'nosotros':'tenemos' ,
    'ellosellasuds':'tienen'
}
, 'preterite':{
    'yo': 'tuve',
    'tu': 'tuviste',
    'elellausted': 'tuvo',
    'nosotros': 'tuvimos',
    'ellosellasuds': 'tuvieron'
    }
    }
    }

article_list = ["yo", "tu", "elellausted", "nosotros", "ellosellasuds"]
verb_list = list(verbs.keys())

tense_list = []
for key in verbs:
  for tense in verbs[key]:
    if tense not in tense_list:
      tense_list.append(tense)
# or you could just manually type a list of tenses, probably more efficient. 

while True:
  article_choice = random.choice(article_list)
  verb_choice = random.choice(verb_list)
  tense_choice = random.choice(tense_list)

  question = "What is the {} {} form of {}?\n> ".format(tense_choice, article_choice, verb_choice)

  while True:
    response = input("{}".format(question)) #in python2: raw_input(..)

    if verbs[verb_choice][tense_choice][article_choice] == response.lower().strip():
      print("Correct!")
      break
    else:
      print("Incorrect, try again.")

【讨论】:

    【解决方案2】:

    TL;DR

    我在 Windmill 的代码中添加了一些功能 - 保存的动词分数计数器有助于优先考虑低分词缀,一些跳过或退出的选项,我将 dict 作为 json 保存在一个文件中,我还添加了一些建议网页抓取动词。

    学习工具

    • 目标:通过测试用户经常失败的动词来简化动词的学习。

    用例

    请注意,我在搜索相同的一般用例时遇到了这篇文章。

    即“我想学习X语言的动词,并通过一个小程序进行改进。”

    具体来说,我在学习葡萄牙语时还需要一个动词变位学习工具。葡萄牙语在某些方面与西班牙语相似,其中之一就是动词变位。

    我还考虑了您想要一个动词变位学习工具这一事实,我认为这是问题通常所暗示的 - 该工具的使用不仅限于西班牙语。从问题中我了解到学习工具应该可以帮助您学习-例如在你学习的过程中适应你,并优先考虑你一直犯错的单词,它应该很容易理解它是如何工作的。

    针对弱点的设计

    基本上,我们希望跟踪我们的分数以针对我们的弱点,否则程序将回到我们已经熟悉的动词*。我认为这在这样的学习工具中很重要,否则使用起来会浪费时间。

    * 添加有关动词是规则还是不规则的信息可能是一个想法。这样我们就可以在某种时态的规则形式被很好地理解之后,优先考虑不规则动词。这里的“好”可以由工具的用户启发式地或经验地决定,作为一个研究问题。两者都取决于用户的需求和工具存储的信息。

    获取数据

    首先我们需要动词表数据。

    您可以选择手动输入数据,这需要大量的乏味和耐心,或者自动输入,如果您不了解网络抓取,则需要学习的意愿。我建议您在 python 中查找 webscraping,这样您就可以学习使用 requests 和 lxml 或 beautifulsoup 等库构建获取动词所需的基本命令。取决于您决定抓取哪个网站,因为有许多在线词典可用于抓取有关动词变形的信息。

    数据格式选择

    保存抓取的数据时,您有一些选择。

    json

    如果您想使用字典,我建议将其保存为 json 文件并使用 python json 库加载。这样您就可以将文件与代码分开。

    所以你的动词在一个名为spanish_verbs.json的文件中看起来像这样:

    {
      "hacer": {
        "present": {
          "yo": "hago",
          "tu": "haces",
          "elellausted": "hace",
          "nosotros": "hacemos",
          "ellosellasuds": "hacen"
        },
        "preterite": {
          "yo": "hice",
          "tu": "hiciste",
          "elellausted": "hizo",
          "nosotros": "hicimos",
          "ellosellasuds": "hicieron"
        }
      },
      "tener": {
        "present": {
          "yo": "tengo",
          "tu": "tienes",
          "elellaud": "tiene",
          "nosotros": "tenemos",
          "ellosellasuds": "tienen"
        },
        "preterite": {
          "yo": "tuve",
          "tu": "tuviste",
          "elellausted": "tuvo",
          "nosotros": "tuvimos",
          "ellosellasuds": "tuvieron"
        }
      }
    }
    

    所以现在你可以用一些简单的东西来加载它们,而不是在程序中包含动词:

    with open("spanish_verbs.json", 'r') as f:
          verbs = json.load(f)
    
    CSV

    否则我会将数据保存为 csv 或 tsv 并使用 pandas 之类的东西来加载文件。

    程序动作和界面

    当前界面是基于终端的。如果你想更进一步,你可以使用 tkinter 或 wxpython 之类的东西来构建 GUI。

    启动程序

    从终端启动程序:

    python3 ./ask_verb_tense_example.py spanish_verbs.json verb_scores.tsv
    
    

    动作

    您可以使用的操作是

    1. 键入动词并按回车键检查是否正确
    • 通过运行此命令,您可以正确也可以不正确

    1. 选择一个选项并按回车键

    关于选项,您可以选择

    • q - 保存进度并退出

    • p - 传递并显示用户不记得的动词

    • 这些选项假定不应该有任何称为 q 或 p 的动词。

    记录分数

    程序中还有一个动词评分数据框,在您完成所有动词一次后,它会更新并跟踪和训练您使用一组评分最差的动词中的一个随机动词。

    计分规则

    评分规则如下:

    • 输入正确答案将使动词的正确累积分数加一

    • 输入错误的答案会使动词的错误累积分数加一

    • 及格会在不正确的动词累积分数上加一

    • 上述任何操作都会将总变量加一,并对其进行跟踪以检查不正确/正确答案的百分比

    代码

    代码如下:

    import random
    import readline
    import sys
    import os
    import json
    import pandas as pd
    
    with open(sys.argv[1], 'r') as f:
       verbs = json.load(f)
    
    def load_verb_scores(filename):
       if not os.path.exists(filename):
           verb_scores = pd.DataFrame(
               {
                   "verb": pd.Series([], dtype='str'),
                   "correct": pd.Series([], dtype='int'),
                   "incorrect": pd.Series([], dtype='int'),
                   "total": pd.Series([], dtype='int'),
               },
           )
       else:
           verb_scores = pd.read_csv(filename, sep='\t')
       return verb_scores
    
    def update_scores(verb_scores, verb, correct=False, incorrect=False):
       total=0
    
       if correct and incorrect:
           raise Exception("Answer to question cannot be both correct and incorrect!")
    
       if (verb_scores.verb==verb).any():
    
           if correct:
               verb_scores.loc[(verb_scores.verb==verb),"correct"]+=1
    
           elif incorrect:
               verb_scores.loc[(verb_scores.verb==verb),"incorrect"]+=1
    
           verb_scores.loc[(verb_scores.verb==verb),"total"]+=1
    
       else:
           verb_scores=verb_scores.append(
               {
                   "verb":verb,
                   "correct":int(correct),
                   "incorrect":int(incorrect),
                   "total":1,
               },
               ignore_index=True
           )
    
    
       return verb_scores
    
    verb_scores = load_verb_scores(sys.argv[2])
    total_to_practice = sum([len(y) for v in verbs.values() for y in v.values()])
    print(f"found {total_to_practice} verbs to practice")
    
    while True:
    
       if verb_scores.shape[0]==total_to_practice:
           # random choice out of poorest performing verbs
           poorest_performance = (verb_scores.incorrect/verb_scores.total).nlargest(5).index
           verb = random.choice(verb_scores.loc[poorest_performance].verb.tolist())
    
           v_index=verb_scores.verb==verb
    
           # warning: some verbs may correspond to multiple infinitives/tenses/articles !
           infinitive = [i for i, v in verbs.items() for y in v.values() if verb in y.values()][0]
           tense = [t for v in verbs.values() for t, y in v.items() if verb in y.values()][0]
           article = [a for a,v in verbs.get(infinitive).get(tense).items() if verb==v][0]
    
           incorrect_pct = (verb_scores[v_index].incorrect*100/verb_scores[v_index].total).item()
           print("selected verb: ", infinitive, " wrong ", incorrect_pct, "% of the time")
    
       else:
           # random choice over all verbs to begin with
           infinitive = random.choice(list(verbs.keys()))
    
           tense2articles2verbs = verbs.get(infinitive)
           tense = random.choice(list(tense2articles2verbs.keys())) # choose a tense
           articles2verbs = tense2articles2verbs.get(tense)
           article = random.choice(list(articles2verbs.keys())) # choose an article
           verb = articles2verbs.get(article)
    
       question = "What is the {} {} form of {}?\n> ".format(
           tense,
           article,
           infinitive,
       )
    
       while True:
           response = input("{}".format(question))
    
           if verb == response.lower().strip():
               print("\033[92mCorrect!\033[0m")
               verb_scores = update_scores(
                   verb_scores,
                   verb,
                   correct=True
               )
               break
           elif 'p' == response.lower():
               print("\033[91mPassing... the correct verb was {}\033[0m".format(verb))
               verb_scores = update_scores(
                   verb_scores,
                   verb,
                   incorrect=True
               )
               break
           elif 'q' == response.lower():
               print("Safe quit.")
               verb_scores.to_csv(sys.argv[2], sep='\t', index=False)
               sys.exit(0)
           else:
               verb_scores = update_scores(
                   verb_scores,
                   verb,
                   incorrect=True
               )
    
               print("\033[91mIncorrect, try again.\033[0m")
    

    示例输出

    程序输出如下所示:

    What is the present nosotros form of hacer?
    > hacemos
    Correct!
    

    在你遍历所有可能的动词后,它会开始遍历你最容易出错的前 5 个动词。

    $ python3 ./ask_verb_tense_example.py spanish_verbs.json verb_scores.tsv
    found 20 verbs to practice
    selected verb:  tener  wrong  100.0 % of the time
    What is the preterite tu form of tener?
    > tenes
    Incorrect, try again.
    ...
    

    分数文件

    您还可以访问名为verb_scores.tsv 的动词分数文件,该文件在用户按q 保存并退出后由程序创建和更新。通过这种方式,您可以通过将 tsv 导入某些电子表格软件来跟踪您的进度。

    verb     correct   incorrect  total
    hacemos  0         3          3
    tiene    1         1          2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-25
      • 2016-02-13
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      相关资源
      最近更新 更多