PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
spell.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * spell.h
4  *
5  * Declarations for ISpell dictionary
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  *
9  * src/include/tsearch/dicts/spell.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifndef __SPELL_H__
15 #define __SPELL_H__
16 
17 #include "regex/regex.h"
18 #include "tsearch/dicts/regis.h"
19 #include "tsearch/ts_public.h"
20 
21 /*
22  * SPNode and SPNodeData are used to represent prefix tree (Trie) to store
23  * a words list.
24  */
25 struct SPNode;
26 
27 typedef struct
28 {
30  isword:1,
31  /* Stores compound flags listed below */
32  compoundflag:4,
33  /* Reference to an entry of the AffixData field */
34  affix:19;
35  struct SPNode *node;
36 } SPNodeData;
37 
38 /*
39  * Names of FF_ are correlated with Hunspell options in affix file
40  * http://hunspell.sourceforge.net/
41  */
42 #define FF_COMPOUNDONLY 0x01
43 #define FF_COMPOUNDBEGIN 0x02
44 #define FF_COMPOUNDMIDDLE 0x04
45 #define FF_COMPOUNDLAST 0x08
46 #define FF_COMPOUNDFLAG ( FF_COMPOUNDBEGIN | FF_COMPOUNDMIDDLE | \
47  FF_COMPOUNDLAST )
48 #define FF_COMPOUNDFLAGMASK 0x0f
49 
50 typedef struct SPNode
51 {
53  SPNodeData data[FLEXIBLE_ARRAY_MEMBER];
54 } SPNode;
55 
56 #define SPNHDRSZ (offsetof(SPNode,data))
57 
58 /*
59  * Represents an entry in a words list.
60  */
61 typedef struct spell_struct
62 {
63  union
64  {
65  /*
66  * flag is filled in by NIImportDictionary(). After
67  * NISortDictionary(), d is used instead of flag.
68  */
69  char *flag;
70  /* d is used in mkSPNode() */
71  struct
72  {
73  /* Reference to an entry of the AffixData field */
74  int affix;
75  /* Length of the word */
76  int len;
77  } d;
78  } p;
79  char word[FLEXIBLE_ARRAY_MEMBER];
80 } SPELL;
81 
82 #define SPELLHDRSZ (offsetof(SPELL, word))
83 
84 /*
85  * Represents an entry in an affix list.
86  */
87 typedef struct aff_struct
88 {
89  char *flag;
90  /* FF_SUFFIX or FF_PREFIX */
92  flagflags:7,
93  issimple:1,
94  isregis:1,
95  replen:14;
96  char *find;
97  char *repl;
98  union
99  {
102  } reg;
103 } AFFIX;
104 
105 /*
106  * affixes use dictionary flags too
107  */
108 #define FF_COMPOUNDPERMITFLAG 0x10
109 #define FF_COMPOUNDFORBIDFLAG 0x20
110 #define FF_CROSSPRODUCT 0x40
111 
112 /*
113  * Don't change the order of these. Initialization sorts by these,
114  * and expects prefixes to come first after sorting.
115  */
116 #define FF_SUFFIX 1
117 #define FF_PREFIX 0
118 
119 /*
120  * AffixNode and AffixNodeData are used to represent prefix tree (Trie) to store
121  * an affix list.
122  */
123 struct AffixNode;
124 
125 typedef struct
126 {
128  naff:24;
130  struct AffixNode *node;
131 } AffixNodeData;
132 
133 typedef struct AffixNode
134 {
136  length:31;
137  AffixNodeData data[FLEXIBLE_ARRAY_MEMBER];
138 } AffixNode;
139 
140 #define ANHRDSZ (offsetof(AffixNode, data))
141 
142 typedef struct
143 {
144  char *affix;
145  int len;
146  bool issuffix;
147 } CMPDAffix;
148 
149 /*
150  * Type of encoding affix flags in Hunspell dictionaries
151  */
152 typedef enum
153 {
154  FM_CHAR, /* one character (like ispell) */
155  FM_LONG, /* two characters */
156  FM_NUM /* number, >= 0 and < 65536 */
157 } FlagMode;
158 
159 /*
160  * Structure to store Hunspell options. Flag representation depends on flag
161  * type. These flags are about support of compound words.
162  */
163 typedef struct CompoundAffixFlag
164 {
165  union
166  {
167  /* Flag name if flagMode is FM_CHAR or FM_LONG */
168  char *s;
169  /* Flag name if flagMode is FM_NUM */
171  } flag;
172  /* we don't have a bsearch_arg version, so, copy FlagMode */
176 
177 #define FLAGNUM_MAXSIZE (1 << 16)
178 
179 typedef struct
180 {
181  int maffixes;
182  int naffixes;
184 
187 
189  /* Array of sets of affixes */
190  char **AffixData;
194 
196 
199 
200  /*
201  * All follow fields are actually needed only for initialization
202  */
203 
204  /* Array of Hunspell options in affix file */
206  /* number of entries in CompoundAffixFlags array */
208  /* allocated length of CompoundAffixFlags array */
210 
211  /*
212  * Remaining fields are only used during dictionary construction; they are
213  * set up by NIStartBuild and cleared by NIFinishBuild.
214  */
215  MemoryContext buildCxt; /* temp context for construction */
216 
217  /* Temporary array of all words in the dict file */
219  int nspell; /* number of valid entries in Spell array */
220  int mspell; /* allocated length of Spell array */
221 
222  /* These are used to allocate "compact" data without palloc overhead */
223  char *firstfree; /* first free address (always maxaligned) */
224  size_t avail; /* free space remaining at firstfree */
225 } IspellDict;
226 
227 extern TSLexeme *NINormalizeWord(IspellDict *Conf, char *word);
228 
229 extern void NIStartBuild(IspellDict *Conf);
230 extern void NIImportAffixes(IspellDict *Conf, const char *filename);
231 extern void NIImportDictionary(IspellDict *Conf, const char *filename);
232 extern void NISortDictionary(IspellDict *Conf);
233 extern void NISortAffixes(IspellDict *Conf);
234 extern void NIFinishBuild(IspellDict *Conf);
235 
236 #endif
char * flag
Definition: spell.h:89
MemoryContext buildCxt
Definition: spell.h:215
int naffixes
Definition: spell.h:182
int nAffixData
Definition: spell.h:192
struct spell_struct::@110::@111 d
Definition: regis.h:32
struct SPNode SPNode
int lenAffixData
Definition: spell.h:191
int mspell
Definition: spell.h:220
TSLexeme * NINormalizeWord(IspellDict *Conf, char *word)
Definition: spell.c:2500
void NIImportAffixes(IspellDict *Conf, const char *filename)
Definition: spell.c:1408
bool useFlagAliases
Definition: spell.h:193
void NISortDictionary(IspellDict *Conf)
Definition: spell.c:1697
uint32 length
Definition: spell.h:135
uint32 flagflags
Definition: spell.h:91
bool usecompound
Definition: spell.h:197
struct AffixNode * node
Definition: spell.h:130
AffixNodeData data[FLEXIBLE_ARRAY_MEMBER]
Definition: spell.h:137
AffixNode * Prefix
Definition: spell.h:186
size_t avail
Definition: spell.h:224
void NIStartBuild(IspellDict *Conf)
Definition: spell.c:87
AffixNode * Suffix
Definition: spell.h:185
SPNodeData data[FLEXIBLE_ARRAY_MEMBER]
Definition: spell.h:53
AFFIX ** aff
Definition: spell.h:129
uint32 type
Definition: spell.h:91
int affix
Definition: spell.h:74
Definition: spell.h:154
uint32 isregis
Definition: spell.h:91
uint32 length
Definition: spell.h:52
struct SPNode * node
Definition: spell.h:35
Regis regis
Definition: spell.h:101
SPNode * Dictionary
Definition: spell.h:188
uint32 value
Definition: spell.h:174
int nspell
Definition: spell.h:219
int len
Definition: spell.h:145
void NIFinishBuild(IspellDict *Conf)
Definition: spell.c:102
union aff_struct::@112 reg
char ** AffixData
Definition: spell.h:190
Definition: spell.h:156
uint32 issimple
Definition: spell.h:91
AFFIX * Affix
Definition: spell.h:183
struct aff_struct AFFIX
Definition: spell.h:50
unsigned int uint32
Definition: c.h:268
union CompoundAffixFlag::@113 flag
char word[FLEXIBLE_ARRAY_MEMBER]
Definition: spell.h:79
char * find
Definition: spell.h:96
int len
Definition: spell.h:76
struct spell_struct SPELL
char * flag
Definition: spell.h:69
struct AffixNode AffixNode
char * affix
Definition: spell.h:144
FlagMode flagMode
Definition: spell.h:173
struct CompoundAffixFlag CompoundAffixFlag
regex_t regex
Definition: spell.h:100
CompoundAffixFlag * CompoundAffixFlags
Definition: spell.h:205
bool issuffix
Definition: spell.h:146
char * firstfree
Definition: spell.h:223
int mCompoundAffixFlag
Definition: spell.h:209
SPELL ** Spell
Definition: spell.h:218
CMPDAffix * CompoundAffix
Definition: spell.h:195
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1243
Definition: spell.h:155
static char * filename
Definition: pg_dumpall.c:89
FlagMode
Definition: spell.h:152
void NIImportDictionary(IspellDict *Conf, const char *filename)
Definition: spell.c:514
char * repl
Definition: spell.h:97
uint32 isvoid
Definition: spell.h:135
int maffixes
Definition: spell.h:181
uint32 replen
Definition: spell.h:91
int nCompoundAffixFlag
Definition: spell.h:207
void NISortAffixes(IspellDict *Conf)
Definition: spell.c:1941
Definition: regex.h:55
long val
Definition: informix.c:689
union spell_struct::@110 p
FlagMode flagMode
Definition: spell.h:198