1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Hunspell, based on MySpell.
15  *
16  * The Initial Developers of the Original Code are
17  * Kevin Hendricks (MySpell) and Németh László (Hunspell).
18  * Portions created by the Initial Developers are Copyright (C) 2002-2005
19  * the Initial Developers. All Rights Reserved.
20  *
21  * Contributor(s): David Einstein, Davide Prina, Giuseppe Modugno,
22  * Gianluca Turconi, Simon Brouwer, Noll János, Bíró Árpád,
23  * Goldman Eleonóra, Sarlós Tamás, Bencsáth Boldizsár, Halácsy Péter,
24  * Dvornik László, Gefferth András, Nagy Viktor, Varga Dániel, Chris Halls,
25  * Rene Engelhard, Bram Moolenaar, Dafydd Jones, Harri Pitkänen
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 /*
41  * Copyright 2002 Kevin B. Hendricks, Stratford, Ontario, Canada
42  * And Contributors.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  *
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  *
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  *
55  * 3. All modifications to the source code must be clearly marked as
56  *    such.  Binary redistributions based on modified source code
57  *    must be clearly marked as modified versions in the documentation
58  *    and/or other materials provided with the distribution.
59  *
60  * THIS SOFTWARE IS PROVIDED BY KEVIN B. HENDRICKS AND CONTRIBUTORS
61  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
62  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
63  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
64  * KEVIN B. HENDRICKS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
65  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
66  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73 #ifndef MYSPELLMGR_HXX_
74 #define MYSPELLMGR_HXX_
75 
76 #include "hunvisapi.h"
77 #include "w_char.hxx"
78 #include "atypes.hxx"
79 #include <string>
80 #include <vector>
81 
82 #ifdef HUNSPELL_CHROME_CLIENT
83 #include "third_party/hunspell/google/bdict_reader.h"
84 #endif
85 
86 #define SPELL_XML "<?xml?>"
87 
88 #define MAXSUGGESTION 5
89 #define MAXSHARPS 5
90 
91 #ifndef MAXWORDLEN
92 #define MAXWORDLEN 100
93 #endif
94 
95 #if defined __GNUC__ && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
96 #  define H_DEPRECATED __attribute__((__deprecated__))
97 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
98 #  define H_DEPRECATED __declspec(deprecated)
99 #else
100 #  define H_DEPRECATED
101 #endif
102 
103 class HunspellImpl;
104 
105 class LIBHUNSPELL_DLL_EXPORTED Hunspell {
106  private:
107   Hunspell(const Hunspell&);
108   Hunspell& operator=(const Hunspell&);
109 
110  private:
111   HunspellImpl* m_Impl;
112 
113  public:
114   /* Hunspell(aff, dic) - constructor of Hunspell class
115    * input: path of affix file and dictionary file
116    *
117    * In WIN32 environment, use UTF-8 encoded paths started with the long path
118    * prefix \\\\?\\ to handle system-independent character encoding and very
119    * long path names (without the long path prefix Hunspell will use fopen()
120    * with system-dependent character encoding instead of _wfopen()).
121    */
122 #ifdef HUNSPELL_CHROME_CLIENT
123   Hunspell(const unsigned char* bdict_data, size_t bdict_length);
124 #else
125   Hunspell(const char* affpath, const char* dpath, const char* key = NULL);
126 #endif
127   ~Hunspell();
128 
129 #ifndef HUNSPELL_CHROME_CLIENT
130   /* load extra dictionaries (only dic files) */
131   int add_dic(const char* dpath, const char* key = NULL);
132 #endif
133 
134   /* spell(word) - spellcheck word
135    * output: false = bad word, true = good word
136    *
137    * plus output:
138    *   info: information bit array, fields:
139    *     SPELL_COMPOUND  = a compound word
140    *     SPELL_FORBIDDEN = an explicit forbidden word
141    *   root: root (stem), when input is a word with affix(es)
142    */
143   bool spell(const std::string& word, int* info = NULL, std::string* root = NULL);
144   H_DEPRECATED int spell(const char* word, int* info = NULL, char** root = NULL);
145 
146   /* suggest(suggestions, word) - search suggestions
147    * input: pointer to an array of strings pointer and the (bad) word
148    *   array of strings pointer (here *slst) may not be initialized
149    * output: number of suggestions in string array, and suggestions in
150    *   a newly allocated array of strings (*slts will be NULL when number
151    *   of suggestion equals 0.)
152    */
153   std::vector<std::string> suggest(const std::string& word);
154   H_DEPRECATED int suggest(char*** slst, const char* word);
155 
156   /* Suggest words from suffix rules
157    * suffix_suggest(suggestions, root_word)
158    * input: pointer to an array of strings pointer and the  word
159    *   array of strings pointer (here *slst) may not be initialized
160    * output: number of suggestions in string array, and suggestions in
161    *   a newly allocated array of strings (*slts will be NULL when number
162    *   of suggestion equals 0.)
163    */
164   std::vector<std::string> suffix_suggest(const std::string& root_word);
165   H_DEPRECATED int suffix_suggest(char*** slst, const char* root_word);
166 
167   /* deallocate suggestion lists */
168   H_DEPRECATED void free_list(char*** slst, int n);
169 
170   const std::string& get_dict_encoding() const;
171   char* get_dic_encoding();
172 
173   /* morphological functions */
174 
175   /* analyze(result, word) - morphological analysis of the word */
176   std::vector<std::string> analyze(const std::string& word);
177   H_DEPRECATED int analyze(char*** slst, const char* word);
178 
179   /* stem(word) - stemmer function */
180   std::vector<std::string> stem(const std::string& word);
181   H_DEPRECATED int stem(char*** slst, const char* word);
182 
183   /* stem(analysis, n) - get stems from a morph. analysis
184    * example:
185    * char ** result, result2;
186    * int n1 = analyze(&result, "words");
187    * int n2 = stem(&result2, result, n1);
188    */
189   std::vector<std::string> stem(const std::vector<std::string>& morph);
190   H_DEPRECATED int stem(char*** slst, char** morph, int n);
191 
192   /* generate(result, word, word2) - morphological generation by example(s) */
193   std::vector<std::string> generate(const std::string& word, const std::string& word2);
194   H_DEPRECATED int generate(char*** slst, const char* word, const char* word2);
195 
196   /* generate(result, word, desc, n) - generation by morph. description(s)
197    * example:
198    * char ** result;
199    * char * affix = "is:plural"; // description depends from dictionaries, too
200    * int n = generate(&result, "word", &affix, 1);
201    * for (int i = 0; i < n; i++) printf("%s\n", result[i]);
202    */
203   std::vector<std::string> generate(const std::string& word, const std::vector<std::string>& pl);
204   H_DEPRECATED int generate(char*** slst, const char* word, char** desc, int n);
205 
206   /* functions for run-time modification of the dictionary */
207 
208   /* add word to the run-time dictionary */
209 
210   int add(const std::string& word);
211 
212   /* add word to the run-time dictionary with affix flags of
213    * the example (a dictionary word): Hunspell will recognize
214    * affixed forms of the new word, too.
215    */
216 
217   int add_with_affix(const std::string& word, const std::string& example);
218 
219   /* remove word from the run-time dictionary */
220 
221   int remove(const std::string& word);
222 
223   /* other */
224 
225   /* get extra word characters definied in affix file for tokenization */
226   const char* get_wordchars() const;
227   const std::string& get_wordchars_cpp() const;
228   const std::vector<w_char>& get_wordchars_utf16() const;
229 
230   struct cs_info* get_csconv();
231 
232   const char* get_version() const;
233   const std::string& get_version_cpp() const;
234 
235   int get_langnum() const;
236 
237   /* need for putdic */
238   bool input_conv(const std::string& word, std::string& dest);
239   H_DEPRECATED int input_conv(const char* word, char* dest, size_t destsize);
240 };
241 
242 #endif
243