[3365] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * mgpp_invf_dump.cpp -- Program to dump uot an inverted fil
|
---|
| 4 | * Copyright (C) 1994 Neil Sharman
|
---|
| 5 | *
|
---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This program is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | * GNU General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU General Public License
|
---|
| 17 | * along with this program; if not, write to the Free Software
|
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | *
|
---|
| 20 | **************************************************************************/
|
---|
| 21 |
|
---|
| 22 | #define _XOPEN_SOURCE 1
|
---|
[19822] | 23 | // This was added for Solaris, but it makes things worse on Solaris for me...
|
---|
| 24 | // #define _XOPEN_SOURCE_EXTENDED 1
|
---|
[3365] | 25 |
|
---|
| 26 | // need this to avoid bizarre compiler problems under VC++ 6.0
|
---|
| 27 | #if defined (__WIN32__) && !defined (GSDL_USE_IOS_H)
|
---|
| 28 | # include <iostream>
|
---|
| 29 | #endif
|
---|
| 30 |
|
---|
| 31 | /* getopt is in posix.2, so cygwin should have it in unistd, but doesn't */
|
---|
| 32 | #if defined (__WIN32__) || defined (__CYGWIN__)
|
---|
| 33 | # include "getopt_old.h"
|
---|
| 34 | #else
|
---|
| 35 | # include <unistd.h>
|
---|
| 36 | #endif
|
---|
| 37 |
|
---|
| 38 | #include "sysfuncs.h"
|
---|
| 39 | #include "messages.h"
|
---|
| 40 | #include "bitio_m_stdio.h"
|
---|
| 41 | #include "bitio_gen.h"
|
---|
| 42 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
| 43 |
|
---|
| 44 | #include "mg_files.h"
|
---|
| 45 | #include "locallib.h"
|
---|
| 46 | #include "words.h"
|
---|
| 47 | #include "invf.h"
|
---|
| 48 | #include "WordData.h"
|
---|
| 49 |
|
---|
| 50 | static void PrintInvfWord (FILE *invfFile,
|
---|
| 51 | invf_dict_header &idh,
|
---|
| 52 | invf_file_header &ifh,
|
---|
| 53 | word_dict_el &wordEl,
|
---|
[25147] | 54 | mg_u_long wordStart,
|
---|
[3365] | 55 | bool printFrags) {
|
---|
| 56 | cout << wordEl.frag_occur << " \"" << wordEl.el << "\"\n";
|
---|
| 57 |
|
---|
| 58 | if (printFrags) {
|
---|
| 59 | // seek to the appropriate place in the inverted file
|
---|
| 60 | fseek (invfFile, wordStart, SEEK_SET);
|
---|
| 61 |
|
---|
| 62 | stdio_bitio_buffer buffer(invfFile);
|
---|
| 63 |
|
---|
[25147] | 64 | mg_u_long B = BIO_Bblock_Init (idh.num_frags, wordEl.frag_occur);
|
---|
| 65 | mg_u_long fragNum = 0;
|
---|
| 66 | mg_u_long i;
|
---|
[9613] | 67 | for (i=0; i<wordEl.frag_occur; ++i) {
|
---|
[25147] | 68 | mg_u_long delta = buffer.bblock_decode (B, NULL);
|
---|
[3365] | 69 | fragNum += delta;
|
---|
| 70 | cout << " " << fragNum;
|
---|
| 71 |
|
---|
| 72 | if (!ifh.word_level_index ) {
|
---|
[25147] | 73 | mg_u_long count = buffer.gamma_decode (NULL);
|
---|
[3365] | 74 | cout << "(" << count << ")";
|
---|
| 75 | } else {
|
---|
| 76 | cout << "(1)";
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | cout << "\n";
|
---|
| 81 |
|
---|
| 82 | buffer.done();
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static void PrintInvfTag (FILE *invfFile,
|
---|
| 87 | invf_dict_header &idh,
|
---|
| 88 | invf_file_header &/*ifh*/,
|
---|
| 89 | dict_el &tagEl,
|
---|
[25147] | 90 | mg_u_long tagStart,
|
---|
[3365] | 91 | bool printFrags) {
|
---|
| 92 | cout << tagEl.frag_occur << " \"<" << tagEl.el << ">\"\n";
|
---|
| 93 |
|
---|
| 94 | if (printFrags) {
|
---|
| 95 | // seek to the appropriate place in the inverted file
|
---|
| 96 | fseek (invfFile, tagStart, SEEK_SET);
|
---|
| 97 |
|
---|
| 98 | stdio_bitio_buffer buffer(invfFile);
|
---|
| 99 |
|
---|
[25147] | 100 | mg_u_long pTag = tagEl.frag_occur*2;
|
---|
| 101 | mg_u_long B = BIO_Bblock_Init (idh.num_frags+pTag, pTag);
|
---|
| 102 | mg_u_long fragNum = 0;
|
---|
| 103 | mg_u_long i;
|
---|
[9613] | 104 | for (i=0; i<tagEl.frag_occur; ++i) {
|
---|
[25147] | 105 | mg_u_long delta = buffer.bblock_decode (B, NULL)-1;
|
---|
[3365] | 106 | fragNum += delta;
|
---|
| 107 | cout << " " << fragNum;
|
---|
| 108 | cout << "-";
|
---|
| 109 | delta = buffer.bblock_decode (B, NULL)-1;
|
---|
| 110 | fragNum += delta;
|
---|
| 111 | cout << fragNum;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | cout << "\n";
|
---|
| 115 |
|
---|
| 116 | buffer.done();
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | static void PrintHeaderInfo (invf_dict_header &idh,
|
---|
| 121 | invf_file_header &ifh) {
|
---|
| 122 | cerr << "Lookback: " << idh.lookback << "\n";
|
---|
| 123 | cerr << "Word Dict Size: " << idh.word_dict_size << "\n";
|
---|
| 124 | cerr << "Tag Dict Size: " << idh.tag_dict_size << "\n";
|
---|
| 125 | cerr << "Num Documents: " << idh.num_docs << "\n";
|
---|
| 126 | cerr << "Num Fragments: " << idh.num_frags << "\n";
|
---|
| 127 | cerr << "Num Words: " << idh.num_words << "\n";
|
---|
| 128 |
|
---|
| 129 | cerr << "Skip Mode: " << ifh.skip_mode << "\n";
|
---|
| 130 | cerr << "Word Level Index: " << ifh.word_level_index << "\n";
|
---|
| 131 |
|
---|
| 132 | cerr << "\n";
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | static void process_files (char *filename,
|
---|
| 137 | bool printHeader,
|
---|
| 138 | bool printWords,
|
---|
| 139 | bool printTags,
|
---|
| 140 | bool printFrags) {
|
---|
| 141 | // open the dictionary
|
---|
| 142 | FILE *dictFile = open_file (filename, INVF_DICT_SUFFIX, "rb",
|
---|
| 143 | MAGIC_STEM_BUILD, MG_ABORT);
|
---|
| 144 | invf_dict_header idh;
|
---|
| 145 | idh.Read (dictFile);
|
---|
| 146 |
|
---|
| 147 | // open the inverted file
|
---|
| 148 | FILE *invfFile = open_file (filename, INVF_SUFFIX, "rb",
|
---|
| 149 | MAGIC_INVF, MG_ABORT);
|
---|
| 150 |
|
---|
| 151 | invf_file_header ifh;
|
---|
| 152 | ifh.Read (invfFile);
|
---|
| 153 |
|
---|
| 154 | if (ifh.skip_mode != SKIP_MODE_NO_SKIPS)
|
---|
| 155 | FatalError (1, "The invf file contains skips. Unable to dump.");
|
---|
| 156 |
|
---|
| 157 | // print out header information
|
---|
| 158 | if (printHeader) {
|
---|
| 159 | PrintHeaderInfo (idh, ifh);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | // open the inverted index
|
---|
| 163 | FILE *invfIdxFile = open_file (filename, INVF_IDX_SUFFIX, "rb",
|
---|
| 164 | MAGIC_INVI, MG_ABORT);
|
---|
| 165 |
|
---|
| 166 | // go to the start of the word dictionary
|
---|
| 167 | fseek (dictFile, idh.word_dict_start, SEEK_SET);
|
---|
| 168 |
|
---|
| 169 | // process all the words
|
---|
| 170 | if (printWords) {
|
---|
[25147] | 171 | mg_u_long wordNum;
|
---|
| 172 | mg_u_long wordStart;
|
---|
[3365] | 173 | word_dict_el wordEl;
|
---|
| 174 | wordEl.SetNumLevels (idh.num_levels);
|
---|
[9613] | 175 | for (wordNum=0; wordNum<idh.word_dict_size; ++wordNum) {
|
---|
[3365] | 176 | wordEl.Read (dictFile, idh.num_levels);
|
---|
| 177 | ReadUL (invfIdxFile, wordStart);
|
---|
| 178 | PrintInvfWord (invfFile, idh, ifh, wordEl, wordStart, printFrags);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | // process all the tags
|
---|
| 183 | if (printTags) {
|
---|
[25147] | 184 | mg_u_long tagNum;
|
---|
| 185 | mg_u_long tagStart;
|
---|
[3365] | 186 | dict_el tagEl;
|
---|
[9613] | 187 | for (tagNum=0; tagNum<idh.tag_dict_size; ++tagNum) {
|
---|
[3365] | 188 | tagEl.Read (dictFile);
|
---|
| 189 | ReadUL (invfIdxFile, tagStart);
|
---|
| 190 | PrintInvfTag (invfFile, idh, ifh, tagEl, tagStart, printFrags);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | // close the open files
|
---|
| 194 | fclose (invfIdxFile);
|
---|
| 195 | fclose (invfFile);
|
---|
| 196 | fclose (dictFile);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | int main (int argc, char **argv) {
|
---|
[25147] | 201 | char *dir_name, *filename = (char*)"";
|
---|
[3365] | 202 | int ch;
|
---|
| 203 | msg_prefix = argv[0];
|
---|
| 204 | dir_name = getenv ("MGDATA");
|
---|
| 205 | opterr = 0;
|
---|
| 206 |
|
---|
| 207 | bool printHeader = false;
|
---|
| 208 | bool printWords = false;
|
---|
| 209 | bool printTags = false;
|
---|
| 210 | bool printFrags = false;
|
---|
| 211 |
|
---|
| 212 | msg_prefix = argv[0];
|
---|
| 213 | while ((ch = getopt (argc, argv, "hrwtnf:d:")) != -1) {
|
---|
| 214 | switch (ch) {
|
---|
| 215 | case 'f': // input file
|
---|
| 216 | filename = optarg;
|
---|
| 217 | break;
|
---|
| 218 | case 'd':
|
---|
| 219 | set_basepath(optarg);
|
---|
| 220 | break;
|
---|
| 221 | case 'r':
|
---|
| 222 | printHeader = true;
|
---|
| 223 | break;
|
---|
| 224 | case 'w':
|
---|
| 225 | printWords = true;
|
---|
| 226 | break;
|
---|
| 227 | case 'n':
|
---|
| 228 | printFrags = true;
|
---|
| 229 | break;
|
---|
| 230 | case 't':
|
---|
| 231 | printTags = true;
|
---|
| 232 | break;
|
---|
| 233 | case 'h':
|
---|
| 234 | case '?':
|
---|
| 235 | fprintf (stderr, "usage: %s [-h] [-r] [-w] [-t] [-n] [-f input_file]"
|
---|
| 236 | "[-d data directory]\n(-rwnt:print header, words, tags, fragnums)\n",
|
---|
| 237 | argv[0]);
|
---|
| 238 | exit (1);
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | process_files (filename, printHeader, printWords, printTags, printFrags);
|
---|
| 243 |
|
---|
| 244 | return 0;
|
---|
| 245 | }
|
---|