Skip to content

Instantly share code, notes, and snippets.

@jackbergus
Last active November 8, 2016 18:53
Show Gist options
  • Save jackbergus/77a0b39dbc27439b740b337fe103ea95 to your computer and use it in GitHub Desktop.
Save jackbergus/77a0b39dbc27439b740b337fe103ea95 to your computer and use it in GitHub Desktop.
A python script for learning German's die Wörter
[
{
"art": "der",
"de": "Apfel",
"errors": 0,
"first": 0,
"it": "la mela"
},
{
"art": "die",
"de": "T\u00fcr",
"errors": 0,
"first": 0,
"it": "la porta"
},
{
"art": "die",
"de": "Wurst",
"errors": 0,
"first": 0,
"it": "la salsiccia"
},
{
"art": "das",
"de": "Bild",
"errors": 0,
"first": 0,
"it": "il quadro"
},
{
"art": "der",
"de": "Boden",
"errors": 0,
"first": 0,
"it": "il pavimento"
},
{
"art": "der",
"de": "Stuhl",
"errors": 0,
"first": 0,
"it": "la sedia"
},
{
"art": "die",
"de": "Tasche",
"errors": 0,
"first": 0,
"it": "la tasca"
},
{
"art": "die",
"de": "Tasche",
"errors": 0,
"first": 0,
"it": "la borsa"
},
{
"art": "das",
"de": "Buch",
"errors": 0,
"first": 0,
"it": "il libro"
},
{
"art": "der",
"de": "Buchstabe",
"errors": 0,
"first": 0,
"it": "la lettera"
},
{
"art": "der",
"de": "Tisch",
"errors": 0,
"first": 0,
"it": "il tavolo"
},
{
"art": "das",
"de": "Fenster",
"errors": 0,
"first": 0,
"it": "la finestra"
},
{
"art": "die",
"de": "Antwort",
"errors": 0,
"first": 0,
"it": "la risposta"
},
{
"art": "der",
"de": "Wasserkocher",
"errors": 0,
"first": 0,
"it": "il bollitore"
},
{
"art": "der",
"de": "Herd",
"errors": 0,
"first": 0,
"it": "i fornelli/la cucina"
},
{
"art": "der",
"de": "Bleistift",
"errors": 0,
"first": 0,
"it": "la matita"
},
{
"art": "der",
"de": "Kuli",
"errors": 0,
"first": 0,
"it": "la penna"
},
{
"art": "der",
"de": "Drucker",
"errors": 0,
"first": 0,
"it": "la stampante"
},
{
"art": "der",
"de": "Fr\u00fchling",
"errors": 0,
"first": 0,
"it": "la primavera"
}
]
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import calendar
import random
import datetime
import time
import sys
import traceback
import copy
# Loading data
data = json.load(open('data.json'))
indices = range(len(data))
random.seed(datetime.datetime.now())
frage = []
def turn():
global frage
if (len(frage)==0):
sort()
let = copy.copy(indices)
random.shuffle(let)
frage = let[0:9]
return frage.pop()
def now():
return calendar.timegm(time.gmtime())
def comparator(x,y):
xl = (now()-data[x]["first"])
yl = (now()-data[y]["first"])
if (xl == yl):
return cmp(data[x]["errors"],data[y]["errors"])
else:
return cmp(xl,yl)
# Sorting by errors
def sort():
sorted(indices,cmp=comparator, reverse=True)
def update_with_answer(i,correct):
if not correct:
data[i]["first"] = now()
data[i]["errors"] += 1
else:
if (data[i]["errors"]>0):
data[i]["errors"] -= 1
data[i]["first"] = now()
def close_and_save():
json.dump(data, open('data.json', 'w'), sort_keys=True, indent=4)
sys.exit(0)
print("Write :q to quit")
while True:
i = turn()
current = data[i]
print(current["it"])
answer = raw_input().decode('utf-8')
if (answer == ":q"):
close_and_save()
else:
test = (answer == (current["art"]+u" "+current["de"]))
if test:
print("Ok!")
else:
print(u"Errore: la risposta corretta era \""+(current["art"]+u" "+current["de"])+u"\"")
update_with_answer(i,test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment