05bd7082d4738968220d5ce3c80e8b4a811926f3
[SXSI/TextCollection.git] / swcsa / buildFacade.h
1 /* only for getTime() */
2 #include <sys/time.h>
3 #include <sys/resource.h>
4
5
6 #include "utils/valstring.h"
7 #include "utils/defValues.h"
8 #include "utils/MemoryManager.h"
9 #include "utils/fileInfo.h"
10
11 #include "utils/hash.h"
12
13 #include "utils/huff.h"
14 //#include "utils/errors.c"
15 #include "utils/parameters.h"
16
17 //from SEARCHER FACADE 
18 #include "utils/huffDec.h"
19 //#include "icsa/icsa.h"
20
21 #include "intIndex/interfaceIntIndex.h"
22
23 #ifndef uchar
24 #define uchar unsigned char
25 #endif
26 #ifndef uint
27 #define uint  unsigned int
28 #endif
29 #ifndef ulong
30 #define ulong unsigned long
31 #endif
32
33 #define STRLEN(str,len) \
34 {len=0; \
35  byte *ptr = str; \
36  while(*ptr++) len++; \
37 }
38
39 #define ADDLEN(str,len) \
40 {byte *ptr = str; \
41         while(*ptr++) len++; \
42 }
43
44 /** Some data types used **ONLY**during construction process */
45         
46 // Words, both the canonical words and their variants
47         typedef struct {
48                 unsigned long slot;   // the position in the hash table of the canonical word
49                 byte *word; //makes alphanumerical sorting easier...
50         } tposInHT;
51         
52         
53         typedef struct SzoneMem {        //a large block of memory to load a file into mem.
54                 byte *zone;  //block of mem.
55                 uint size;  //number of bytes            
56         } tZoneMem;
57
58         //  words dataStructure.        
59         typedef struct {
60                 uint *words;
61                 uint elemSize;  //the size (in bits) of each pointer.
62                 tZoneMem wordsZoneMem; // a block of memory where the canonical words are loaded (from file).
63
64         } twords;
65
66
67 /** Some data types used during searches */
68
69         
70
71
72         /**the WCSA index structures... */
73         typedef struct {
74                         
75                 /**valid words */
76                 twords wordsData;               /* vocabulary (words) of the index */
77
78                 ulong n;                                /* number of different words. */                
79                 uint seSize;                    /* number of words in the source text */ 
80
81                 uint sourceTextSize;    /*the size of the source text in bytes*/
82
83                 //ticsa *myicsa; //the WiCSA on SE words
84                 void *myicsa; //the WiCSA on SE words
85         
86                 //#ifndef CSA_ON
87                 uint *se;
88                 //#endif
89
90         }twcsa;
91
92
93 /** ******************************************************************************
94     * Interface (from pizza chili) for using the WCSA index
95 *********************************************************************************/
96
97 /* Error management */
98
99         /* Returns a string describing the error associated with error number
100           e. The string must not be freed, and it will be overwritten with
101           subsequent calls. */
102
103 char *error_index (int e);
104
105 /* Building the index */
106
107         /* Creates index from text[0..length-1]. Note that the index is an 
108           opaque data type. Any build option must be passed in string 
109           build_options, whose syntax depends on the index. The index must 
110           always work with some default parameters if build_options is NULL. 
111           The returned index is ready to be queried. */
112
113 int build_index (uchar *text, ulong length, char *build_options, void **index);
114
115         /*  Saves index on disk by using single or multiple files, having 
116           proper extensions. */
117
118 int save_index (void *index, char *filename);
119
120         /*  Loads index from one or more file(s) named filename, possibly 
121           adding the proper extensions. */
122
123 int load_index (char *filename, void **index);
124
125         /* Frees the memory occupied by index. */
126
127 int free_index (void *index);
128
129         /* Gives the memory occupied by index in bytes. */
130
131 int index_size(void *index, ulong *size);
132
133 /* Querying the index */
134
135         /* Writes in numocc the number of occurrences of the substring 
136           pattern[0..length-1] found in the text indexed by index. */
137
138 int count (void *index, uchar *pattern, ulong length, ulong *numocc);
139
140         /* Gives the length of the text indexed */
141
142 int get_length(void *index, ulong *length);
143
144 /* Accessing the indexed text  */
145
146         /* Writes in numocc the number of occurrences of the substring 
147           pattern[0..length-1] in the text indexed by index. It also allocates
148           occ (which must be freed by the caller) and writes the locations of 
149           the numocc occurrences in occ, in arbitrary order.  */
150
151 int locate (void *index, uchar *pattern, ulong length, ulong **occ, 
152         ulong *numocc);
153
154         /*  Allocates snippet (which must be freed by the caller) and writes 
155           the substring text[from..to] into it. Returns in snippet_length the 
156           length of the text snippet actually extracted (that could be less 
157           than to-from+1 if to is larger than the text size). */
158
159 int extract (void *index, ulong from, ulong to, uchar **snippet, 
160         ulong *snippet_length);
161
162         /* Displays the text (snippet) surrounding any occurrence of the 
163           substring pattern[0..length-1] within the text indexed by index. 
164           The snippet must include numc characters before and after the 
165           pattern occurrence, totalizing length+2*numc characters, or less if 
166           the text boundaries are reached. Writes in numocc the number of 
167           occurrences, and allocates the arrays snippet_text and 
168           snippet_lengths (which must be freed by the caller). The first is a 
169           character array of numocc*(length+2*numc) characters, with a new 
170           snippet starting at every multiple of length+2*numc. The second 
171           gives the real length of each of the numocc snippets. */
172
173 int display (void *index, uchar *pattern, ulong length, ulong numc, 
174         ulong *numocc, uchar **snippet_text, ulong **snippet_lengths);
175
176         /*  Obtains the length of the text indexed by index. */
177
178 int length (void *index, ulong *length);
179
180                 /* Shows summary info of the index */
181 int printInfo(void *index);
182
183 /** *******************************************************************************************/
184 /** Building part of the index ****************************************************************/
185
186 int build_WCSA (uchar *text, ulong length, char *build_options, void **index);
187 int build_iCSA (char  *build_options, void *index); 
188
189
190
191 /** *******************************************************************************************/
192 /** Search part of the index ******************************************************************/
193 // Definitions of some PUBLIC function prototipes.
194
195                 //loading/freeing the data structures into memory.
196         
197     void loadStructs(twcsa *wcsa, char *basename);      
198         twcsa *loadWCSA(char *filename);        
199         
200                 //returns the source text from given [offsetIni, offsetFin] offsets.
201         //byte *displayFacade (twcsa *wcsa, uint offsetIni, uint offsetFin);
202         byte *displayFacadeMalloc (twcsa *wcsa, uint offsetIni, uint offsetFin, ulong *length);  
203         int displayFacade (twcsa *wcsa, uint offsetIni, uint offsetFin, ulong *length, byte *dstptr);
204
205                 //locate all the ocurrences of a word/phrase 
206         int locateFacade (twcsa *wcsa, uint *sourceTextPositions,uint *sePositions, uint number);
207         
208                 //show text around the occurrences of a word. 
209         int locateAllAndDisplay (twcsa *wcsa, uint *sePositions, uint number, int radix);
210
211                 //recovers the source text by calling display (either only once or "len" times)
212         void recoverSourceText1(twcsa *wcsa, char *basename, char *ext, uint sourceTextSize);
213         void recoverSourceText2(twcsa *wcsa, char *basename, char *ext, uint sourceTextSize);
214
215         //***Searching for a TEXT pattern ...
216         
217                 //extracts the ids of the valid words of a "plain text".
218         void parseTextIntoIntegers(twcsa *wcsa, byte *textPattern, uint patLen, uint *integerPattern, uint *sizeIntegers) ;
219                 
220                 //counts the occurrences of a given text pattern.
221         int countTextOcurrences(twcsa *wcsa, byte *textPattern);
222         
223                 //returns the offsets (to the source text) where of a given text pattern appears.       
224         uint *locateTextOcurrences(twcsa *wcsa, byte *textPattern, int *numberOccurrences);
225         
226                 //shows a snippet with the text around the ocurrences of a pattern.     
227         int displayTextOcurrences(twcsa *wcsa, byte *textPattern, uint radixDisplay);
228
229
230 /** ***********************************************************************************
231   * WORD-ORIENTED QUERY FUNCTIONS: LocateWord and DisplayWord
232   * ***********************************************************************************/  
233         /** Writes in numocc the number of occurrences of the substring 
234           pattern[0..length-1] in the text indexed by index. It also allocates
235           occ (which must be freed by the caller) and writes the locations of 
236           the numocc occurrences in occ, in arbitrary order. These occurrences
237           refer to the offsets in TOH where the caller could start a display
238           operation. So locateWord implies synchronization using B.
239           ** Parameter kbefore sets locateWord not to obtain the offset in TOH of the
240              searched word, but the offset in TOH of k-before words before.       
241         */        
242           
243 int locateWord(void *index, uchar *pattern, ulong length, ulong **occ, ulong *numocc, uint kbefore);
244
245   /** Displays the text (snippet) surrounding any occurrence of the 
246     substring pattern[0..length-1] within the text indexed by index. 
247     The snippet must include numc characters before and after the 
248     pattern occurrence, totalizing length+2*numc characters, or less if 
249     the text boundaries are reached. Writes in numocc the number of 
250     occurrences, and allocates the arrays snippet_text and 
251     snippet_lengths (which must be freed by the caller). The first is a 
252     character array of numocc*(length+2*numc) characters, with a new 
253     snippet starting at every multiple of length+2*numc. The second 
254     gives the real length of each of the numocc snippets. */
255
256  int displayWords (void *index, uchar *pattern, ulong length, ulong numc, 
257          ulong *numocc, uchar **snippet_text, ulong **snippet_lengths, uint kbefore);
258
259
260 /** simulates extration of text process, but do not actually returns anything at all 
261    Extracts upto <=2K words from K=wordsbefore words before each occurrence of a pattern.
262    Less than 2K words can be extracted if more than numc characters have been already obtained.
263    Do nothing else... do not return the text */
264    
265         int  displayTextOcurrencesNoShow(void *index, uchar *pattern, ulong length, uint wordsbefore, uint maxnumc);
266
267
268 /**  Allocates text (which must be freed by the caller) and recovers the
269   the substring of text starting from the "fromword"-th word up to the
270   "toWord"-th words. Returns in text the text, and in "text_lenght" the 
271   length of the text  actually extracted. Text is allocated. 
272   Actually extracts SE[fromWord .. toWord) ... not the last word.    */
273
274 int extractWords (void *index, ulong fromWord, ulong toWord, uchar **text, 
275         ulong *text_length);
276
277
278
279
280                 //recovers the source text by calling display (either only once or "len" times)
281         void recoverSourceText1(twcsa *wcsa, char *basename, char *ext, uint sourceTextSize);
282         void recoverSourceText2(twcsa *wcsa, char *basename, char *ext, uint sourceTextSize);
283
284
285 // Definitions of PRIVATE functions
286
287         //Auxiliary functions
288
289         uint structsSizeDisk(twcsa *wcsa);
290         uint structsSizeMem(twcsa *wcsa);
291         void printInfoReduced(twcsa *wcsa); 
292         int saveSEfile (char *basename, uint *v, uint n);
293         double getTime2 (void); 
294
295