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); \
47 typedef struct _item {
61 CATEGORY_ORDER_RANDOM,
62 CATEGORY_ORDER_SEQUENTIAL
70 CHALLENGE_TYPE_TEXT_TO_SPEECH
73 typedef struct _category {
79 /* Support sequential introduction of items from bin 0 */
80 category_order_t order;
81 /* Support categories where responses are timed (0.0 == disable). */
84 /* Support challenges of non-text types (image, audio, etc.) */
85 challenge_type_t challenge_type;
86 /* Whether to repeat afterwards (for a little extra reinforcement) */
90 typedef struct _mnemon {
95 category_t *categories;
108 xmalloc (size_t size)
114 fprintf (stderr, "Error: out of memory\n");
122 xrealloc (void *ptr, size_t size)
126 ret = realloc (ptr, size);
128 fprintf (stderr, "Error: out of memory\n");
136 xstrdup (const char *s)
142 fprintf (stderr, "Error: out of memory\n");
150 xstrndup (const char *s, size_t n)
154 ret = strndup (s, n);
156 fprintf (stderr, "Error: out of memory\n");
164 xasprintf (char **strp, const char *fmt, ...)
170 ret = vasprintf (strp, fmt, ap);
174 fprintf (stderr, "Error: out of memory\n");
180 item_init (item_t *item,
182 const char *challenge,
183 const char *response)
187 item->challenge = xmalloc (strlen (challenge) + 1 +
188 strlen (response) + 1);
189 item->response = item->challenge + strlen (challenge) + 1;
191 strcpy (item->challenge, challenge);
192 strcpy (item->response, response);
196 item_fini (item_t *item)
198 /* item->response shares allocation with item->challenge, so
199 * doesn't require a separate call to free */
200 free (item->challenge);
204 category_init (category_t *category,
207 category->name = xstrdup (name);
209 category->items_size = 0;
210 category->num_items = 0;
211 category->items = NULL;
212 category->order = CATEGORY_ORDER_RANDOM;
213 category->time_limit = 0.0;
214 category->bin_zero_head = 0;
215 category->challenge_type = CHALLENGE_TYPE_TEXT;
216 category->repeat = 0;
220 category_fini (category_t *category)
224 for (i = 0; i < category->num_items; i++)
225 item_fini (&category->items[i]);
227 free (category->items);
229 free (category->name);
233 category_grow (category_t *category)
235 if (category->items_size)
236 category->items_size *= 2;
238 category->items_size = 1;
240 category->items = xrealloc (category->items,
241 category->items_size * sizeof (item_t));
245 category_add_item (category_t *category,
247 const char *challenge,
248 const char *response)
252 if (category->num_items == category->items_size)
253 category_grow (category);
255 item = &category->items[category->num_items++];
257 item_init (item, score, challenge, response);
263 category_next_bin_zero_item (category_t *category)
265 int *i = &category->bin_zero_head;
267 for ( ; *i < category->num_items; *i = *i + 1)
268 if (category->items[*i].score == 0)
269 return &category->items[*i];
275 category_print (category_t *category,
281 fprintf (file, "order = %s\n\n",
282 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
283 fprintf (file, "time = %f\n\n",
284 category->time_limit);
286 fprintf (file, "challenge = ");
287 switch (category->challenge_type) {
288 case CHALLENGE_TYPE_TEXT:
289 fprintf (file, "text");
291 case CHALLENGE_TYPE_IMAGE:
292 fprintf (file, "image");
294 case CHALLENGE_TYPE_AUDIO:
295 fprintf (file, "audio");
297 case CHALLENGE_TYPE_MIDI:
298 fprintf (file, "midi");
300 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
301 fprintf (file, "text-to-speech");
304 fprintf (file, "\n\n");
306 fprintf (file, "repeat = %d\n\n", category->repeat);
308 for (i = 0; i < category->num_items; i++) {
309 item = &category->items[i];
311 fprintf (file, "\n");
312 fprintf (file, "%d\n%s\n%s\n",
320 bin_init (bin_t *bin,
331 bin_fini (bin_t *bin)
337 bin_grow (bin_t *bin)
340 bin->items_size *= 2;
344 bin->items = xrealloc (bin->items,
345 bin->items_size * sizeof (item_t*));
349 bin_add_item (bin_t *bin,
352 assert (item->score == bin->score);
354 if (bin->num_items == bin->items_size)
357 bin->items[bin->num_items++] = item;
361 bin_remove_item (bin_t *bin,
364 /* Replace the current item with the last item, (no need to shift
365 * any more than that since we don't care about the order of the
366 * items within a bin). */
369 bin->items[item_index] = bin->items[bin->num_items];
372 /* Find the index for an item within a bin.
374 * XXX: This is currently a linear search, so is a potential
375 * performance problem.
378 bin_item_index (bin_t *bin,
383 for (i = 0; i < bin->num_items; i++)
384 if (bin->items[i] == item)
390 typedef int (item_match_predicate_t) (void *closure, item_t *item);
392 /* Return the number of items in the bin from the given category (or
393 * from all categories if category == NULL) */
395 bin_num_items_matching (bin_t *bin,
396 item_match_predicate_t *predicate,
399 int i, num_items = 0;
401 if (predicate == NULL)
402 return bin->num_items;
404 for (i = 0; i < bin->num_items; i++)
405 if ((predicate) (closure, bin->items[i]))
412 mnemon_init (mnemon_t *mnemon)
416 home = getenv ("HOME");
420 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
422 mnemon->categories_size = 0;
423 mnemon->num_categories = 0;
424 mnemon->categories = NULL;
426 mnemon->bins_size = 0;
427 mnemon->num_bins = 0;
430 mnemon->to_introduce = 10;
431 mnemon->to_master = 10;
432 mnemon->unlearned = 0;
433 mnemon->mastered = -1;
437 mnemon_fini (mnemon_t *mnemon)
441 for (i = 0; i < mnemon->num_bins; i++)
442 bin_fini (&mnemon->bins[i]);
445 for (i = 0; i < mnemon->num_categories; i++)
446 category_fini (&mnemon->categories[i]);
447 free (mnemon->categories);
449 free (mnemon->dir_name);
453 mnemon_categories_grow (mnemon_t *mnemon)
455 if (mnemon->categories_size)
456 mnemon->categories_size *= 2;
458 mnemon->categories_size = 1;
460 mnemon->categories = xrealloc (mnemon->categories,
461 mnemon->categories_size * sizeof (category_t));
464 /* Get a category by name if it exists */
466 mnemon_get_category_if_exists (mnemon_t *mnemon,
471 for (i = 0; i < mnemon->num_categories; i++)
472 if (strcmp (mnemon->categories[i].name, name) == 0)
473 return &mnemon->categories[i];
478 /* Get a category by name, creating new one if necessary. */
480 mnemon_get_category (mnemon_t *mnemon,
483 category_t *category;
485 category = mnemon_get_category_if_exists (mnemon, name);
489 mnemon_categories_grow (mnemon);
491 category = &mnemon->categories[mnemon->num_categories++];
493 category_init (category, name);
499 mnemon_bins_grow (mnemon_t *mnemon)
501 if (mnemon->bins_size)
502 mnemon->bins_size *= 2;
504 mnemon->bins_size = 1;
506 mnemon->bins = xrealloc (mnemon->bins,
507 mnemon->bins_size * sizeof (bin_t));
511 mnemon_get_bin (mnemon_t *mnemon,
517 for (i = 0; i < mnemon->num_bins; i++)
518 if (mnemon->bins[i].score == score)
519 return &mnemon->bins[i];
520 else if (mnemon->bins[i].score > score)
523 if (mnemon->num_bins == mnemon->bins_size)
524 mnemon_bins_grow (mnemon);
526 bin = &mnemon->bins[i];
528 /* Make room to insert new bin at its sorted location. */
529 if (i < mnemon->num_bins)
530 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
533 bin_init (bin, score);
539 mnemon_remove_bin (mnemon_t *mnemon,
542 int i = bin - mnemon->bins;
546 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
553 int len = strlen (s);
556 if (s[len - 1] == '\n')
561 trim_space (char *string)
566 while (*s && isspace (*s))
571 s = string + strlen (string) - 1;
572 while (s > string && isspace (*s)) {
581 mnemon_load_category (mnemon_t *mnemon,
585 char *line = NULL, *end;
586 size_t line_size = 0;
590 category_t *category;
594 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
595 sprintf (path, "%s/%s", mnemon->dir_name, name);
597 file = fopen (path, "r");
599 fprintf (stderr, "Error: Failed to open %s: %s\n",
600 path, strerror (errno));
604 fstat (fileno(file), &st);
605 if (! S_ISREG(st.st_mode)) {
606 fprintf (stderr, "Error: File %s is not a regular file.\n", path);
610 category = mnemon_get_category (mnemon, name);
612 #define READ_LINE do { \
613 bytes_read = getline (&line, &line_size, file); \
614 if (bytes_read == -1) \
622 char *name, *equal, *value;
624 /* Ignore blank lines */
629 /* An initial digit means we hit an item. Trigger the
630 * spaghetti machine. */
631 if ((*line >= '0' && *line <= '9') || *line == '-')
634 equal = strchr (line, '=');
636 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
637 line, path, line_count);
645 name = trim_space (name);
646 value = trim_space (value);
648 if (strcmp (name, "order") == 0) {
649 if (strcmp (value, "sequential") == 0) {
650 category->order = CATEGORY_ORDER_SEQUENTIAL;
651 } else if (strcmp (value, "random") == 0) {
652 category->order = CATEGORY_ORDER_RANDOM;
654 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
655 value, path, line_count);
658 } else if (strcmp (name, "time") == 0) {
661 limit = strtod (value, &end);
662 while (isspace (*end))
665 category->time_limit = limit;
667 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
668 value, path, line_count);
671 } else if (strcmp (name, "challenge") == 0) {
672 if (strcmp (value, "text") == 0) {
673 category->challenge_type = CHALLENGE_TYPE_TEXT;
674 } else if (strcmp (value, "image") == 0) {
675 category->challenge_type = CHALLENGE_TYPE_IMAGE;
676 } else if (strcmp (value, "audio") == 0) {
677 category->challenge_type = CHALLENGE_TYPE_AUDIO;
678 } else if (strcmp (value, "midi") == 0) {
679 category->challenge_type = CHALLENGE_TYPE_MIDI;
680 } else if (strcmp (value, "text-to-speech") == 0) {
681 category->challenge_type = CHALLENGE_TYPE_TEXT_TO_SPEECH;
683 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
684 value, path, line_count);
687 } else if (strcmp (name, "repeat") == 0) {
688 if (strcmp (value, "0") == 0)
689 category->repeat = 0;
691 category->repeat = 1;
693 fprintf (stderr, "Unknown option %s at %s:%d\n",
694 name, path, line_count);
702 char *challenge, *response;
704 /* Ignore blank lines */
709 /* Read bin number */
711 score = strtol (line, &end, 10);
713 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
714 line, path, line_count);
720 challenge = strdup (line);
726 category_add_item (category, score, challenge, response);
736 /* Resize category items to fit exactly. */
737 category->items_size = category->num_items;
738 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
740 /* Now that the category is completely loaded, with stable
741 * pointers to every item, we can add each item to its appropriate
743 for (i = 0; i < category->num_items; i++) {
744 item_t *item = &category->items[i];
745 bin_t *bin = mnemon_get_bin (mnemon, item->score);
747 bin_add_item (bin, item);
752 mnemon_load (mnemon_t *mnemon)
755 struct dirent *dirent;
757 dir = opendir (mnemon->dir_name);
759 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
760 mnemon->dir_name, strerror (errno));
765 dirent = readdir (dir);
769 if (dirent->d_type == DT_REG) {
770 /* Ignore files matching *~, (yes, this shouldn't be
771 * hard-coded in such an ad-hoc way, but there you go. */
772 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
773 mnemon_load_category (mnemon, dirent->d_name);
781 mnemon_save (mnemon_t *mnemon)
784 char *filename, *lock_filename;
786 category_t *category;
788 for (i = 0; i < mnemon->num_categories; i++) {
789 category = &mnemon->categories[i];
791 xasprintf (&filename, "%s/%s",
792 mnemon->dir_name, category->name);
793 xasprintf (&lock_filename, "%s/.#%s",
794 mnemon->dir_name, category->name);
796 file = fopen (lock_filename, "w");
798 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
799 lock_filename, strerror (errno));
803 category_print (category, file);
805 fsync (fileno (file));
808 err = rename (lock_filename, filename);
810 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
811 lock_filename, filename, strerror (errno));
816 free (lock_filename);
820 /* Return a uniformly-distributed pseudo-random integer within the
823 * 0 <= result < num_values
826 rand_within (int num_values)
828 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
831 /* Return an exponentially-distributed pseudo-random integer within
834 * 0 <= result < num_values
836 * The distribution is such that each successively larger value will
837 * occur with a probability of half of the previous value.
840 rand_within_exponential (int num_values)
843 static uint32_t mask = 0;
847 /* Optimize the constant case. */
857 while (mask > RAND_MAX)
864 if (ones == num_values)
872 /* Find the category to which an item belongs. */
874 mnemon_item_category (mnemon_t *mnemon,
877 category_t *category;
880 for (i = 0; i < mnemon->num_categories; i++) {
881 category = &mnemon->categories[i];
882 item_index = item - category->items;
883 if (item_index >= 0 && item_index < category->num_items)
890 typedef struct _item_in_category_closure
893 category_t *category;
894 } item_in_category_closure_t;
897 mnemon_item_in_category (void *closure, item_t *item)
899 item_in_category_closure_t *iicc = closure;
900 mnemon_t *mnemon = iicc->mnemon;
901 category_t *category = iicc->category;
903 return (mnemon_item_category (mnemon, item) == category);
906 typedef struct _item_in_category_of_length_closure
909 category_t *category;
911 } item_in_category_of_length_closure_t;
914 mnemon_item_in_category_of_length (void *closure, item_t *item)
916 item_in_category_of_length_closure_t *iicolc = closure;
917 mnemon_t *mnemon = iicolc->mnemon;
918 category_t *category = iicolc->category;
919 int length = iicolc->length;
921 if (mnemon_item_category (mnemon, item) != category)
924 return strlen (item->challenge) == length;
928 mnemon_select_item (mnemon_t *mnemon,
931 category_t **category_ret)
933 int bin_index, item_index;
936 category_t *category;
938 bin_index = rand_within_exponential (mnemon->num_bins);
939 bin = &mnemon->bins[bin_index];
941 /* The most intuitive understanding of the to_introduce counter is
942 * that it's tracking never-before-learned items as they are
943 * pulled from the bin with score 0. But that bin can become
944 * empty. So the refined rule is that we decrement to_introduce
945 * whenever we pull from the lowest-indexed bin with a
946 * non-negative score. */
947 if (mnemon->to_introduce && bin->score >=0 &&
948 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
950 mnemon->to_introduce--;
953 item_index = rand_within (bin->num_items);
955 item = bin->items[item_index];
956 category = mnemon_item_category (mnemon, item);
958 if (bin->score == 0) {
959 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
960 item = category_next_bin_zero_item (category);
962 item_index = bin_item_index (bin, item);
967 *item_index_ret = item_index;
968 *category_ret = category;
972 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
973 #define HISTOGRAM_BAR_WIDTH 63
976 print_histogram_bar (double size,
979 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
980 static char const *boxes[8] = {
985 while (size > units_per_cell) {
987 size -= units_per_cell;
990 size /= units_per_cell;
994 else if (size > 6.5/8.0)
996 else if (size > 5.5/8.0)
998 else if (size > 4.5/8.0)
1000 else if (size > 3.5/8.0)
1002 else if (size > 2.5/8.0)
1004 else if (size > 1.5/8.0)
1006 else if (size > 0.5/8.0)
1013 mnemon_print_histogram (mnemon_t *mnemon,
1014 const char *category_name,
1017 int i, last_score, max;
1018 category_t *category = NULL;
1021 item_match_predicate_t *predicate = NULL;
1022 void *closure = NULL;
1023 item_in_category_closure_t item_in_category;
1024 item_in_category_of_length_closure_t item_in_category_of_length;
1026 if (mnemon->num_bins == 0)
1029 if (category_name) {
1030 category = mnemon_get_category_if_exists (mnemon, category_name);
1033 predicate = mnemon_item_in_category_of_length;
1034 item_in_category_of_length.mnemon = mnemon;
1035 item_in_category_of_length.category = category;
1036 item_in_category_of_length.length = length;
1037 closure = &item_in_category_of_length;
1039 predicate = mnemon_item_in_category;
1040 item_in_category.mnemon = mnemon;
1041 item_in_category.category = category;
1042 closure = &item_in_category;
1047 for (i = 0; i < mnemon->num_bins; i++) {
1048 num_items = bin_num_items_matching (&mnemon->bins[i],
1049 predicate, closure);
1050 if (i == 0 || num_items > max)
1054 for (i = 0; i < mnemon->num_bins; i++) {
1055 bin = &mnemon->bins[i];
1057 while (bin->score - last_score > 1)
1058 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1059 num_items = bin_num_items_matching (bin,
1060 predicate, closure);
1061 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1062 print_histogram_bar (num_items, max);
1063 last_score = bin->score;
1068 mnemon_handle_command (mnemon_t *mnemon,
1069 const char *command)
1073 switch (command[0]) {
1074 /* 'h' for histogram */
1077 char *category = NULL;
1081 arg += strspn (arg, " \t");
1082 len = strcspn (arg, " \t");
1084 category = xstrndup (arg, len);
1086 arg += strspn (arg, " \t");
1088 length = atoi (arg);
1090 mnemon_print_histogram (mnemon, category, length);
1093 /* 'r' for repeat */
1096 /* Nothing necessary for repeating. */
1100 printf ("Unknown command: %s\n", command);
1106 mnemon_handle_response (mnemon_t *mnemon,
1110 const char *response,
1111 double response_time,
1116 correct = (strcmp (response, item->response) == 0);
1118 bin_remove_item (bin, item_index);
1120 /* If the bin is now empty, we must remove it. Also if we just
1121 * picked the last word we'll ever pick from the bin with
1122 * score 0, then we can remove that as well. */
1123 if (bin->num_items == 0 ||
1124 (bin->score == 0 && mnemon->to_introduce == 0))
1126 mnemon_remove_bin (mnemon, bin);
1130 (time_limit == 0.0 || response_time < time_limit))
1133 /* We reserve an item score of 0 for an item that has
1134 * never been asked. */
1135 if (item->score == 0) {
1137 mnemon->unlearned--;
1138 printf ("You got it!");
1139 } else if (item->score < 0) {
1140 printf ("Yes---just give me %d more.",
1142 } else if (item->score == 1) {
1143 printf ("On your first try, no less!");
1145 printf ("Masterful (%dx).", item->score);
1146 if (mnemon->to_master)
1147 mnemon->to_master--;
1151 printf (" %s is the correct answer.",
1154 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1155 response_time, time_limit);
1156 /* Penalize an incorrect response by forcing the score
1158 if (item->score >= 0) {
1159 if (item->score > 0)
1160 printf (" Oops, you knew that, right? (%dx)\n ",
1162 mnemon->unlearned++;
1163 /* We increase to_master here as an extra penalty. If the
1164 * user is forgetting stuff learned previously, then more
1165 * time should be spent on mastering than learning new
1166 * items. Note that we only do this during the initial
1167 * phase while new items are still being introduced. */
1168 if (mnemon->to_introduce)
1169 mnemon->to_master++;
1170 /* We go to -2 to force a little extra reinforcement
1171 * when re-learning an item, (otherwise, it will often
1172 * get asked again immediately where it is easy to get
1173 * a correct response without any learning). */
1181 if (mnemon->to_introduce)
1182 printf ("%d to come. ", mnemon->to_introduce);
1183 if (mnemon->unlearned)
1184 printf ("%d still unlearned. ", mnemon->unlearned);
1185 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1186 printf ("%d items to master", mnemon->to_master);
1189 bin = mnemon_get_bin (mnemon, item->score);
1191 bin_add_item (bin, item);
1195 mnemon_show_challenge (mnemon_t *mnemon,
1196 challenge_type_t challenge_type,
1197 const char *challenge)
1199 const char *program;
1202 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1203 printf ("%s\n", challenge);
1207 /* XXX: Yes, shelling out to system is total cheese. The planned
1208 * fix here is to bring graphical display in process, (or at least
1209 * have a custom external program that accepts image filenames on
1212 switch (challenge_type) {
1213 case CHALLENGE_TYPE_TEXT:
1216 case CHALLENGE_TYPE_IMAGE:
1217 program = "xli -gamma 2.2";
1219 case CHALLENGE_TYPE_AUDIO:
1222 case CHALLENGE_TYPE_MIDI:
1223 program = "timidity -Os";
1225 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
1226 program = "mnemon-tts";
1230 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1239 mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
1243 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1246 /* XXX: And this is just embarrassing (obviously wrong in several
1247 * ways). Hopefully I'll amend away any commit that includes this.
1249 xasprintf (&command, "killall xli");
1255 mnemon_do_challenges (mnemon_t *mnemon)
1260 category_t *category;
1264 /* Count the number of items with negative scores. */
1265 mnemon->unlearned = 0;
1266 for (i = 0; i < mnemon->num_bins; i++) {
1267 bin = &mnemon->bins[i];
1268 if (bin->score >= 0)
1270 mnemon->unlearned += bin->num_items;
1273 mnemon->to_introduce -= mnemon->unlearned;
1274 if (mnemon->to_introduce < 0)
1275 mnemon->to_introduce = 0;
1277 /* Get rid of bin with score of 0 if we aren't going to be
1278 * introducing anything from it. */
1279 if (mnemon->to_introduce == 0) {
1280 bin = mnemon_get_bin (mnemon, 0);
1281 mnemon_remove_bin (mnemon, bin);
1284 if (mnemon->unlearned) {
1285 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1286 if (mnemon->to_introduce)
1287 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1290 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1295 struct timeval start, end;
1297 mnemon_select_item (mnemon, &bin, &item_index, &category);
1298 item = bin->items[item_index];
1301 if (category->time_limit > 0.0) {
1302 response = readline ("The next one is timed. Press enter when ready:");
1306 mnemon_show_challenge (mnemon, category->challenge_type,
1309 gettimeofday (&start, NULL);
1310 response = readline ("> ");
1311 gettimeofday (&end, NULL);
1313 mnemon_hide_challenge (mnemon, category->challenge_type);
1315 /* Terminate on EOF */
1316 if (response == NULL) {
1321 if (response[0] == '/') {
1322 mnemon_handle_command (mnemon, response + 1);
1329 mnemon_handle_response (mnemon, bin, item_index,
1331 (end.tv_sec + end.tv_usec / 1e6) -
1332 (start.tv_sec + start.tv_usec / 1e6),
1333 category->time_limit);
1336 /* Replay audio challenges for reinforcement. */
1337 if (category->repeat)
1339 mnemon_show_challenge (mnemon, category->challenge_type,
1341 printf ("%s\n", item->challenge);
1344 } while (mnemon->to_introduce ||
1345 mnemon->unlearned ||
1346 mnemon->to_master > 0);
1350 main (int argc, char *argv[])
1355 void _load_categories()
1359 for (i = 1; i < argc; i++)
1360 mnemon_load_category (&mnemon, argv[i]);
1362 mnemon_load (&mnemon);
1366 srand (time (NULL));
1368 mnemon_init (&mnemon);
1370 _load_categories ();
1372 mnemon_do_challenges (&mnemon);
1374 mnemon_save (&mnemon);
1376 mnemon_fini (&mnemon);
1378 mnemon_init (&mnemon);
1380 _load_categories ();
1382 printf ("Great job.\nHere are your current results:\n");
1383 mnemon_print_histogram (&mnemon, NULL, 0);
1384 response = readline ("Press enter to quit.\n");
1387 mnemon_fini (&mnemon);