[go: up one dir, main page]

Menu

[94c440]: / src / suggestions.c  Maximize  Restore  History

Download this file

167 lines (135 with data), 5.3 kB

  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
/**
* Artha - Free cross-platform open thesaurus
* Copyright (C) 2009 Sundaram Ramaswamy, legends2k@yahoo.com
*
* Artha is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Artha 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Artha; if not, see <http://www.gnu.org/licenses/>.
*/
/*
Code suggestions.c
Dynamic loading of libenchant for spell checks using
GLib's GModule. Function prototypes are taken from
suggestions.h
*/
#include "suggestions.h"
gchar** suggestions_get(gchar *lemma)
{
gchar **suggestions = NULL;
gchar **valid_suggestions = NULL;
size_t suggestions_count = 0, i = 0, valid_count = 0;
GSList *str_list = NULL, *temp_list = NULL;
// check if Enchant broker and English dictionary are initialized
// also check if WordNet database files are opened
if(enchant_broker && enchant_dict && (1 == OpenDB))
{
if(enchant_dict_check(enchant_dict, lemma, -1))
{
suggestions = enchant_dict_suggest(enchant_dict, lemma, -1, &suggestions_count);
for(i = 0; i < suggestions_count; i++)
{
if(wni_request_nyms(suggestions[i], NULL, 0, FALSE))
{
str_list = g_slist_prepend(str_list, g_strdup(suggestions[i]));
++valid_count;
}
}
if(valid_count)
{
valid_suggestions = g_new0(gchar*, valid_count + 1);
for(temp_list = str_list; temp_list; temp_list = g_slist_next(temp_list))
valid_suggestions[--valid_count] = temp_list->data;
g_slist_free(str_list);
}
if(suggestions)
enchant_dict_free_string_list(enchant_dict, suggestions);
}
}
return valid_suggestions;
}
void find_dictionary(const char * const lang_tag, const char * const provider_name,
const char * const provider_desc,
const char * const provider_file,
void * user_data)
{
gchar *str_dict_tag = user_data;
if(str_dict_tag[0] == '\0')
if(g_str_has_prefix(lang_tag, DICT_ENGLISH_PREFIX))
{
g_snprintf(str_dict_tag, DICT_TAG_MAX_LENGTH, "%s", lang_tag);
G_PRINTF("Alternative dict '%s' selected!\n", lang_tag);
}
}
gboolean suggestions_init()
{
gboolean dict_initialized = FALSE;
gchar dict_tag[DICT_TAG_MAX_LENGTH] = DICT_TAG_ENGLISH;
if(g_module_supported() && (mod_enchant = g_module_open(ENCHANT_FILE, G_MODULE_BIND_LAZY)))
{
g_module_symbol(mod_enchant, FUNC_BROKER_INIT, (gpointer *) &enchant_broker_init);
g_module_symbol(mod_enchant, FUNC_BROKER_FREE, (gpointer *) &enchant_broker_free);
g_module_symbol(mod_enchant, FUNC_BROKER_LIST_DICTS, (gpointer *) &enchant_broker_list_dicts);
g_module_symbol(mod_enchant, FUNC_BROKER_DICT_EXISTS, (gpointer *) &enchant_broker_dict_exists);
g_module_symbol(mod_enchant, FUNC_BROKER_REQ_DICT, (gpointer *) &enchant_broker_request_dict);
g_module_symbol(mod_enchant, FUNC_BROKER_FREE_DICT, (gpointer *) &enchant_broker_free_dict);
g_module_symbol(mod_enchant, FUNC_DICT_CHECK, (gpointer *) &enchant_dict_check);
g_module_symbol(mod_enchant, FUNC_DICT_SUGGEST, (gpointer *) &enchant_dict_suggest);
g_module_symbol(mod_enchant, FUNC_DICT_FREE_STRINGS, (gpointer *) &enchant_dict_free_string_list);
// in older version of Enchant, enchant_dict_free_strings might be absent, they will have the
// now deprecated function enchant_dict_free_suggestions
if(NULL == enchant_dict_free_string_list)
g_module_symbol(mod_enchant, FUNC_DICT_FREE_SUGGESTS, (gpointer *) &enchant_dict_free_string_list);
// check if we have obtained the essential function pointers
if(NULL != enchant_broker_init && NULL != enchant_broker_free && NULL != enchant_broker_dict_exists &&
NULL != enchant_broker_request_dict && NULL != enchant_broker_free_dict && NULL != enchant_dict_check &&
NULL != enchant_dict_suggest && NULL != enchant_dict_free_string_list)
{
enchant_broker = enchant_broker_init();
if(enchant_broker)
{
if(!enchant_broker_dict_exists(enchant_broker, dict_tag) && enchant_broker_list_dicts)
{
dict_tag[0] = '\0';
G_PRINTF("Suggestions: Couldn't find dict 'en'. Looking for alternatives...\n");
enchant_broker_list_dicts(enchant_broker, find_dictionary, dict_tag);
}
if(dict_tag[0] != '\0')
{
enchant_dict = enchant_broker_request_dict(enchant_broker, dict_tag);
if(enchant_dict) dict_initialized = TRUE;
}
if(!dict_initialized)
{
enchant_broker_free(enchant_broker);
enchant_broker = NULL;
g_module_close(mod_enchant);
mod_enchant = NULL;
}
}
}
}
return dict_initialized;
}
gboolean suggestions_uninit()
{
gboolean free_success = TRUE;
if(enchant_dict)
enchant_broker_free_dict(enchant_broker, enchant_dict);
if(enchant_broker)
enchant_broker_free(enchant_broker);
enchant_dict = NULL;
enchant_broker = NULL;
if(mod_enchant)
free_success = g_module_close(mod_enchant);
mod_enchant = NULL;
return free_success;
}