summaryrefslogtreecommitdiff
path: root/providers/enchant_voikko.c
blob: 97ff3be3565c8677093751f27ba75e949a47a659 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* enchant
 * Copyright (C) 2003,2004 Dom Lachowicz
 *               2006-2007 Harri Pitkänen <hatapitk@iki.fi>
 *               2006 Anssi Hannula <anssi.hannula@gmail.com>
 *               2017 Børre Gaup <borre.gaup@uit.no>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders
 * give permission to link the code of this program with
 * non-LGPL Spelling Provider libraries (eg: a MSFT Office
 * spell checker backend) and distribute linked combinations including
 * the two.  You must obey the GNU Lesser General Public License in all
 * respects for all of the code used other than said providers.  If you modify
 * this file, you may extend this exception to your version of the
 * file, but you are not obligated to do so.  If you do not wish to
 * do so, delete this exception statement from your version.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libvoikko/voikko.h>

#include "enchant-provider.h"

/**
 * Voikko is a spell checker for VFST and HFST format dictionaries. More information is available from:
 *
 * http://voikko.sourceforge.net/
 */

static int
voikko_dict_check (EnchantDict * me, const char *const word, size_t len)
{
	char *word_nul = g_strndup(word, len);
	int result = voikkoSpellCstr((struct VoikkoHandle *)me->user_data, word_nul);
	free(word_nul);
	if (result == VOIKKO_SPELL_FAILED)
		return 1;
	else if (result == VOIKKO_SPELL_OK)
		return 0;
	else
		return -1;
}

static char **
voikko_dict_suggest (EnchantDict * me, const char *const word,
		     size_t len, size_t * out_n_suggs)
{
	char *word_nul = g_strndup(word, len);
	char **voikko_sugg_arr = voikkoSuggestCstr((struct VoikkoHandle *)me->user_data, word_nul);
	free(word_nul);
	if (voikko_sugg_arr == NULL)
		return NULL;
	for (*out_n_suggs = 0; voikko_sugg_arr[*out_n_suggs] != NULL; (*out_n_suggs)++);

	char **sugg_arr = calloc(sizeof (char *), *out_n_suggs + 1);
	if (sugg_arr == NULL)
		return NULL;
	for (size_t i = 0; i < *out_n_suggs; i++) {
		sugg_arr[i] = strdup (voikko_sugg_arr[i]);
	}
	voikkoFreeCstrArray (voikko_sugg_arr);
	return sugg_arr;
}

static void
voikko_provider_dispose_dict (EnchantProvider * me _GL_UNUSED, EnchantDict * dict)
{
	voikkoTerminate((struct VoikkoHandle *)dict->user_data);
	free (dict);
}

static char **
voikko_provider_list_dicts (EnchantProvider * me _GL_UNUSED,
			    size_t * out_n_dicts)
{
	size_t i;
	char ** out_list = NULL;
	*out_n_dicts = 0;
	char ** voikko_langs = voikkoListSupportedSpellingLanguages (NULL);

	for (i = 0; voikko_langs[i] != NULL; i++) {
		(*out_n_dicts)++;
	}

	if (*out_n_dicts) {
		out_list = calloc (*out_n_dicts + 1, sizeof (char *));
		if (out_list != NULL)
			for (i = 0; i < *out_n_dicts; i++) {
				out_list[i] = strdup (voikko_langs[i]);
			}
	}
	voikkoFreeCstrArray(voikko_langs);

	return out_list;
}

static int
voikko_provider_dictionary_exists (struct str_enchant_provider * me _GL_UNUSED,
				   const char *const tag)
{
	size_t i;
	int exists = 0;
	char ** voikko_langs = voikkoListSupportedSpellingLanguages (NULL);

	for (i = 0; voikko_langs[i] != NULL; i++) {
		if (strncmp (tag, voikko_langs[i], strlen (tag)) == 0) {
			exists = 1;
			break;
		}
	}
	voikkoFreeCstrArray(voikko_langs);

	return exists;
}

static EnchantDict *
voikko_provider_request_dict (EnchantProvider * me, const char *const tag)
{
	const char * voikko_error;

	if (!voikko_provider_dictionary_exists (NULL, tag)) {
		return NULL;
	}

	struct VoikkoHandle *voikko_handle = voikkoInit (&voikko_error, tag, NULL);
	if (voikko_handle == NULL) {
		enchant_provider_set_error (me, voikko_error);
		return NULL;
	}

	EnchantDict *dict = calloc (sizeof (EnchantDict), 1);
	if (dict == NULL)
		return NULL;
	dict->user_data = (void *)voikko_handle;
	dict->check = voikko_dict_check;
	dict->suggest = voikko_dict_suggest;

	return dict;
}

static void
voikko_provider_dispose (EnchantProvider * me)
{
	free (me);
}

static const char *
voikko_provider_identify (EnchantProvider * me _GL_UNUSED)
{
	return "voikko";
}

static const char *
voikko_provider_describe (EnchantProvider * me _GL_UNUSED)
{
	return "Voikko Provider";
}

EnchantProvider *init_enchant_provider (void);

EnchantProvider *
init_enchant_provider (void)
{
	EnchantProvider *provider = calloc (sizeof (EnchantProvider), 1);
	if (provider == NULL)
		return NULL;
	provider->dispose = voikko_provider_dispose;
	provider->request_dict = voikko_provider_request_dict;
	provider->dispose_dict = voikko_provider_dispose_dict;
	provider->dictionary_exists = voikko_provider_dictionary_exists;
	provider->identify = voikko_provider_identify;
	provider->describe = voikko_provider_describe;
	provider->list_dicts = voikko_provider_list_dicts;

	return provider;
}