📜 ⬆️ ⬇️

A simple Python script to learn English words or why I don’t use flashcards

On Habré, the topic of learning the English language ( proof ) has been raised more than once. She was viewed from different angles. Someone wrote that why not learn the words when learning a foreign one. Someone shared ways, of which there are a great many, from simple cards, to a whole notebook, I don't want to write out the writing itself. In the end, there is LinguaLeo , also a great service, where you can learn 10 words a day at no extra charge. Although sometimes you do not want to pay, and 10 words seem to be a very small amount for a young organism who knows the mysteries and charms of a foreign language. Then maybe this script will be useful to you.

To hell with GUI!

Not. Not at all to hell, but in this problem. All we need is to read the word, write its translation and press the Enter key. Pretty simple mechanism of work. So why strain and rape for the sake of this tkinter?

JSON to the rescue

There is certainly an amateur. I used JSON. Someone like XML. The data structure will be fairly simple.

{ "catch, caught, caught":", , ", "fly, flew, flown":", " } 

')
We save a similar “dictionary” of two words into a file, which we will call for example english.json, and in order not to configure the locale once again we save it in Windows 866 encoding (I am sure that UTF-8 is suitable for Linux).

His Excellency Code

First of all, we import the sys module, and the random module.

 import sys import random 


Further, we unexpectedly define the classical function main. According to the plan, it will immediately require a dictionary, which will need to be specified in the parameters when starting the script.

 def main(): try: wordict = eval(open(sys.argv[1]).read()) #   except: print "You have to enter all parameters.\nExample: python wordrepeater.py yourdictionary.json" #   ? raw_input("") 


We now have a wordict dictionary. Next you need to do two modes of passage through the dictionary. The first is that the script gives us an English word, and we answer one of its translations. The second passage is just the opposite - the script is a Russian word for us, and we are English for it. Here comes the dictionary designer.

  mode = raw_input("Choose mode:\n\t1:Word To Translation;\n\t2:Translation To Word.\n>> "); if mode == "2": wordict = {wordict[k]:k for k in wordict.keys()} #          . 


After the done manipulations, a few lines remain, which will run through the entire dictionary. You can describe them inside the main function, although I preferred to describe another function.

 def keysToValues(dic): #      wrong = 0 #    keys = dic.keys() #       . while True: tmpkey = random.choice(keys) #      print "{0}: {1}".format(len(keys), tmpkey) #    .        value = dic[tmpkey] #      if raw_input("Translation: ") in value.split(", "): #   "" ,         print "True. {0}\n".format(value) keys.remove(tmpkey) # ! !    . else: wrong += 1 print "Wrong! {0}\n".format(value) # ...    . if len(keys) < 1: #   .       . raw_input("\n\nNothing\nWrong - {0}".format(wrong)) #      sys.exit() #   Enter,   


All that remains to be done is to run this function in main, and then start main.

  keysToValues(wordict) if __name__ == "__main__": main() 


With free time and this script, it turns out to learn up to 50 (I never tried again) English words in the evening. The most tedious is to fill out the dictionary. But I am sure that it is not more tiresome than making cards.

The final result.

For more convenience, all the code in one place.
 import sys import random def keysToValues(dic): wrong = 0 right = 0 keys = dic.keys() while True: tmpkey = random.choice(keys) print "{0}: {1}".format(len(keys), tmpkey) #print str(len(keys))+":", tmpkey value = dic[tmpkey] #answ = raw_input("Translation: ") if raw_input("Translation: ") in value.split(", "): print "True. {0}\n".format(value) #fjf right += 1 keys.remove(tmpkey) else: wrong += 1 print "Wrong! {0}\n".format(value) if len(keys) < 1: raw_input("\n\nNothing\nRight - {0}. Wrong - {1}".format(right, wrong)) sys.exit() def main(): try: wordict = eval(open(sys.argv[1]).read()) except: print "You have to enter all parameters.\nExample: python wordrepeater.py yourdictionary.json" raw_input("") mode = raw_input("Choose mode:\n\t1:Word To Translation;\n\t2:Translation To Word.\n>> "); if mode == "2": wordict = {wordict[k]:k for k in wordict.keys()} elif mode == "": print "Exit" sys.exit() keysToValues(wordict) if __name__ == "__main__": main() 

Source: https://habr.com/ru/post/211466/


All Articles