2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
27 #include <sys/types.h>
36 #include <readline/readline.h>
37 #include <readline/history.h>
39 #define ASSERT_NOT_REACHED \
41 static const int NOT_REACHED = 0; \
42 assert (NOT_REACHED); \
45 #define unused(foo) foo __attribute__((unused))
49 typedef struct _item {
63 CATEGORY_ORDER_RANDOM,
64 CATEGORY_ORDER_SEQUENTIAL
72 CHALLENGE_TYPE_TEXT_TO_SPEECH
75 typedef struct _category {
81 /* Support sequential introduction of items from bin 0 */
82 category_order_t order;
83 /* Support categories where responses are timed (0.0 == disable). */
86 /* Support challenges of non-text types (image, audio, etc.) */
87 challenge_type_t challenge_type;
88 /* Whether to repeat afterwards (for a little extra reinforcement) */
92 typedef struct _mnemon {
97 category_t *categories;
110 xmalloc (size_t size)
116 fprintf (stderr, "Error: out of memory\n");
124 xrealloc (void *ptr, size_t size)
128 ret = realloc (ptr, size);
130 fprintf (stderr, "Error: out of memory\n");
138 xstrdup (const char *s)
144 fprintf (stderr, "Error: out of memory\n");
152 xstrndup (const char *s, size_t n)
156 ret = strndup (s, n);
158 fprintf (stderr, "Error: out of memory\n");
166 xasprintf (char **strp, const char *fmt, ...)
172 ret = vasprintf (strp, fmt, ap);
176 fprintf (stderr, "Error: out of memory\n");
182 item_init (item_t *item,
184 const char *challenge,
185 const char *response)
189 item->challenge = xmalloc (strlen (challenge) + 1 +
190 strlen (response) + 1);
191 item->response = item->challenge + strlen (challenge) + 1;
193 strcpy (item->challenge, challenge);
194 strcpy (item->response, response);
198 item_fini (item_t *item)
200 /* item->response shares allocation with item->challenge, so
201 * doesn't require a separate call to free */
202 free (item->challenge);
206 category_init (category_t *category,
209 category->name = xstrdup (name);
211 category->items_size = 0;
212 category->num_items = 0;
213 category->items = NULL;
214 category->order = CATEGORY_ORDER_RANDOM;
215 category->time_limit = 0.0;
216 category->bin_zero_head = 0;
217 category->challenge_type = CHALLENGE_TYPE_TEXT;
218 category->repeat = 0;
222 category_fini (category_t *category)
226 for (i = 0; i < category->num_items; i++)
227 item_fini (&category->items[i]);
229 free (category->items);
231 free (category->name);
235 category_grow (category_t *category)
237 if (category->items_size)
238 category->items_size *= 2;
240 category->items_size = 1;
242 category->items = xrealloc (category->items,
243 category->items_size * sizeof (item_t));
247 category_add_item (category_t *category,
249 const char *challenge,
250 const char *response)
254 if (category->num_items == category->items_size)
255 category_grow (category);
257 item = &category->items[category->num_items++];
259 item_init (item, score, challenge, response);
265 category_next_bin_zero_item (category_t *category)
267 int *i = &category->bin_zero_head;
269 for ( ; *i < category->num_items; *i = *i + 1)
270 if (category->items[*i].score == 0)
271 return &category->items[*i];
277 category_print (category_t *category,
283 fprintf (file, "order = %s\n\n",
284 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
285 fprintf (file, "time = %f\n\n",
286 category->time_limit);
288 fprintf (file, "challenge = ");
289 switch (category->challenge_type) {
290 case CHALLENGE_TYPE_TEXT:
291 fprintf (file, "text");
293 case CHALLENGE_TYPE_IMAGE:
294 fprintf (file, "image");
296 case CHALLENGE_TYPE_AUDIO:
297 fprintf (file, "audio");
299 case CHALLENGE_TYPE_MIDI:
300 fprintf (file, "midi");
302 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
303 fprintf (file, "text-to-speech");
306 fprintf (file, "\n\n");
308 fprintf (file, "repeat = %d\n\n", category->repeat);
310 for (i = 0; i < category->num_items; i++) {
311 item = &category->items[i];
313 fprintf (file, "\n");
314 fprintf (file, "%d\n%s\n%s\n",
322 bin_init (bin_t *bin,
333 bin_fini (bin_t *bin)
339 bin_grow (bin_t *bin)
342 bin->items_size *= 2;
346 bin->items = xrealloc (bin->items,
347 bin->items_size * sizeof (item_t*));
351 bin_add_item (bin_t *bin,
354 assert (item->score == bin->score);
356 if (bin->num_items == bin->items_size)
359 bin->items[bin->num_items++] = item;
363 bin_remove_item (bin_t *bin,
366 /* Replace the current item with the last item, (no need to shift
367 * any more than that since we don't care about the order of the
368 * items within a bin). */
371 bin->items[item_index] = bin->items[bin->num_items];
374 /* Find the index for an item within a bin.
376 * XXX: This is currently a linear search, so is a potential
377 * performance problem.
380 bin_item_index (bin_t *bin,
385 for (i = 0; i < bin->num_items; i++)
386 if (bin->items[i] == item)
392 typedef int (item_match_predicate_t) (void *closure, item_t *item);
394 /* Return the number of items in the bin from the given category (or
395 * from all categories if category == NULL) */
397 bin_num_items_matching (bin_t *bin,
398 item_match_predicate_t *predicate,
401 int i, num_items = 0;
403 if (predicate == NULL)
404 return bin->num_items;
406 for (i = 0; i < bin->num_items; i++)
407 if ((predicate) (closure, bin->items[i]))
414 mnemon_init (mnemon_t *mnemon)
418 home = getenv ("HOME");
422 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
424 mnemon->categories_size = 0;
425 mnemon->num_categories = 0;
426 mnemon->categories = NULL;
428 mnemon->bins_size = 0;
429 mnemon->num_bins = 0;
432 mnemon->to_introduce = 10;
433 mnemon->to_master = 10;
434 mnemon->unlearned = 0;
435 mnemon->mastered = -1;
439 mnemon_fini (mnemon_t *mnemon)
443 for (i = 0; i < mnemon->num_bins; i++)
444 bin_fini (&mnemon->bins[i]);
447 for (i = 0; i < mnemon->num_categories; i++)
448 category_fini (&mnemon->categories[i]);
449 free (mnemon->categories);
451 free (mnemon->dir_name);
455 mnemon_categories_grow (mnemon_t *mnemon)
457 if (mnemon->categories_size)
458 mnemon->categories_size *= 2;
460 mnemon->categories_size = 1;
462 mnemon->categories = xrealloc (mnemon->categories,
463 mnemon->categories_size * sizeof (category_t));
466 /* Get a category by name if it exists */
468 mnemon_get_category_if_exists (mnemon_t *mnemon,
473 for (i = 0; i < mnemon->num_categories; i++)
474 if (strcmp (mnemon->categories[i].name, name) == 0)
475 return &mnemon->categories[i];
480 /* Get a category by name, creating new one if necessary. */
482 mnemon_get_category (mnemon_t *mnemon,
485 category_t *category;
487 category = mnemon_get_category_if_exists (mnemon, name);
491 mnemon_categories_grow (mnemon);
493 category = &mnemon->categories[mnemon->num_categories++];
495 category_init (category, name);
501 mnemon_bins_grow (mnemon_t *mnemon)
503 if (mnemon->bins_size)
504 mnemon->bins_size *= 2;
506 mnemon->bins_size = 1;
508 mnemon->bins = xrealloc (mnemon->bins,
509 mnemon->bins_size * sizeof (bin_t));
513 mnemon_get_bin (mnemon_t *mnemon,
519 for (i = 0; i < mnemon->num_bins; i++)
520 if (mnemon->bins[i].score == score)
521 return &mnemon->bins[i];
522 else if (mnemon->bins[i].score > score)
525 if (mnemon->num_bins == mnemon->bins_size)
526 mnemon_bins_grow (mnemon);
528 bin = &mnemon->bins[i];
530 /* Make room to insert new bin at its sorted location. */
531 if (i < mnemon->num_bins)
532 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
535 bin_init (bin, score);
541 mnemon_remove_bin (mnemon_t *mnemon,
544 int i = bin - mnemon->bins;
548 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
555 int len = strlen (s);
558 if (s[len - 1] == '\n')
563 trim_space (char *string)
568 while (*s && isspace (*s))
573 s = string + strlen (string) - 1;
574 while (s > string && isspace (*s)) {
583 mnemon_load_category (mnemon_t *mnemon,
587 char *line = NULL, *end;
588 size_t line_size = 0;
592 category_t *category;
596 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
597 sprintf (path, "%s/%s", mnemon->dir_name, name);
599 file = fopen (path, "r");
601 fprintf (stderr, "Error: Failed to open %s: %s\n",
602 path, strerror (errno));
606 fstat (fileno(file), &st);
607 if (! S_ISREG(st.st_mode)) {
608 fprintf (stderr, "Error: File %s is not a regular file.\n", path);
612 category = mnemon_get_category (mnemon, name);
614 #define READ_LINE do { \
615 bytes_read = getline (&line, &line_size, file); \
616 if (bytes_read == -1) \
624 char *name, *equal, *value;
626 /* Ignore blank lines */
631 /* An initial digit means we hit an item. Trigger the
632 * spaghetti machine. */
633 if ((*line >= '0' && *line <= '9') || *line == '-')
636 equal = strchr (line, '=');
638 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
639 line, path, line_count);
647 name = trim_space (name);
648 value = trim_space (value);
650 if (strcmp (name, "order") == 0) {
651 if (strcmp (value, "sequential") == 0) {
652 category->order = CATEGORY_ORDER_SEQUENTIAL;
653 } else if (strcmp (value, "random") == 0) {
654 category->order = CATEGORY_ORDER_RANDOM;
656 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
657 value, path, line_count);
660 } else if (strcmp (name, "time") == 0) {
663 limit = strtod (value, &end);
664 while (isspace (*end))
667 category->time_limit = limit;
669 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
670 value, path, line_count);
673 } else if (strcmp (name, "challenge") == 0) {
674 if (strcmp (value, "text") == 0) {
675 category->challenge_type = CHALLENGE_TYPE_TEXT;
676 } else if (strcmp (value, "image") == 0) {
677 category->challenge_type = CHALLENGE_TYPE_IMAGE;
678 } else if (strcmp (value, "audio") == 0) {
679 category->challenge_type = CHALLENGE_TYPE_AUDIO;
680 } else if (strcmp (value, "midi") == 0) {
681 category->challenge_type = CHALLENGE_TYPE_MIDI;
682 } else if (strcmp (value, "text-to-speech") == 0) {
683 category->challenge_type = CHALLENGE_TYPE_TEXT_TO_SPEECH;
685 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
686 value, path, line_count);
689 } else if (strcmp (name, "repeat") == 0) {
690 if (strcmp (value, "0") == 0)
691 category->repeat = 0;
693 category->repeat = 1;
695 fprintf (stderr, "Unknown option %s at %s:%d\n",
696 name, path, line_count);
704 char *challenge, *response;
706 /* Ignore blank lines */
711 /* Read bin number */
713 score = strtol (line, &end, 10);
715 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
716 line, path, line_count);
722 challenge = strdup (line);
728 category_add_item (category, score, challenge, response);
738 /* Resize category items to fit exactly. */
739 category->items_size = category->num_items;
740 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
742 /* Now that the category is completely loaded, with stable
743 * pointers to every item, we can add each item to its appropriate
745 for (i = 0; i < category->num_items; i++) {
746 item_t *item = &category->items[i];
747 bin_t *bin = mnemon_get_bin (mnemon, item->score);
749 bin_add_item (bin, item);
754 mnemon_load (mnemon_t *mnemon)
757 struct dirent *dirent;
759 dir = opendir (mnemon->dir_name);
761 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
762 mnemon->dir_name, strerror (errno));
767 dirent = readdir (dir);
771 if (dirent->d_type == DT_REG) {
772 /* Ignore files matching *~, (yes, this shouldn't be
773 * hard-coded in such an ad-hoc way, but there you go. */
774 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
775 mnemon_load_category (mnemon, dirent->d_name);
783 mnemon_save (mnemon_t *mnemon)
786 char *filename, *lock_filename;
788 category_t *category;
790 for (i = 0; i < mnemon->num_categories; i++) {
791 category = &mnemon->categories[i];
793 xasprintf (&filename, "%s/%s",
794 mnemon->dir_name, category->name);
795 xasprintf (&lock_filename, "%s/.#%s",
796 mnemon->dir_name, category->name);
798 file = fopen (lock_filename, "w");
800 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
801 lock_filename, strerror (errno));
805 category_print (category, file);
807 fsync (fileno (file));
810 err = rename (lock_filename, filename);
812 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
813 lock_filename, filename, strerror (errno));
818 free (lock_filename);
822 /* Return a uniformly-distributed pseudo-random integer within the
825 * 0 <= result < num_values
828 rand_within (int num_values)
830 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
833 /* Return an exponentially-distributed pseudo-random integer within
836 * 0 <= result < num_values
838 * The distribution is such that each successively larger value will
839 * occur with a probability of half of the previous value.
842 rand_within_exponential (int num_values)
845 static uint32_t mask = 0;
849 /* Optimize the constant case. */
859 while (mask > RAND_MAX)
866 if (ones == num_values)
874 /* Find the category to which an item belongs. */
876 mnemon_item_category (mnemon_t *mnemon,
879 category_t *category;
882 for (i = 0; i < mnemon->num_categories; i++) {
883 category = &mnemon->categories[i];
884 item_index = item - category->items;
885 if (item_index >= 0 && item_index < category->num_items)
892 typedef struct _item_in_category_closure
895 category_t *category;
896 } item_in_category_closure_t;
899 mnemon_item_in_category (void *closure, item_t *item)
901 item_in_category_closure_t *iicc = closure;
902 mnemon_t *mnemon = iicc->mnemon;
903 category_t *category = iicc->category;
905 return (mnemon_item_category (mnemon, item) == category);
908 typedef struct _item_in_category_of_length_closure
911 category_t *category;
913 } item_in_category_of_length_closure_t;
916 mnemon_item_in_category_of_length (void *closure, item_t *item)
918 item_in_category_of_length_closure_t *iicolc = closure;
919 mnemon_t *mnemon = iicolc->mnemon;
920 category_t *category = iicolc->category;
921 unsigned int length = iicolc->length;
923 if (mnemon_item_category (mnemon, item) != category)
926 return strlen (item->challenge) == length;
930 mnemon_select_item (mnemon_t *mnemon,
933 category_t **category_ret)
935 int bin_index, item_index;
938 category_t *category;
940 bin_index = rand_within_exponential (mnemon->num_bins);
941 bin = &mnemon->bins[bin_index];
943 /* The most intuitive understanding of the to_introduce counter is
944 * that it's tracking never-before-learned items as they are
945 * pulled from the bin with score 0. But that bin can become
946 * empty. So the refined rule is that we decrement to_introduce
947 * whenever we pull from the lowest-indexed bin with a
948 * non-negative score. */
949 if (mnemon->to_introduce && bin->score >=0 &&
950 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
952 mnemon->to_introduce--;
955 item_index = rand_within (bin->num_items);
957 item = bin->items[item_index];
958 category = mnemon_item_category (mnemon, item);
960 if (bin->score == 0) {
961 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
962 item = category_next_bin_zero_item (category);
964 item_index = bin_item_index (bin, item);
969 *item_index_ret = item_index;
970 *category_ret = category;
974 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
975 #define HISTOGRAM_BAR_WIDTH 63
978 print_histogram_bar (double size,
981 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
982 static char const *boxes[8] = {
987 while (size > units_per_cell) {
989 size -= units_per_cell;
992 size /= units_per_cell;
996 else if (size > 6.5/8.0)
998 else if (size > 5.5/8.0)
1000 else if (size > 4.5/8.0)
1002 else if (size > 3.5/8.0)
1004 else if (size > 2.5/8.0)
1006 else if (size > 1.5/8.0)
1008 else if (size > 0.5/8.0)
1015 mnemon_print_histogram (mnemon_t *mnemon,
1016 const char *category_name,
1019 int i, last_score, max;
1020 category_t *category = NULL;
1023 item_match_predicate_t *predicate = NULL;
1024 void *closure = NULL;
1025 item_in_category_closure_t item_in_category;
1026 item_in_category_of_length_closure_t item_in_category_of_length;
1028 if (mnemon->num_bins == 0)
1031 if (category_name) {
1032 category = mnemon_get_category_if_exists (mnemon, category_name);
1035 predicate = mnemon_item_in_category_of_length;
1036 item_in_category_of_length.mnemon = mnemon;
1037 item_in_category_of_length.category = category;
1038 item_in_category_of_length.length = length;
1039 closure = &item_in_category_of_length;
1041 predicate = mnemon_item_in_category;
1042 item_in_category.mnemon = mnemon;
1043 item_in_category.category = category;
1044 closure = &item_in_category;
1049 for (i = 0; i < mnemon->num_bins; i++) {
1050 num_items = bin_num_items_matching (&mnemon->bins[i],
1051 predicate, closure);
1052 if (i == 0 || num_items > max)
1056 for (i = 0; i < mnemon->num_bins; i++) {
1057 bin = &mnemon->bins[i];
1059 while (bin->score - last_score > 1)
1060 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1061 num_items = bin_num_items_matching (bin,
1062 predicate, closure);
1063 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1064 print_histogram_bar (num_items, max);
1065 last_score = bin->score;
1070 mnemon_handle_command (mnemon_t *mnemon,
1071 const char *command)
1075 switch (command[0]) {
1076 /* 'h' for histogram */
1079 char *category = NULL;
1083 arg += strspn (arg, " \t");
1084 len = strcspn (arg, " \t");
1086 category = xstrndup (arg, len);
1088 arg += strspn (arg, " \t");
1090 length = atoi (arg);
1092 mnemon_print_histogram (mnemon, category, length);
1095 /* 'r' for repeat */
1098 /* Nothing necessary for repeating. */
1102 printf ("Unknown command: %s\n", command);
1108 mnemon_handle_response (mnemon_t *mnemon,
1112 const char *response,
1113 double response_time,
1118 correct = (strcmp (response, item->response) == 0);
1120 bin_remove_item (bin, item_index);
1122 /* If the bin is now empty, we must remove it. Also if we just
1123 * picked the last word we'll ever pick from the bin with
1124 * score 0, then we can remove that as well. */
1125 if (bin->num_items == 0 ||
1126 (bin->score == 0 && mnemon->to_introduce == 0))
1128 mnemon_remove_bin (mnemon, bin);
1132 (time_limit == 0.0 || response_time < time_limit))
1135 /* We reserve an item score of 0 for an item that has
1136 * never been asked. */
1137 if (item->score == 0) {
1139 mnemon->unlearned--;
1140 printf ("You got it!");
1141 } else if (item->score < 0) {
1142 printf ("Yes---just give me %d more.",
1144 } else if (item->score == 1) {
1145 printf ("On your first try, no less!");
1147 printf ("Masterful (%dx).", item->score);
1148 if (mnemon->to_master)
1149 mnemon->to_master--;
1153 printf (" %s is the correct answer.",
1156 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1157 response_time, time_limit);
1158 /* Penalize an incorrect response by forcing the score
1160 if (item->score >= 0) {
1161 if (item->score > 0)
1162 printf (" Oops, you knew that, right? (%dx)\n ",
1164 mnemon->unlearned++;
1165 /* We increase to_master here as an extra penalty. If the
1166 * user is forgetting stuff learned previously, then more
1167 * time should be spent on mastering than learning new
1168 * items. Note that we only do this during the initial
1169 * phase while new items are still being introduced. */
1170 if (mnemon->to_introduce)
1171 mnemon->to_master++;
1172 /* We go to -2 to force a little extra reinforcement
1173 * when re-learning an item, (otherwise, it will often
1174 * get asked again immediately where it is easy to get
1175 * a correct response without any learning). */
1183 if (mnemon->to_introduce)
1184 printf ("%d to come. ", mnemon->to_introduce);
1185 if (mnemon->unlearned)
1186 printf ("%d still unlearned. ", mnemon->unlearned);
1187 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1188 printf ("%d items to master", mnemon->to_master);
1191 bin = mnemon_get_bin (mnemon, item->score);
1193 bin_add_item (bin, item);
1197 mnemon_show_challenge (mnemon_t *mnemon,
1198 challenge_type_t challenge_type,
1199 const char *challenge)
1201 const char *program;
1204 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1205 printf ("%s\n", challenge);
1209 /* XXX: Yes, shelling out to system is total cheese. The planned
1210 * fix here is to bring graphical display in process, (or at least
1211 * have a custom external program that accepts image filenames on
1214 switch (challenge_type) {
1215 case CHALLENGE_TYPE_TEXT:
1218 case CHALLENGE_TYPE_IMAGE:
1219 program = "xli -gamma 2.2";
1221 case CHALLENGE_TYPE_AUDIO:
1224 case CHALLENGE_TYPE_MIDI:
1225 program = "timidity -Os";
1227 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
1228 program = "mnemon-tts";
1232 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1241 mnemon_hide_challenge (unused (mnemon_t *mnemon),
1242 challenge_type_t challenge_type)
1246 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1249 /* XXX: And this is just embarrassing (obviously wrong in several
1250 * ways). Hopefully I'll amend away any commit that includes this.
1252 xasprintf (&command, "killall xli");
1258 mnemon_do_challenges (mnemon_t *mnemon)
1263 category_t *category;
1267 /* Count the number of items with negative scores. */
1268 mnemon->unlearned = 0;
1269 for (i = 0; i < mnemon->num_bins; i++) {
1270 bin = &mnemon->bins[i];
1271 if (bin->score >= 0)
1273 mnemon->unlearned += bin->num_items;
1276 mnemon->to_introduce -= mnemon->unlearned;
1277 if (mnemon->to_introduce < 0)
1278 mnemon->to_introduce = 0;
1280 /* Get rid of bin with score of 0 if we aren't going to be
1281 * introducing anything from it. */
1282 if (mnemon->to_introduce == 0) {
1283 bin = mnemon_get_bin (mnemon, 0);
1284 mnemon_remove_bin (mnemon, bin);
1287 if (mnemon->unlearned) {
1288 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1289 if (mnemon->to_introduce)
1290 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1293 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1298 struct timeval start, end;
1300 mnemon_select_item (mnemon, &bin, &item_index, &category);
1301 item = bin->items[item_index];
1304 if (category->time_limit > 0.0) {
1305 response = readline ("The next one is timed. Press enter when ready:");
1309 mnemon_show_challenge (mnemon, category->challenge_type,
1312 gettimeofday (&start, NULL);
1313 response = readline ("> ");
1314 gettimeofday (&end, NULL);
1316 mnemon_hide_challenge (mnemon, category->challenge_type);
1318 /* Terminate on EOF */
1319 if (response == NULL) {
1324 if (response[0] == '/') {
1325 mnemon_handle_command (mnemon, response + 1);
1332 mnemon_handle_response (mnemon, bin, item_index,
1334 (end.tv_sec + end.tv_usec / 1e6) -
1335 (start.tv_sec + start.tv_usec / 1e6),
1336 category->time_limit);
1339 /* Replay audio challenges for reinforcement. */
1340 if (category->repeat)
1342 mnemon_show_challenge (mnemon, category->challenge_type,
1344 printf ("%s\n", item->challenge);
1347 } while (mnemon->to_introduce ||
1348 mnemon->unlearned ||
1349 mnemon->to_master > 0);
1353 main (int argc, char *argv[])
1358 void _load_categories()
1362 for (i = 1; i < argc; i++)
1363 mnemon_load_category (&mnemon, argv[i]);
1365 mnemon_load (&mnemon);
1369 srand (time (NULL));
1371 mnemon_init (&mnemon);
1373 _load_categories ();
1375 mnemon_do_challenges (&mnemon);
1377 mnemon_save (&mnemon);
1379 mnemon_fini (&mnemon);
1381 mnemon_init (&mnemon);
1383 _load_categories ();
1385 printf ("Great job.\nHere are your current results:\n");
1386 mnemon_print_histogram (&mnemon, NULL, 0);
1387 response = readline ("Press enter to quit.\n");
1390 mnemon_fini (&mnemon);