/********************************************************************** A Simple Supervised Learning Utility ver 1.0 Written by Kazuyuki HANAAHARA (hanahara@cs.kobe-u.ac.jp) See sample file "qa.lst" **********************************************************************/ #include #include /* * Size parameters. * Conventional DOS machine is currently assumed.(But that'll be enough) */ #define BUFFSIZE 30000 /* Total buffer size */ #define MAX_ITEM 1000 /* Maximum number of items */ #define MAX_LENGTH 400 /* Maximum line length */ char buff[BUFFSIZE], /* Buffer area for items */ *item[MAX_ITEM][2]; /* Pointers to each Q & A */ int Nitem; /* Actual number of items */ int level[MAX_ITEM]; /* A kind of difficulty of each item */ /*-------------------------------------------------------------------- Read items from specified file pointer. Args. fp File to be read. Return Number of items in the file. */ int ReadItem( fp ) FILE *fp; { int n, i, l; char *p; for( n = l = 0, item[0][0] = buff; !feof( fp ) && n < MAX_ITEM && item[n][0] < buff + BUFFSIZE - MAX_LENGTH * 2; ){ for( i = 0; i < 2 && !feof( fp ); ){ /* * Get next line. */ if( fgets((p = item[n][i]), MAX_LENGTH, fp) == NULL ) continue; else l++; /* * Format check. */ if( *p != 'q' && *p != 'Q' && *p != 'a' && *p != 'A' ) continue; /* Maybe a comment line */ if( i == 0 && (*p != 'q' && *p != 'Q') || i == 1 && (*p != 'a' && *p != 'A') ){ printf( "** Line %d: format error **\n>>%s\n", l, item[n][i] ); exit( 0 ); } /* * Set next pointer. */ while( *p != NULL && *p != '\n' ) p++; *(p++) = NULL; if( i == 0 ) item[n][1] = p; else if( n < MAX_ITEM - 1 ) item[n+1][0] = p; /* * Next line... */ i++; } if( i == 2 ) n++; /* Read next tiem... */ } if( !feof( fp ) ) printf( "*** Warning: There are more items in the file.\n" ); Nitem = n; return( n ); } /*-------------------------------------------------------------------- Try all items one time. Args. left Number of left items. try nth try. (, 2, 3...) Return Number of items not cleared. */ int TryItems( left, try ) int left, try; { int n, i, j; char s[100]; printf( "***** %d items to be examined.\n", left ); for( n = left; n > 0; n-- ){ /* * Randomly select next item. */ i = rand() % n; for( j = -1; i >= 0; i-- ){ j++; while( level[j] >= try ) j++; /* Examined(=try) or Cleared(=10000) */ } /* * Display selected item for examination. */ printf( "%d:%s\n---[CR] ", j + 1, item[j][0] ); fflush( stdin ); getchar(); printf( "%d:%s\n---[OK?(y/n)] ", j + 1, item[j][1] ); fflush( stdin ); fgets( s, 100, stdin ); if( *s == 'y' || *s == 'Y' ){ /*--- Judged as cleared ---*/ level[j] = 10000; left--; } else{ /*--- Still not cleared ---*/ (level[j])++; } } return( left ); } /*-------------------------------------------------------------------- Perform supervised learning */ void SupervisedLearn() { int n, m, i; /* * Reset level values. */ for( n = 0; n < Nitem; n++ ) level[n] = 0; /* * Try all items until they all are cleared. */ for( i = 1; n > 0; i++ ){ m = TryItems( n, i ); /* Display result. */ printf( "\n***** Try %d:%5.1lf%%(%d/%d)\n\n", i, (double)(n-m)/(double)n * 100.0, n - m, n ); n = m; } } int main( argc, argv ) int argc; char *argv[]; { FILE *fp; int n, i; if( argc == 1 ){ printf( "sl [Q/A list file: see 'qa.lst']\n" ); exit( 0 ); } if( (fp = fopen( argv[1], "r" )) == NULL ){ printf( "File `%s' not found.\n", argv[1] ); exit( 0 ); } ReadItem( fp ); printf( "[[[ There are %d items in `%s']]]\n", Nitem, argv[1] ); fclose( fp ); srand( time( NULL ) ); SupervisedLearn(); }