2003-08-07 15:13:06 +04:00
|
|
|
/* $NetBSD: locate.bigram.c,v 1.10 2003/08/07 11:14:20 agc Exp $ */
|
1994-12-22 09:17:28 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-12-22 09:17:28 +03:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* James A. Woods.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 08:11:51 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-19 08:11:51 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-22 09:17:28 +03:00
|
|
|
#if 0
|
1995-09-01 02:36:32 +04:00
|
|
|
static char sccsid[] = "@(#)locate.bigram.c 8.2 (Berkeley) 4/28/95";
|
1994-12-22 09:17:28 +03:00
|
|
|
#endif
|
2003-08-07 15:13:06 +04:00
|
|
|
__RCSID("$NetBSD: locate.bigram.c,v 1.10 2003/08/07 11:14:20 agc Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bigram < text > bigrams
|
|
|
|
*
|
|
|
|
* List bigrams for 'updatedb' script.
|
|
|
|
* Use 'code' to encode a file using this output.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2000-03-20 22:17:35 +03:00
|
|
|
#include <stdlib.h>
|
2000-03-23 00:45:02 +03:00
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/param.h> /* for MAXPATHLEN */
|
|
|
|
|
1997-10-19 08:11:51 +04:00
|
|
|
int main __P((int, char **));
|
2000-03-20 22:17:35 +03:00
|
|
|
static int compare_bigrams __P((const void *, const void *));
|
|
|
|
static void add_bigram __P((u_char, u_char));
|
|
|
|
|
|
|
|
static char buf1[MAXPATHLEN] = " ";
|
|
|
|
static char buf2[MAXPATHLEN];
|
|
|
|
|
|
|
|
struct bigram {
|
|
|
|
int count;
|
|
|
|
u_char b1, b2; /* needed for final sorting */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bigram bigrams[256 * 256];
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_bigram(i1, i2)
|
|
|
|
u_char i1, i2;
|
|
|
|
{
|
2000-05-06 14:26:45 +04:00
|
|
|
if (i1 != '\n' && i2 != '\n')
|
|
|
|
bigrams[(i1<<8)+i2].count++;
|
2000-03-20 22:17:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare_bigrams(item1, item2)
|
|
|
|
const void *item1, *item2;
|
|
|
|
{
|
|
|
|
const struct bigram *it1=item1, *it2=item2;
|
|
|
|
|
|
|
|
if (it1->count != it2->count)
|
|
|
|
return it2->count - it1->count;
|
|
|
|
else if (it1->b1 != it2->b1)
|
|
|
|
return it2->b1 - it1->b1;
|
|
|
|
else
|
|
|
|
return it2->b2 - it2->b2;
|
|
|
|
}
|
1997-10-19 08:11:51 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 08:11:51 +04:00
|
|
|
char *cp;
|
|
|
|
char *oldpath = buf1, *path = buf2;
|
2000-03-20 22:17:35 +03:00
|
|
|
struct bigram *bg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* initialize bigram array */
|
|
|
|
memset(bigrams, 0, sizeof(bigrams));
|
|
|
|
for(i=0; i < 65536; i++) {
|
|
|
|
bigrams[i].b1 = i / 256;
|
|
|
|
bigrams[i].b2 = i % 256;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) {
|
|
|
|
|
|
|
|
/* skip longest common prefix */
|
|
|
|
for ( cp = path; *cp == *oldpath; cp++, oldpath++ )
|
1995-09-01 02:36:32 +04:00
|
|
|
if ( *oldpath == '\0' )
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2000-03-20 22:17:35 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* output post-residue bigrams only
|
|
|
|
*/
|
2000-03-20 22:17:35 +03:00
|
|
|
for(; cp[0] != '\0' && cp[1] != '\0'; cp += 2)
|
|
|
|
add_bigram((u_char)cp[0], (u_char)cp[1]);
|
|
|
|
|
|
|
|
if (path == buf1) /* swap pointers */
|
1993-03-21 12:45:37 +03:00
|
|
|
path = buf2, oldpath = buf1;
|
|
|
|
else
|
|
|
|
path = buf1, oldpath = buf2;
|
|
|
|
}
|
2000-03-20 22:17:35 +03:00
|
|
|
|
|
|
|
/* sort the bigrams by how many times it appeared and their value */
|
|
|
|
heapsort((void *)bigrams, 256 * 256, sizeof(struct bigram),
|
|
|
|
compare_bigrams);
|
|
|
|
|
|
|
|
/* write 128 most frequent bigrams out */
|
|
|
|
bg = bigrams;
|
|
|
|
for (i = 0; i < 128 && bg->count > 0; i++, bg++) {
|
|
|
|
if (bg->b1 != '\0')
|
|
|
|
fputc(bg->b1, stdout);
|
|
|
|
if (bg->b2 != '\0')
|
|
|
|
fputc(bg->b2, stdout);
|
|
|
|
}
|
|
|
|
|
1995-09-01 02:36:32 +04:00
|
|
|
return (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|