home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / mir / a_occur2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-02  |  9.1 KB  |  276 lines

  1. /*
  2.  *  usage - a_occur2  [ min_frequency [ filename_under_min ] ]
  3.  *              < merged a_occur files > combined
  4.  *
  5.  * A_OCCUR2 A utility to calculate cumulative frequency of
  6.  *          merged A_OCCUR outputs.  If a minimum frequency is
  7.  *          specified, then all lower frequency items are either
  8.  *          suppressed or sent to a file named in the next argument.
  9.  *
  10.  * Input:   ASCII text, in which each line starts with a number
  11.  *          (a frequency count) followed by blanks, then sorted text
  12.  *          starting in the seventh column.
  13.  *
  14.  * Output:  A copy of the same file in which multiple identical lines
  15.  *          are shown only once, preceded by the combined frequency
  16.  *          count.
  17.  *
  18.  * Writeup: MIR TUTORIAL ONE, topic five.
  19.  *
  20.  *  Written:    Douglas Lowry   Oct 28 87
  21.  *  Modified:   Douglas Lowry   Apr 30 92  Reworked entirely
  22.  *              Copyright (C) 1992 Innotech Inc.
  23.  *
  24.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  25.  *    usage and co-ordination of the MIR family of programs to analyze,
  26.  *    prepare and index databases (small through gigabyte size), and
  27.  *    how to build integrated retrieval software around the MIR search
  28.  *    engine.  The fifth of the five MIR tutorial series explains how
  29.  *    to extend indexing capability into leading edge search-related
  30.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  31.  *    MIR files are in the DBMS library.  The same files are on the
  32.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  33.  *    is available by mail ($10 US... check, Visa or Mastercard);
  34.  *    diskettes with Introduction, Tutorial ONE software and the
  35.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  36.  *    for a tutorial is also $29.
  37.  *
  38.  *    E-mail...
  39.  *                Compuserve  71431,1337
  40.  *                Internet    doug.lowry%canrem.com
  41.  *                UUCP        canrem!doug.lowry
  42.  *                Others:     doug.lowry@canrem.uucp
  43.  *
  44.  *    FAX...                  416 963-5677
  45.  *
  46.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  47.  *                            Marpex Inc.
  48.  *                            5334 Yonge Street, #1102
  49.  *                            North York, Ontario
  50.  *                            Canada  M2N 6M2
  51.  *
  52.  *    Related database consultation and preparation services are
  53.  *    available through:
  54.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  55.  *              North York, Ontario  Canada   M2J 4Z7
  56.  *              Tel.  416 492-3838   FAX  416 492-3843
  57.  *
  58.  *  This program is free software; you may redistribute it and/or
  59.  *  modify it under the terms of the GNU General Public License as
  60.  *  published by the Free Software Foundation; either version 2 of
  61.  *  the License, or (at your option) any later version.
  62.  *
  63.  *  This program is distributed in the hope that it will be useful,
  64.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  65.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  66.  *  GNU General Public License for more details.
  67.  *
  68.  *  You should have received a copy of the GNU General Public License
  69.  *  (file 05LICENS) along with this program; if not, write to the
  70.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  71.  *  USA.
  72.  */
  73.  
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76.  
  77. #define     MAX_BYTES   512
  78. #define     repeat      for(;;)
  79.  
  80. /*
  81.  * declarations 
  82.  */
  83.  
  84. typedef     enum        _bool
  85.              { FALSE = 0, TRUE = 1 }  Bool;
  86.  
  87.     void        Usage_(), process();
  88.     char        *Cmdname_() {    return( "a_occur2" );  }
  89.  
  90. /*
  91.  * MAIN
  92.  */
  93.  
  94. main( argc, argv )
  95.     int  argc;
  96.     char **argv;
  97. {
  98.     FILE    *fp_out ;       /*  for under minimum listing   */
  99.     char    c10 ;
  100.     int     min_occur ;
  101.  
  102.     min_occur = 0;
  103.     fp_out = NULL;
  104.  
  105.     if( argc > 1 )
  106.     {
  107.         c10 = argv[1][0] ;
  108.         if( argc > 3 || c10 == '-'|| c10 == '/' || c10 == '?' )
  109.             Usage_();
  110.  
  111.         if(( min_occur = atoi( argv[1] )) < 1 )
  112.         {
  113.             fprintf( stderr,"First argument expects a number > 0.\n\n" );
  114.             Usage_();
  115.         }
  116.     }
  117.  
  118.     if( argc > 2 )
  119.     {
  120.         if(( fp_out = fopen( argv[2], "w" )) == NULL )
  121.         {
  122.             fprintf( stderr, "\nUnable to open file %s.\n",
  123.                 argv[2] ) ;
  124.             Usage_() ;
  125.         }
  126.     }
  127.  
  128.     process( min_occur, fp_out ) ;
  129.  
  130.     if( fp_out != NULL )
  131.     {
  132.         if( fclose( fp_out ))
  133.             fprintf( stderr, "Trouble closing file %s\n", argv[2] );
  134.     }
  135.     exit( 0 ) ;
  136. }
  137. /*
  138.  *  Usage_
  139.  */
  140.     void
  141. Usage_()
  142. {
  143.     fprintf( stderr,
  144.         "\nusage: %s [ min_frequency [ filename_under_min ] ]\n\
  145.             < merged a_occur files > combined\n\n\
  146.         A utility to calculate cumulative frequency of\n\
  147.         merged A_OCCUR outputs.  If a minimum frequency is\n",
  148.             Cmdname_() ) ;
  149.     fprintf( stderr,
  150. "        specified, then all lower frequency items are either\n\
  151.         suppressed or sent to a file named in the next argument.\n\n\
  152. Input:  ASCII text, in which each line starts with a number\n\
  153.         (a frequency count) followed by blanks, then sorted text\n\
  154.         starting in the seventh column.\n\n" ) ;
  155.     fprintf( stderr,
  156. "Output: A copy of the same file in which multiple identical lines\n\
  157.         are shown only once, preceded by the combined frequency\n\
  158.         count.\n\n\
  159. Writeup: MIR TUTORIAL ONE, topic five.\n\n" ) ;
  160.     exit( 1 ) ;
  161. }
  162. /*
  163.  *  PROCESS
  164.  */
  165.     void
  166. process( min_occur, fp_out )
  167.     FILE    *fp_out ;       /*  for under minimum listing   */
  168.     int     min_occur ;
  169. {
  170.     char    buf[2][MAX_BYTES];  /* alternating line inputs      */
  171.     Bool    done,       /* last line has been read      */
  172.             same;       /* 2 successive lines identical */
  173.     long int
  174.             freq[2],    /* count of occurences of line  */
  175.             sizer ;
  176.     int     this,       /* current buffer is 0 or 1     */
  177.             that,       /* other buffer is 1 or 0       */
  178.             lines_in,   /* count                        */
  179.             len[2],     /* line length of each buffer   */
  180.             i;
  181.  
  182.     len[0] = len[1] = freq[0] = freq[1] = lines_in = 0;
  183.     done = FALSE;
  184.     this = 0;
  185.     that = 1;
  186.  
  187.     while( !done )
  188.     {
  189.         if( fgets( buf[this], MAX_BYTES, stdin ) == NULL )
  190.             done = TRUE;
  191.         freq[this] = atol( buf[this] ) ;
  192.         if( !freq[ this ] )
  193.         {
  194.             fprintf( stderr, "No frequency beginning line %d...\n%s\n",
  195.                 lines_in, buf[ this ] );
  196.             exit( 1 ) ;
  197.         }
  198.         lines_in++ ;
  199.         len[this] = strlen( buf[this] ) - 1 ;
  200.         while( isspace( buf[this][len[this]-1] ))
  201.             len[this] -= 1 ;
  202.         buf[this][len[this]] = '\0';    /* replace linefeed */
  203.         if( done || len[this] < 0 )
  204.             len[this] = 0;
  205.  
  206.         same = FALSE;       /* compare 2 consecutive lines  */
  207.         if( len[this] == len[that] )
  208.         {
  209.             same = TRUE;
  210.             for( i = 6; i < len[0]; i++ )
  211.             {
  212.                 if( buf[0][i] != buf[1][i] )
  213.                 {
  214.                     same = FALSE;
  215.                     if( buf[this][i] < buf[that][i] )
  216.                     {
  217.                         fprintf( stderr,
  218.                             "Not sorted... lines %d and %d\n%s\n%s\n",
  219.                             lines_in - 1, lines_in, buf[this], buf[that] );
  220.                         Usage_() ;
  221.                     }
  222.                     break;
  223.                 }
  224.             }
  225.         }
  226.  
  227.         if( same )
  228.             freq[ that ] += freq[ this ];
  229.         else            /* if not same, print       */
  230.         {
  231.             if( freq[ that ] )
  232.             {
  233.                 if( freq [ that ] >= min_occur )
  234.                 {
  235.                     printf( "%ld", freq[ that ] ) ;
  236.                     sizer = freq[ that ] ;
  237.                     while( sizer < 100000 )
  238.                     {
  239.                         putchar( ' ' ) ;
  240.                         sizer *= 10 ;
  241.                     }
  242.                     if( !printf( "%s\n", &buf[that][6] ))
  243.                     {
  244.                         fprintf( stderr, "Writing failure after %d lines\n",
  245.                             lines_in );
  246.                         exit( 1 ) ;
  247.                     }
  248.                 }
  249.                 else if( min_occur && fp_out != NULL )
  250.                 {
  251.                     fprintf( fp_out, "%ld", freq[ that ] ) ;
  252.                     sizer = freq[ that ] ;
  253.                     while( sizer < 100000 )
  254.                     {
  255.                         fputc( ' ', fp_out ) ;
  256.                         sizer *= 10 ;
  257.                     }
  258.                     if( !fprintf( fp_out, "%s\n", &buf[that][6] ))
  259.                     {
  260.                         fprintf( stderr, "Writing failure after %d lines\n",
  261.                             lines_in );
  262.                         exit( 1 ) ;
  263.                     }
  264.                 }
  265.             }
  266.             this = that;
  267.             if( this )
  268.                 that = 0;
  269.             else
  270.                 that = 1;
  271.         }
  272.     }
  273.  
  274.     return ;
  275. }
  276.