/* ** PROGRAM NAME: WORDCNT.CPL ** ------------- ** Program Source Code File (.CPL) ** Concordance(tm) Information Retrieval System, Professional Edition ** ** COPYRIGHT: ** ---------- ** (C) Copyright Dataflight Software, Inc. 1992. All Rights Reserved. ** ** SYNOPSIS: Counts total occurrence of all words in database. ** --------- ** */ /* Uniform width of document number stored in temporary btree file. */ short WIDTH = 10; /* Error codes. */ short DISKFULLERROR = 1; short REINDEXERROR = 2; short USEREXITERROR = 3; short NOHITSERROR = 4; short OPENDBERROR = 5; short TEMPFILEERROR = 6; short IVTFILEERROR = 7; short DCTFILEERROR = 8; short QUERYZEROERR = 9; short ZERODOCERROR = 10; main() { int db; switch(talley(db)) { case DISKFULLERROR: Message("Disk full encountered while writing temporary file."); break; case USEREXITERROR: break; case OPENDBERROR : Message("Please open a database before running word count."); break; case TEMPFILEERROR: Message("Couldn't create temporary .LST file. "); break; case DCTFILEERROR : Message("Couldn't open database dictionary."); break; case IVTFILEERROR : Message("Couldn't open database .IVT file."); break; case ZERODOCERROR : Message("This search located zero documents, nothing to do."); break; case 0: Message("This database dictionary has been tallied."); break; } } /* main() */ /**************************************************************** * Name: talley * * Synopsis: Talley total number of occurrences in database. * ****************************************************************/ talley(int db) { int error, /* Error control variable. */ i, /* Misc loop variable. */ offset, /* Offset into field of word. */ invtHandle, /* Handle to opened .IVT file. */ treeHandle, /* Handle to opened .DCT file. */ wordNumber, /* Status variable for progress display. */ blocks; /* Number of blocks in progress display. */ char word[33], /* Word to weight, at "offset" in "field." */ treeName[128]; /* Name of temporary weightedTreeHandle. */ float occurrences, /* Total occurrences of all words. */ percent, /* Progress percentage through database. */ totalWords; /* Total count of word in dictionary. */ text screen; /* Saved screen, restored on exit. */ int DOCUMENTOFFSET, WORDOFFSET = 4; cursoroff(); if (db.documents < 0) if (getfile("Open Database","*.DCB",treeName) == CR) db = opendb(treeName); /* Do some initial error checking to ensure that a database */ /* is open, that documents were located in a query, and that */ /* a search was actually performed. */ if (db.documents < 0) error = OPENDBERROR; else { if (count(db) <= 0) error = ZERODOCERROR; else { /* Open the inverted text file for processing. */ if ((invtHandle = open(db.database+".IVT","r")) == EOF) error = IVTFILEERROR; else { /* Open the database dictionary for processing */ if ((treeHandle = btopen(db.database+".DCT")) == EOF) { close(invtHandle); error = DCTFILEERROR; } } } } if (error == FALSE) { /* Initialize the screen for status display */ /* while we are processing the data base. */ screen = save(8,19,17,61); blocks = 37; scroll(8,19,14,59,0,0,MenuColor_); box(8,19,16,61,"DS",MenuColor_); puts(09,24,"ÄÄ Database Occurrence Tally ÄÄ",MenuColor_); puts(10,21,"Processing word",MenuColor_); puts(11,21,"Occurrence count",MenuColor_); puts(12,21,"Dictionary processed",MenuColor_); puts(14,21,rep('°',blocks),MenuColor_); puts(10,57,"1",MenuColor_); puts(12,57,"0%",MenuColor_); /* Cycle through each word retrieving hit count. */ totalWords = btcount(treeHandle); for(i = btfirst(treeHandle,word,offset); i == 0; i = btnext(treeHandle,word,offset)) { seek(invtHandle,offset+WORDOFFSET,'B'); read(invtHandle,i,sizeof(i)); occurrences = occurrences + i; /* Update the status display. */ wordNumber = wordNumber + 1; percent = wordNumber/totalWords; puts(10,36,pad(word,'R',22),MenuColor_); puts(11,48,str(occurrences,10,0,','),MenuColor_); puts(12,48,str(percent*100.0,10),MenuColor_); puts(14,21,rep('²',blocks*percent),MenuColor_); /* See if the user wants to exit the processing loop early. */ if (keypress()) { if (getkey() == ESC) if (Message("Cancel dictionary talley? Y/N") == 'Y') { error = USEREXITERROR; break; } } } /* Close the .IVT and .DCT files, we are finished with them. */ close(invtHandle); btclose(treeHandle); /* Restore the user's screen. */ if (error == FALSE) getkey(); restore(8,19,screen); } return(error); } /* talley() */ /**************************************************************** * Name: Message * * Synopsis: Displays error message and waits for key. * ****************************************************************/ Message(text message) { text screen; int key; cursoroff(); screen = save(10,13,13,69); box(10,13,13,69,"DS", MenuColor_); puts(11,14,pad(message,'C',53),MenuColor_); if ((key = getkey()) & 255) key = asc(upper(chr(key))); restore(10,13,screen); return(key); } /* Message() */ /**************************************************************** * Name: Path * * Synopsis: Returns the path up to the last \ and without * * the file's name. * ****************************************************************/ Path(text dosPath) { int i, j; if ((i = match(dosPath,":",1)) == 0) i = match(dosPath,"\",1); while(j = match(dosPath,"\",i+1)) i = j; return(substr(dosPath,1,i)); } /* Path() */ /**************************************************************** * Global Variable Declarations and Initialization * ****************************************************************/ int CTRLPGUP = 33792; short LEFT = 19200, RIGHT = 19712, UP = 18432, DOWN = 20480, HOME = 18176, END = 20224, PGUP = 18688, PGDN = 20736, CTRLPGDN = 30208, F2 = 15360, F3 = 15616, F4 = 15872, F5 = 16128, F6 = 16384, F7 = 16640, F8 = 16896, F9 = 17152, F10 = 17408; char ESC = 27, CTRLP = 16, EOF = -1, FALSE = 0, TRUE = 1, CR = 13, LF = 10;