├── texts ├── english.txt ├── ch1_2.txt └── ch1_1.txt ├── README.md └── text_memorize.py /texts/english.txt: -------------------------------------------------------------------------------- 1 | The quick brown fox jumps over the lazy dog. 2 | -------------------------------------------------------------------------------- /texts/ch1_2.txt: -------------------------------------------------------------------------------- 1 | Джеф, ты, наверное, ещё не был у нас на юге? 2 | Нет, не был. 3 | Так давай, приезжай к нам в гости в Крым! 4 | Огромное спасибо, но я не могу. Весной ещё идут занятия. 5 | Тогда приезжай летом. Летом бывает такая чудесная погода: солнце, тёплое море ... 6 | И можно загорать и купаться? 7 | Конечно, можно. Когда у тебя кончаются занятия? 8 | В мае. И в июне я совсем свободен. 9 | Отлично. Тогда давай договоримся на июнь. 10 | Прекрасно. 11 | -------------------------------------------------------------------------------- /texts/ch1_1.txt: -------------------------------------------------------------------------------- 1 | Ванесса, сегодня такая хорошая погода. Мы собираемся поехать на дачу. Не хочешь поехать с нами? 2 | Сегодня не холодно для дачи? Ведь на улице всего 12 градусов. 3 | Это сейчас 12 градусов. А днëм будет тепло. 4 | А дача далеко? 5 | Меньше часа на электричке. Подумай, можно гулять в лесу, собирать грибы ... А недалеко озеро ... 6 | А если пойдëт дождь? 7 | Если пойдëт дождь, то вернëмся пораньше. 8 | Гм. А где мы встретимся? 9 | Давай встретимся через час на Финляндском вокзале. 10 | Хорошо. Договорились. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | text-memorize 2 | ------------- 3 | [![asciicast](https://asciinema.org/a/984avh6xfeaqxe8earjpg6xnn.png)](https://asciinema.org/a/984avh6xfeaqxe8earjpg6xnn) 4 | 5 | A tool to help memorize some text! 6 | 7 | When provided with a file, this program will remove random words from each line of text and ask you to provide the words that were removed. 8 | 9 | You can provide lower and upper bounds to the number of words removed per line by using the `--l` and/or `--u` flags, or remove an exact number with `--n`. You can also specify the number of attempts you want to allow with the `--a` flag. To turn off colors, just send in the `--no-color` flag. 10 | 11 | text-memorize uses Python 2.7. 12 | 13 | ### How to run 14 | 15 | `python text_memorize.py filename` 16 | 17 | For between *i* and *j* words removed per line: 18 | 19 | `python text_memorize.py --l i --u j filename` 20 | 21 | For exactly *k* words removed per line: 22 | 23 | `python text_memorize.py --n k filename` 24 | 25 | For infinite attempts: 26 | 27 | `python text_memorize.py --a 0 filename` 28 | 29 | Without color: 30 | 31 | `python text_memorize.py --no-color filename` 32 | 33 | 34 | ### Contribute 35 | 36 | Contributions are welcome! Just open a pull request and I'll attend to it as soon as possible. 37 | -------------------------------------------------------------------------------- /text_memorize.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding=utf-8 3 | 4 | import argparse 5 | import codecs 6 | import random 7 | import sys 8 | 9 | parser = argparse.ArgumentParser(description="""A tool to help memorize some 10 | text! When provided with a file, this program will remove random words from 11 | each line of text and ask you to provide the words that were removed.""") 12 | 13 | parser.add_argument('--no-color', action='store_true', 14 | help='hide colorful underlining') 15 | 16 | parser.add_argument('--a', dest='tries', type=int, default=3, 17 | help=('number of tries to allow per word ' 18 | '(0 for unlimited tries, default: 3)')) 19 | 20 | parser.add_argument('--n', dest='num', type=int, default=0, 21 | help=('number of words to remove from each line ' 22 | '(0 for random number of removals, default: 0)')) 23 | 24 | parser.add_argument('--l', dest='lower', type=int, default=1, 25 | help=('lower bound on number of words to remove ' 26 | '(inclusive, default: 1)')) 27 | 28 | parser.add_argument('--u', dest='upper', type=int, default=0, 29 | help=('upper bound on number of words to remove ' 30 | '(inclusive, 0 for no upper bound, default: 0)')) 31 | 32 | parser.add_argument('filename', metavar='filename', type=str, 33 | help='the text file') 34 | 35 | args = parser.parse_args() 36 | 37 | try: 38 | with codecs.open(args.filename, 'r', 'utf-8') as f: 39 | current_line = '' 40 | 41 | for line in f: 42 | missing_words = [] 43 | new_word = u'' 44 | split_line = line.split(' ') 45 | 46 | if args.num: 47 | num = args.num 48 | else: 49 | if args.upper: 50 | upper = args.upper + 1 51 | else: 52 | upper = len(split_line) + 1 53 | 54 | num = random.randrange(start=args.lower, stop=upper) 55 | 56 | words = [False] * num 57 | diff = len(split_line) - num 58 | if diff > 0: 59 | words.extend([True] * diff) 60 | random.shuffle(words) 61 | 62 | for i, word in enumerate(split_line): 63 | show_word = words[i] 64 | 65 | color_started = False 66 | 67 | for char in word: 68 | if (char.isalpha() and show_word) or not char.isalpha(): 69 | if color_started: 70 | current_line += '\033[0m' 71 | color_started = False 72 | 73 | current_line += char 74 | else: 75 | new_word += char 76 | 77 | if not color_started and not args.no_color: 78 | current_line += '\033[91m' 79 | color_started = True 80 | 81 | current_line += '_' 82 | if word[-1] != '\n': 83 | if color_started: 84 | current_line += '\033[0m' 85 | color_started = False 86 | current_line += ' ' 87 | 88 | if len(new_word): 89 | missing_words.append(new_word) 90 | new_word = u'' 91 | tries = 1 92 | 93 | while len(missing_words): 94 | 95 | next_word = missing_words[0] 96 | print(current_line) 97 | 98 | try: 99 | guess = raw_input('Enter the next missing word: ') 100 | except KeyboardInterrupt: 101 | sys.exit(0) 102 | except EOFError: 103 | sys.exit(0) 104 | 105 | if unicode(guess,'utf-8') == next_word: 106 | print("Correct! The word was '" + next_word + "'.") 107 | elif args.tries and tries == args.tries: 108 | print("Too many tries. The word was '" + next_word + "'.") 109 | else: 110 | print("Incorrect. Please try again.") 111 | tries += 1 112 | continue 113 | 114 | if args.no_color: 115 | current_line = current_line.replace('_' * len(next_word),next_word,1) 116 | else: 117 | current_line = current_line.replace('\033[91m' + '_' * len(next_word) + '\033[0m',next_word,1) 118 | 119 | missing_words.pop(0) 120 | tries = 1 121 | except IOError: 122 | print("File not found: '{}'".format(args.filename)) 123 | --------------------------------------------------------------------------------