summaryrefslogtreecommitdiff
path: root/src/include/tsearch/dicts/spell.h
blob: cfcc9391d63d3af0869cdc06f63c11a250e38648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*-------------------------------------------------------------------------
 *
 * spell.h
 *
 * Declarations for ISpell dictionary
 *
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
 *
 * $PostgreSQL: pgsql/src/include/tsearch/dicts/spell.h,v 1.4 2007/11/15 21:14:45 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

#ifndef __SPELL_H__
#define __SPELL_H__

#include "regex/regex.h"
#include "tsearch/dicts/regis.h"
#include "tsearch/ts_public.h"

/*
 * Max length of a flag name. Names longer than this will be truncated
 * to the maximum.
 */
#define MAXFLAGLEN 16

struct SPNode;

typedef struct
{
	uint32		val:8,
				isword:1,
				compoundflag:4,
				affix:19;
	struct SPNode *node;
}	SPNodeData;

/*
 * Names of FF_ are correlated with Hunspell options in affix file
 * http://sourceforge.net/docman/display_doc.php?docid=29374&group_id=143754
 */
#define FF_COMPOUNDONLY		0x01
#define FF_COMPOUNDBEGIN	0x02
#define FF_COMPOUNDMIDDLE	0x04
#define FF_COMPOUNDLAST		0x08
#define FF_COMPOUNDFLAG		( FF_COMPOUNDBEGIN | FF_COMPOUNDMIDDLE | FF_COMPOUNDLAST )
#define FF_DICTFLAGMASK		0x0f

typedef struct SPNode
{
	uint32		length;
	SPNodeData	data[1];
}	SPNode;

#define SPNHDRSZ	(offsetof(SPNode,data))


typedef struct spell_struct
{
	union
	{
		/*
		 * flag is filled in by NIImportDictionary. After NISortDictionary, d
		 * is valid and flag is invalid.
		 */
		char		flag[MAXFLAGLEN];
		struct
		{
			int			affix;
			int			len;
		}			d;
	}			p;
	char		word[1];		/* variable length, null-terminated */
}	SPELL;

#define SPELLHDRSZ	(offsetof(SPELL, word))

typedef struct aff_struct
{
	uint32		flag:8,
				type:1,
				flagflags:7,
				issimple:1,
				isregis:1,
				replen:14;
	char	   *find;
	char	   *repl;
	union
	{
		regex_t		regex;
		Regis		regis;
	}			reg;
}	AFFIX;

/*
 * affixes use dictionary flags too
 */
#define FF_COMPOUNDPERMITFLAG	0x10
#define FF_COMPOUNDFORBIDFLAG	0x20
#define FF_CROSSPRODUCT			0x40

/*
 * Don't change the order of these. Initialization sorts by these,
 * and expects prefixes to come first after sorting.
 */
#define FF_SUFFIX				1
#define FF_PREFIX				0

struct AffixNode;

typedef struct
{
	uint32		val:8,
				naff:24;
	AFFIX	  **aff;
	struct AffixNode *node;
}	AffixNodeData;

typedef struct AffixNode
{
	uint32		isvoid:1,
				length:31;
	AffixNodeData data[1];
}	AffixNode;

#define ANHRDSZ		   (offsetof(AffixNode, data))

typedef struct
{
	char	   *affix;
	int			len;
	bool		issuffix;
}	CMPDAffix;

typedef struct
{
	int			maffixes;
	int			naffixes;
	AFFIX	   *Affix;

	/*
	 * Temporary array of all words in the dict file. Only used during
	 * initialization
	 */
	SPELL	  **Spell;
	int			nspell;			/* number of valid entries in Spell array */
	int			mspell;			/* allocated length of Spell array */

	AffixNode  *Suffix;
	AffixNode  *Prefix;

	SPNode	   *Dictionary;
	char	  **AffixData;
	int			lenAffixData;
	int			nAffixData;

	CMPDAffix  *CompoundAffix;

	unsigned char flagval[256];
	bool		usecompound;
}	IspellDict;

extern TSLexeme *NINormalizeWord(IspellDict * Conf, char *word);
extern void NIImportAffixes(IspellDict * Conf, const char *filename);
extern void NIImportDictionary(IspellDict * Conf, const char *filename);
extern void NISortDictionary(IspellDict * Conf);
extern void NISortAffixes(IspellDict * Conf);

#endif