Article View: alt.privacy.anon-server
Article #46805Re: Free reliable wordcount utility available (final version)
From: Richard Heathfield
Date: Sat, 09 Nov 2002 16:09
Date: Sat, 09 Nov 2002 16:09
228 lines
6781 bytes
6781 bytes
"Thomas J. Boschloo" wrote:
>
> Richard Heathfield wrote:
> >
> > "Thomas J. Boschloo" wrote:
>
> > > I just ran that version of my wordcnt utility on my 100mb
> > > swapfile from MS-Dos. If finished well within a full hour :-)
> > > That is a lot less then I expected at first. I ran out of
> > > memory though.. 300000 words counted I believe, lot's of
> > > MICROSOFT strings in my swapfile ;-P
> >
> > You'll be glad to know that you should be able to make it a lot
> > faster. I just spent about half an hour writing a C++ version
> > (C++ is considered by many, including me, to be slower than C),
> > and it runs at a little over twice the speed of yours (on the
> > same hardware, of course).
> >
> > If you want the source code, let me know.
>
> Yes I do.
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <map>
#include <string>
using namespace std;
#define DEFAULT_MINWORDLEN 4
#define DEFAULT_MINCOUNT 4
int GetWord(string &word, FILE *fp)
{
int result = 0;
int ch;
word = "";
/* skip leading spaces */
do
{
ch = fgetc(fp);
} while(ch != EOF && isspace((unsigned char)ch));
if(ch != EOF)
{
do
{
word += ch;
ch = fgetc(fp);
} while(ch != EOF &&
(isalpha((unsigned char)ch) ||
'\'' == ch ||
'-' == ch));
}
if(EOF == ch)
{
result = 1;
}
return result;
}
int GetWordFrequencies(map <string, unsigned long> &freqmap,
unsigned long minword,
FILE *fp)
{
int status = 0;
int result = 0;
string word;
word.resize(128);
do
{
result = GetWord(word, fp);
if(result == 0)
{
if(word.size() > minword)
{
++freqmap[word];
}
}
} while(result == 0);
if(result < 0)
{
status = 1;
}
return status;
}
int PrintWordFrequencies(map <string, unsigned long> &freqmap,
unsigned long mincount)
{
int status = 0;
size_t count = freqmap.size();
map <string, unsigned long> :: const_iterator it = freqmap.begin();
while(it != freqmap.end())
{
if((*it).second >= mincount)
{
printf("%8lu %s\n", (*it).second, (*it).first.c_str());
}
else
{
--count;
}
++it;
}
printf("Unique words of the necessary"
" length and frequency: %lu\n",
(unsigned long)count);
return status;
}
int main(int argc, char **argv)
{
int status = EXIT_SUCCESS;
clock_t end = {0};
clock_t start = clock();
map <string, unsigned long> freqmap;
if(argc > 1)
{
FILE *fp = NULL;
char *filename = argv[1];
char *endp = NULL;
size_t minwordlen = DEFAULT_MINWORDLEN;
size_t mincount = DEFAULT_MINCOUNT;
if(argc > 2)
{
minwordlen = (size_t)strtoul(argv[2], &endp, 10);
if(endp == argv[2])
{
minwordlen = DEFAULT_MINWORDLEN;
}
if(argc > 3)
{
mincount = (size_t)strtoul(argv[3], &endp, 10);
if(endp == argv[3])
{
mincount = DEFAULT_MINCOUNT;
}
}
}
fp = fopen(filename, "rb");
if(fp != NULL)
{
if(0 != GetWordFrequencies(freqmap, minwordlen, fp))
{
fprintf(stderr,
"Error in GetWordFrequencies().\n");
status = EXIT_FAILURE;
}
fclose(fp);
if(EXIT_SUCCESS == status)
{
if(0 != PrintWordFrequencies(freqmap, mincount))
{
fprintf(stderr,
"Error in PrintWordFrequencies().\n");
}
}
}
else
{
fprintf(stderr,
"Can't open input file.\n");
}
}
else
{
fprintf(stderr,
"Required parameter: filename.\n"
" Optional: minimum word length\n"
" and minimum count.\n");
status = EXIT_FAILURE;
}
end = clock();
printf("Time: %.3f\n", (end - start) / (double)CLOCKS_PER_SEC);
return status;
}
> It would give me something to flame you back on.
I'm not interested in flames. If you have any constructive and
well-informed criticism, I'd be glad to hear it.
> Please post
> to this group if it is not too big
It's rather smaller than the program you posted.
> (would sci.crypt do for you?). Note
> that I don't have a C++ compiler on any of my computers. I just don't
> like the language, even if it is object oriented.
I prefer C too, but I didn't want to spend too much time on it.
> (BTW, how did you figure out how to compile it? Do you have Turbo C like
> I have <grin>)
Yes, I have quite a selection of compilers, including Turbo C. If you
want a free C++ compiler, you can get one easily nowadays. Borland, for
example, provide a command-line compiler for free.
--
Richard Heathfield : binary@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Technical Details
Message-ID:
<3DCD3356.EBE0DD11@eton.powernet.co.uk>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.security.pgp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fr.usenet-edu.net!usenet-edu.net!teaser.fr!easynet-quince!easynet-monga!easynet.net!news2.power.net.uk!news.power.net.ukReferences:
<3DC1596C.6D2B55BD@hccnet.nl.invalid> <3DC526FC.CBCFE9B6@hccnet.nl.invalid> <3DC5451F.DBF3CDDA@eton.powernet.co.uk> <pan.2002.11.03.17.05.02.482162@yahoo.co.uk> <3DC81975.45915377@hccnet.nl.invalid> <3DC85EA2.2C8E26DA@earthlink.net> <3DC95533.512AB0A3@hccnet.nl.invalid> <3DC976DA.32815A18@earthlink.net> <3DCBC0A8.F3785708@hccnet.nl.invalid> <3DCC0E5A.4C70F9F@eton.powernet.co.uk> <3DCD229B.1F302611@hccnet.nl.invalid>Show Full Headers
Complete Article Headers
Message-ID: <3DCD3356.EBE0DD11@eton.powernet.co.uk> Subject: Re: Free reliable wordcount utility available (final version) Date: Sat, 09 Nov 2002 16:09:58 +0000 From: Richard Heathfield <binary@eton.powernet.co.uk> References: <3DC1596C.6D2B55BD@hccnet.nl.invalid> <3DC526FC.CBCFE9B6@hccnet.nl.invalid> <3DC5451F.DBF3CDDA@eton.powernet.co.uk> <pan.2002.11.03.17.05.02.482162@yahoo.co.uk> <3DC81975.45915377@hccnet.nl.invalid> <3DC85EA2.2C8E26DA@earthlink.net> <3DC95533.512AB0A3@hccnet.nl.invalid> <3DC976DA.32815A18@earthlink.net> <3DCBC0A8.F3785708@hccnet.nl.invalid> <3DCC0E5A.4C70F9F@eton.powernet.co.uk> <3DCD229B.1F302611@hccnet.nl.invalid> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.security.pgp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fr.usenet-edu.net!usenet-edu.net!teaser.fr!easynet-quince!easynet-monga!easynet.net!news2.power.net.uk!news.power.net.uk Organization: Eton Computer Systems Ltd X-Mailer: Mozilla 4.6 [en-gb]C-CCK-MCD NetscapeOnline.co.uk (WinNT; I) X-Accept-Language: en-GB,en MIME-Version: 1.0 Newsgroups: alt.privacy.anon-server,sci.crypt,alt.fan.cult-dead-cow,alt.security.pgp Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 195.60.5.108 X-Trace: news.power.net.uk 1036862098 195.60.5.108 (9 Nov 2002 17:14:58 GMT) Lines: 229