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>
35 #include <readline/readline.h>
36 #include <readline/history.h>
38 #define ASSERT_NOT_REACHED \
40 static const int NOT_REACHED = 0; \
41 assert (NOT_REACHED); \
46 typedef struct _item {
60 CATEGORY_ORDER_RANDOM,
61 CATEGORY_ORDER_SEQUENTIAL
69 CHALLENGE_TYPE_TEXT_TO_SPEECH
72 typedef struct _category {
78 /* Support sequential introduction of items from bin 0 */
79 category_order_t order;
80 /* Support categories where responses are timed (0.0 == disable). */
83 /* Support challenges of non-text types (image, audio, etc.) */
84 challenge_type_t challenge_type;
85 /* Whether to repeat afterwards (for a little extra reinforcement) */
89 typedef struct _mnemon {
94 category_t *categories;
107 xmalloc (size_t size)
113 fprintf (stderr, "Error: out of memory\n");
121 xrealloc (void *ptr, size_t size)
125 ret = realloc (ptr, size);
127 fprintf (stderr, "Error: out of memory\n");
135 xstrdup (const char *s)
141 fprintf (stderr, "Error: out of memory\n");
149 xstrndup (const char *s, size_t n)
153 ret = strndup (s, n);
155 fprintf (stderr, "Error: out of memory\n");
163 xasprintf (char **strp, const char *fmt, ...)
169 ret = vasprintf (strp, fmt, ap);
173 fprintf (stderr, "Error: out of memory\n");
179 item_init (item_t *item,
181 const char *challenge,
182 const char *response)
186 item->challenge = xmalloc (strlen (challenge) + 1 +
187 strlen (response) + 1);
188 item->response = item->challenge + strlen (challenge) + 1;
190 strcpy (item->challenge, challenge);
191 strcpy (item->response, response);
195 item_fini (item_t *item)
197 /* item->response shares allocation with item->challenge, so
198 * doesn't require a separate call to free */
199 free (item->challenge);
203 category_init (category_t *category,
206 category->name = xstrdup (name);
208 category->items_size = 0;
209 category->num_items = 0;
210 category->items = NULL;
211 category->order = CATEGORY_ORDER_RANDOM;
212 category->time_limit = 0.0;
213 category->bin_zero_head = 0;
214 category->challenge_type = CHALLENGE_TYPE_TEXT;
215 category->repeat = 0;
219 category_fini (category_t *category)
223 for (i = 0; i < category->num_items; i++)
224 item_fini (&category->items[i]);
226 free (category->items);
228 free (category->name);
232 category_grow (category_t *category)
234 if (category->items_size)
235 category->items_size *= 2;
237 category->items_size = 1;
239 category->items = xrealloc (category->items,
240 category->items_size * sizeof (item_t));
244 category_add_item (category_t *category,
246 const char *challenge,
247 const char *response)
251 if (category->num_items == category->items_size)
252 category_grow (category);
254 item = &category->items[category->num_items++];
256 item_init (item, score, challenge, response);
262 category_next_bin_zero_item (category_t *category)
264 int *i = &category->bin_zero_head;
266 for ( ; *i < category->num_items; *i = *i + 1)
267 if (category->items[*i].score == 0)
268 return &category->items[*i];
274 category_print (category_t *category,
280 fprintf (file, "order = %s\n\n",
281 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
282 fprintf (file, "time = %f\n\n",
283 category->time_limit);
285 fprintf (file, "challenge = ");
286 switch (category->challenge_type) {
287 case CHALLENGE_TYPE_TEXT:
288 fprintf (file, "text");
290 case CHALLENGE_TYPE_IMAGE:
291 fprintf (file, "image");
293 case CHALLENGE_TYPE_AUDIO:
294 fprintf (file, "audio");
296 case CHALLENGE_TYPE_MIDI:
297 fprintf (file, "midi");
299 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
300 fprintf (file, "text-to-speech");
303 fprintf (file, "\n\n");
305 fprintf (file, "repeat = %d\n\n", category->repeat);
307 for (i = 0; i < category->num_items; i++) {
308 item = &category->items[i];
310 fprintf (file, "\n");
311 fprintf (file, "%d\n%s\n%s\n",
319 bin_init (bin_t *bin,
330 bin_fini (bin_t *bin)
336 bin_grow (bin_t *bin)
339 bin->items_size *= 2;
343 bin->items = xrealloc (bin->items,
344 bin->items_size * sizeof (item_t*));
348 bin_add_item (bin_t *bin,
351 assert (item->score == bin->score);
353 if (bin->num_items == bin->items_size)
356 bin->items[bin->num_items++] = item;
360 bin_remove_item (bin_t *bin,
363 /* Replace the current item with the last item, (no need to shift
364 * any more than that since we don't care about the order of the
365 * items within a bin). */
368 bin->items[item_index] = bin->items[bin->num_items];
371 /* Find the index for an item within a bin.
373 * XXX: This is currently a linear search, so is a potential
374 * performance problem.
377 bin_item_index (bin_t *bin,
382 for (i = 0; i < bin->num_items; i++)
383 if (bin->items[i] == item)
389 typedef int (item_match_predicate_t) (void *closure, item_t *item);
391 /* Return the number of items in the bin from the given category (or
392 * from all categories if category == NULL) */
394 bin_num_items_matching (bin_t *bin,
395 item_match_predicate_t *predicate,
398 int i, num_items = 0;
400 if (predicate == NULL)
401 return bin->num_items;
403 for (i = 0; i < bin->num_items; i++)
404 if ((predicate) (closure, bin->items[i]))
411 mnemon_init (mnemon_t *mnemon)
415 home = getenv ("HOME");
419 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
421 mnemon->categories_size = 0;
422 mnemon->num_categories = 0;
423 mnemon->categories = NULL;
425 mnemon->bins_size = 0;
426 mnemon->num_bins = 0;
429 mnemon->to_introduce = 10;
430 mnemon->to_master = 10;
431 mnemon->unlearned = 0;
432 mnemon->mastered = -1;
436 mnemon_fini (mnemon_t *mnemon)
440 for (i = 0; i < mnemon->num_bins; i++)
441 bin_fini (&mnemon->bins[i]);
444 for (i = 0; i < mnemon->num_categories; i++)
445 category_fini (&mnemon->categories[i]);
446 free (mnemon->categories);
448 free (mnemon->dir_name);
452 mnemon_categories_grow (mnemon_t *mnemon)
454 if (mnemon->categories_size)
455 mnemon->categories_size *= 2;
457 mnemon->categories_size = 1;
459 mnemon->categories = xrealloc (mnemon->categories,
460 mnemon->categories_size * sizeof (category_t));
463 /* Get a category by name if it exists */
465 mnemon_get_category_if_exists (mnemon_t *mnemon,
470 for (i = 0; i < mnemon->num_categories; i++)
471 if (strcmp (mnemon->categories[i].name, name) == 0)
472 return &mnemon->categories[i];
477 /* Get a category by name, creating new one if necessary. */
479 mnemon_get_category (mnemon_t *mnemon,
482 category_t *category;
484 category = mnemon_get_category_if_exists (mnemon, name);
488 mnemon_categories_grow (mnemon);
490 category = &mnemon->categories[mnemon->num_categories++];
492 category_init (category, name);
498 mnemon_bins_grow (mnemon_t *mnemon)
500 if (mnemon->bins_size)
501 mnemon->bins_size *= 2;
503 mnemon->bins_size = 1;
505 mnemon->bins = xrealloc (mnemon->bins,
506 mnemon->bins_size * sizeof (bin_t));
510 mnemon_get_bin (mnemon_t *mnemon,
516 for (i = 0; i < mnemon->num_bins; i++)
517 if (mnemon->bins[i].score == score)
518 return &mnemon->bins[i];
519 else if (mnemon->bins[i].score > score)
522 if (mnemon->num_bins == mnemon->bins_size)
523 mnemon_bins_grow (mnemon);
525 bin = &mnemon->bins[i];
527 /* Make room to insert new bin at its sorted location. */
528 if (i < mnemon->num_bins)
529 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
532 bin_init (bin, score);
538 mnemon_remove_bin (mnemon_t *mnemon,
541 int i = bin - mnemon->bins;
545 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
552 int len = strlen (s);
555 if (s[len - 1] == '\n')
560 trim_space (char *string)
565 while (*s && isspace (*s))
570 s = string + strlen (string) - 1;
571 while (s > string && isspace (*s)) {
580 mnemon_load_category (mnemon_t *mnemon,
584 char *line = NULL, *end;
585 size_t line_size = 0;
589 category_t *category;
592 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
593 sprintf (path, "%s/%s", mnemon->dir_name, name);
595 file = fopen (path, "r");
597 fprintf (stderr, "Error: Failed to open %s: %s\n",
598 path, strerror (errno));
602 category = mnemon_get_category (mnemon, name);
604 #define READ_LINE do { \
605 bytes_read = getline (&line, &line_size, file); \
606 if (bytes_read == -1) \
614 char *name, *equal, *value;
616 /* Ignore blank lines */
621 /* An initial digit means we hit an item. Trigger the
622 * spaghetti machine. */
623 if ((*line >= '0' && *line <= '9') || *line == '-')
626 equal = strchr (line, '=');
628 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
629 line, path, line_count);
637 name = trim_space (name);
638 value = trim_space (value);
640 if (strcmp (name, "order") == 0) {
641 if (strcmp (value, "sequential") == 0) {
642 category->order = CATEGORY_ORDER_SEQUENTIAL;
643 } else if (strcmp (value, "random") == 0) {
644 category->order = CATEGORY_ORDER_RANDOM;
646 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
647 value, path, line_count);
650 } else if (strcmp (name, "time") == 0) {
653 limit = strtod (value, &end);
654 while (isspace (*end))
657 category->time_limit = limit;
659 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
660 value, path, line_count);
663 } else if (strcmp (name, "challenge") == 0) {
664 if (strcmp (value, "text") == 0) {
665 category->challenge_type = CHALLENGE_TYPE_TEXT;
666 } else if (strcmp (value, "image") == 0) {
667 category->challenge_type = CHALLENGE_TYPE_IMAGE;
668 } else if (strcmp (value, "audio") == 0) {
669 category->challenge_type = CHALLENGE_TYPE_AUDIO;
670 } else if (strcmp (value, "midi") == 0) {
671 category->challenge_type = CHALLENGE_TYPE_MIDI;
672 } else if (strcmp (value, "text-to-speech") == 0) {
673 category->challenge_type = CHALLENGE_TYPE_TEXT_TO_SPEECH;
675 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
676 value, path, line_count);
679 } else if (strcmp (name, "repeat") == 0) {
680 if (strcmp (value, "0") == 0)
681 category->repeat = 0;
683 category->repeat = 1;
685 fprintf (stderr, "Unknown option %s at %s:%d\n",
686 name, path, line_count);
694 char *challenge, *response;
696 /* Ignore blank lines */
701 /* Read bin number */
703 score = strtol (line, &end, 10);
705 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
706 line, path, line_count);
712 challenge = strdup (line);
718 category_add_item (category, score, challenge, response);
728 /* Resize category items to fit exactly. */
729 category->items_size = category->num_items;
730 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
732 /* Now that the category is completely loaded, with stable
733 * pointers to every item, we can add each item to its appropriate
735 for (i = 0; i < category->num_items; i++) {
736 item_t *item = &category->items[i];
737 bin_t *bin = mnemon_get_bin (mnemon, item->score);
739 bin_add_item (bin, item);
744 mnemon_load (mnemon_t *mnemon)
747 struct dirent *dirent;
749 dir = opendir (mnemon->dir_name);
751 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
752 mnemon->dir_name, strerror (errno));
757 dirent = readdir (dir);
761 if (dirent->d_type == DT_REG) {
762 /* Ignore files matching *~, (yes, this shouldn't be
763 * hard-coded in such an ad-hoc way, but there you go. */
764 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
765 mnemon_load_category (mnemon, dirent->d_name);
773 mnemon_save (mnemon_t *mnemon)
776 char *filename, *lock_filename;
778 category_t *category;
780 for (i = 0; i < mnemon->num_categories; i++) {
781 category = &mnemon->categories[i];
783 xasprintf (&filename, "%s/%s",
784 mnemon->dir_name, category->name);
785 xasprintf (&lock_filename, "%s/.#%s",
786 mnemon->dir_name, category->name);
788 file = fopen (lock_filename, "w");
790 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
791 lock_filename, strerror (errno));
795 category_print (category, file);
797 fsync (fileno (file));
800 err = rename (lock_filename, filename);
802 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
803 lock_filename, filename, strerror (errno));
808 free (lock_filename);
812 /* Return a uniformly-distributed pseudo-random integer within the
815 * 0 <= result < num_values
818 rand_within (int num_values)
820 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
823 /* Return an exponentially-distributed pseudo-random integer within
826 * 0 <= result < num_values
828 * The distribution is such that each successively larger value will
829 * occur with a probability of half of the previous value.
832 rand_within_exponential (int num_values)
835 static uint32_t mask = 0;
839 /* Optimize the constant case. */
849 while (mask > RAND_MAX)
856 if (ones == num_values)
864 /* Find the category to which an item belongs. */
866 mnemon_item_category (mnemon_t *mnemon,
869 category_t *category;
872 for (i = 0; i < mnemon->num_categories; i++) {
873 category = &mnemon->categories[i];
874 item_index = item - category->items;
875 if (item_index >= 0 && item_index < category->num_items)
882 typedef struct _item_in_category_closure
885 category_t *category;
886 } item_in_category_closure_t;
889 mnemon_item_in_category (void *closure, item_t *item)
891 item_in_category_closure_t *iicc = closure;
892 mnemon_t *mnemon = iicc->mnemon;
893 category_t *category = iicc->category;
895 return (mnemon_item_category (mnemon, item) == category);
898 typedef struct _item_in_category_of_length_closure
901 category_t *category;
903 } item_in_category_of_length_closure_t;
906 mnemon_item_in_category_of_length (void *closure, item_t *item)
908 item_in_category_of_length_closure_t *iicolc = closure;
909 mnemon_t *mnemon = iicolc->mnemon;
910 category_t *category = iicolc->category;
911 int length = iicolc->length;
913 if (mnemon_item_category (mnemon, item) != category)
916 return strlen (item->challenge) == length;
920 mnemon_select_item (mnemon_t *mnemon,
923 category_t **category_ret)
925 int bin_index, item_index;
928 category_t *category;
930 bin_index = rand_within_exponential (mnemon->num_bins);
931 bin = &mnemon->bins[bin_index];
933 /* The most intuitive understanding of the to_introduce counter is
934 * that it's tracking never-before-learned items as they are
935 * pulled from the bin with score 0. But that bin can become
936 * empty. So the refined rule is that we decrement to_introduce
937 * whenever we pull from the lowest-indexed bin with a
938 * non-negative score. */
939 if (mnemon->to_introduce && bin->score >=0 &&
940 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
942 mnemon->to_introduce--;
945 item_index = rand_within (bin->num_items);
947 item = bin->items[item_index];
948 category = mnemon_item_category (mnemon, item);
950 if (bin->score == 0) {
951 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
952 item = category_next_bin_zero_item (category);
954 item_index = bin_item_index (bin, item);
959 *item_index_ret = item_index;
960 *category_ret = category;
964 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
965 #define HISTOGRAM_BAR_WIDTH 63
968 print_histogram_bar (double size,
971 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
972 static char const *boxes[8] = {
977 while (size > units_per_cell) {
979 size -= units_per_cell;
982 size /= units_per_cell;
986 else if (size > 6.5/8.0)
988 else if (size > 5.5/8.0)
990 else if (size > 4.5/8.0)
992 else if (size > 3.5/8.0)
994 else if (size > 2.5/8.0)
996 else if (size > 1.5/8.0)
998 else if (size > 0.5/8.0)
1005 mnemon_print_histogram (mnemon_t *mnemon,
1006 const char *category_name,
1009 int i, last_score, max;
1010 category_t *category = NULL;
1013 item_match_predicate_t *predicate = NULL;
1014 void *closure = NULL;
1015 item_in_category_closure_t item_in_category;
1016 item_in_category_of_length_closure_t item_in_category_of_length;
1018 if (mnemon->num_bins == 0)
1021 if (category_name) {
1022 category = mnemon_get_category_if_exists (mnemon, category_name);
1025 predicate = mnemon_item_in_category_of_length;
1026 item_in_category_of_length.mnemon = mnemon;
1027 item_in_category_of_length.category = category;
1028 item_in_category_of_length.length = length;
1029 closure = &item_in_category_of_length;
1031 predicate = mnemon_item_in_category;
1032 item_in_category.mnemon = mnemon;
1033 item_in_category.category = category;
1034 closure = &item_in_category;
1039 for (i = 0; i < mnemon->num_bins; i++) {
1040 num_items = bin_num_items_matching (&mnemon->bins[i],
1041 predicate, closure);
1042 if (i == 0 || num_items > max)
1046 for (i = 0; i < mnemon->num_bins; i++) {
1047 bin = &mnemon->bins[i];
1049 while (bin->score - last_score > 1)
1050 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1051 num_items = bin_num_items_matching (bin,
1052 predicate, closure);
1053 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1054 print_histogram_bar (num_items, max);
1055 last_score = bin->score;
1060 mnemon_handle_command (mnemon_t *mnemon,
1061 const char *command)
1065 switch (command[0]) {
1066 /* 'h' for histogram */
1069 char *category = NULL;
1073 arg += strspn (arg, " \t");
1074 len = strcspn (arg, " \t");
1076 category = xstrndup (arg, len);
1078 arg += strspn (arg, " \t");
1080 length = atoi (arg);
1082 mnemon_print_histogram (mnemon, category, length);
1085 /* 'r' for repeat */
1088 /* Nothing necessary for repeating. */
1092 printf ("Unknown command: %s\n", command);
1098 mnemon_handle_response (mnemon_t *mnemon,
1102 const char *response,
1103 double response_time,
1108 correct = (strcmp (response, item->response) == 0);
1110 bin_remove_item (bin, item_index);
1112 /* If the bin is now empty, we must remove it. Also if we just
1113 * picked the last word we'll ever pick from the bin with
1114 * score 0, then we can remove that as well. */
1115 if (bin->num_items == 0 ||
1116 (bin->score == 0 && mnemon->to_introduce == 0))
1118 mnemon_remove_bin (mnemon, bin);
1122 (time_limit == 0.0 || response_time < time_limit))
1125 /* We reserve an item score of 0 for an item that has
1126 * never been asked. */
1127 if (item->score == 0) {
1129 mnemon->unlearned--;
1130 printf ("You got it!");
1131 } else if (item->score < 0) {
1132 printf ("Yes---just give me %d more.",
1134 } else if (item->score == 1) {
1135 printf ("On your first try, no less!");
1137 printf ("Masterful (%dx).", item->score);
1138 if (mnemon->to_master)
1139 mnemon->to_master--;
1143 printf (" %s is the correct answer.",
1146 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1147 response_time, time_limit);
1148 /* Penalize an incorrect response by forcing the score
1150 if (item->score >= 0) {
1151 if (item->score > 0)
1152 printf (" Oops, you knew that, right? (%dx)\n ",
1154 mnemon->unlearned++;
1155 /* We increase to_master here as an extra penalty. If the
1156 * user is forgetting stuff learned previously, then more
1157 * time should be spent on mastering than learning new
1158 * items. Note that we only do this during the initial
1159 * phase while new items are still being introduced. */
1160 if (mnemon->to_introduce)
1161 mnemon->to_master++;
1162 /* We go to -2 to force a little extra reinforcement
1163 * when re-learning an item, (otherwise, it will often
1164 * get asked again immediately where it is easy to get
1165 * a correct response without any learning). */
1173 if (mnemon->to_introduce)
1174 printf ("%d to come. ", mnemon->to_introduce);
1175 if (mnemon->unlearned)
1176 printf ("%d still unlearned. ", mnemon->unlearned);
1177 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1178 printf ("%d items to master", mnemon->to_master);
1181 bin = mnemon_get_bin (mnemon, item->score);
1183 bin_add_item (bin, item);
1187 mnemon_show_challenge (mnemon_t *mnemon,
1188 challenge_type_t challenge_type,
1189 const char *challenge)
1191 const char *program;
1194 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1195 printf ("%s\n", challenge);
1199 /* XXX: Yes, shelling out to system is total cheese. The planned
1200 * fix here is to bring graphical display in process, (or at least
1201 * have a custom external program that accepts image filenames on
1204 switch (challenge_type) {
1205 case CHALLENGE_TYPE_TEXT:
1208 case CHALLENGE_TYPE_IMAGE:
1209 program = "xli -gamma 2.2";
1211 case CHALLENGE_TYPE_AUDIO:
1214 case CHALLENGE_TYPE_MIDI:
1215 program = "timidity -Os";
1217 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
1218 program = "mnemon-tts";
1222 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1231 mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
1235 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1238 /* XXX: And this is just embarrassing (obviously wrong in several
1239 * ways). Hopefully I'll amend away any commit that includes this.
1241 xasprintf (&command, "killall xli");
1247 mnemon_do_challenges (mnemon_t *mnemon)
1252 category_t *category;
1256 /* Count the number of items with negative scores. */
1257 mnemon->unlearned = 0;
1258 for (i = 0; i < mnemon->num_bins; i++) {
1259 bin = &mnemon->bins[i];
1260 if (bin->score >= 0)
1262 mnemon->unlearned += bin->num_items;
1265 mnemon->to_introduce -= mnemon->unlearned;
1266 if (mnemon->to_introduce < 0)
1267 mnemon->to_introduce = 0;
1269 /* Get rid of bin with score of 0 if we aren't going to be
1270 * introducing anything from it. */
1271 if (mnemon->to_introduce == 0) {
1272 bin = mnemon_get_bin (mnemon, 0);
1273 mnemon_remove_bin (mnemon, bin);
1276 if (mnemon->unlearned) {
1277 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1278 if (mnemon->to_introduce)
1279 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1282 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1287 struct timeval start, end;
1289 mnemon_select_item (mnemon, &bin, &item_index, &category);
1290 item = bin->items[item_index];
1293 if (category->time_limit > 0.0) {
1294 response = readline ("The next one is timed. Press enter when ready:");
1298 mnemon_show_challenge (mnemon, category->challenge_type,
1301 gettimeofday (&start, NULL);
1302 response = readline ("> ");
1303 gettimeofday (&end, NULL);
1305 mnemon_hide_challenge (mnemon, category->challenge_type);
1307 /* Terminate on EOF */
1308 if (response == NULL) {
1313 if (response[0] == '/') {
1314 mnemon_handle_command (mnemon, response + 1);
1321 mnemon_handle_response (mnemon, bin, item_index,
1323 (end.tv_sec + end.tv_usec / 1e6) -
1324 (start.tv_sec + start.tv_usec / 1e6),
1325 category->time_limit);
1328 /* Replay audio challenges for reinforcement. */
1329 if (category->repeat)
1331 mnemon_show_challenge (mnemon, category->challenge_type,
1333 printf ("%s\n", item->challenge);
1336 } while (mnemon->to_introduce ||
1337 mnemon->unlearned ||
1338 mnemon->to_master > 0);
1342 main (int argc, char *argv[])
1347 srand (time (NULL));
1349 mnemon_init (&mnemon);
1351 mnemon_load (&mnemon);
1353 mnemon_do_challenges (&mnemon);
1355 mnemon_save (&mnemon);
1357 mnemon_fini (&mnemon);
1359 mnemon_init (&mnemon);
1360 mnemon_load (&mnemon);
1362 printf ("Great job.\nHere are your current results:\n");
1363 mnemon_print_histogram (&mnemon, NULL, 0);
1364 response = readline ("Press enter to quit.\n");
1367 mnemon_fini (&mnemon);