forked from qhauict13/projectDict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
321 lines (281 loc) · 8.06 KB
/
model.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "model.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/**
* Check if file exist
*/
int check_file_exist(const char *file){
struct stat buffer;
int exist = stat(file, &buffer);
if(exist == 0)
return 1;
else
return 0;
}
/**
* load sample data to tree
*/
void load_sample_2_tree(BTA *tree, const char *sample_data){
if(tree == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
FILE *file;
file = fopen(sample_data, "r");
if(file == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
int n = 0;
char word[100], mean[100000];
char temp[100];
while(!feof(file)){
fgets(temp, 100, file);
if(temp[0] != '\t' && temp[0] != '\n'){
if(n==0){ //read the first word
n++;
strcpy(word, temp);
word[strlen(temp)-1] = '\0';
}
else {
mean[strlen(mean)-1] = '\0';
btins(tree, word, mean, strlen(mean)+1);
strcpy(mean, "");
n++;
strcpy(word, temp);
word[strlen(temp)-1] = '\0';
}
}
else strcat(mean, temp);
}
mean[strlen(mean)-1] = '\0';
btins(tree, word, mean, strlen(mean)+1); //put in tree
fclose(file);
}
/**
* load data from word tree to soundex tree
*/
void load_soundex_db(BTA *soundex_t, BTA *word_t){
if(word_t == NULL || soundex_t == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
// set tree word position
btpos(word_t, ZSTART);
while(1) {
char word[200];
char mean[200000];
int rsize, rsize_soundex;
if(btseln(word_t, word, mean, sizeof(mean), &rsize) == QNOKEY)
break; //break if there is no next key in word_t
char *soundex_str = soundex(word);
char series_word[200000];
if(btsel(soundex_t, soundex_str, series_word, sizeof(series_word), &rsize_soundex) != 0){
// soundex_str does not exist on soundex_tree
btins(soundex_t, soundex_str, word, strlen(word) + 1);
} else {
// soundex_str exists on soundex_tree
char separated[100] = ";"; //concat the words that have the same soundex
strcat(separated, word);
strcat(series_word, separated);
btupd(soundex_t, soundex_str, series_word, strlen(series_word) + 1); //update
}
free(soundex_str);
}
btpos(word_t, ZSTART);
}
/**
* soundex algorithm
*/
char *soundex(const char *in) {
int code[] =
{ 0,1,2,3,0,1,2,0,0,2,2,4,5,5,0,1,2,6,2,3,0,1,0,2,0,2 };
/* a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z */
char *key = malloc(sizeof(strlen(in)));
register char ch;
register int last;
register int count;
/* Set up default key, complete with trailing '0's */
strcpy(key, "Z000");
/* Advance to the first letter. If none present,
return default key */
while (*in != '\0' && !isalpha(*in))
++in;
if (*in == '\0')
return key;
/* Pull out the first letter, uppercase it, and
set up for main loop */
key[0] = toupper(*in);
last = code[key[0] - 'A'];
++in;
/* Scan rest of string, stop at end of string or
when the key is full */
for (count = 1; count < 4 && *in != '\0'; ++in) {
/* If non-alpha, ignore the character altogether */
if (isalpha(*in)) {
ch = tolower(*in);
/* Fold together adjacent letters sharing the same code */
if (last != code[ch - 'a']) {
last = code[ch - 'a'];
/* Ignore code==0 letters except as separators */
if (last != 0)
key[count++] = '0' + last;
}
}
}
return key;
}
/**
* add a word to tree
*/
int add_word_to_dict(ChData *data, char *word, char *mean){
if (word == NULL || mean == NULL || data == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
char mean_rev[2000000];
int rsize;
if(btsel(data->tree_word, word, mean_rev, sizeof(mean_rev), &rsize) != 0){
// word does not exist on tree_word
if(btins(data->tree_word, word, mean, strlen(mean) + 1) != 0) //error occurred
return 0;
char *soundex_str = soundex(word);
char series_word[200000];
int rsize_soundex;
if(btsel(data->tree_soundex, soundex_str, series_word, sizeof(series_word), &rsize_soundex) != 0){
// soundex_str does not exist on soundex_tree
if(btins(data->tree_soundex, soundex_str, word, strlen(word) + 1) != 0) //error occurred
return 0;
} else {
// soundex_str exists on soundex_tree
char separated[100] = ";";
strcat(separated, word);
strcat(series_word, separated);
if(btupd(data->tree_soundex, soundex_str, series_word, strlen(series_word) + 1) != 0) //error occurred
return 0;
}
free(soundex_str);
return 1; //successfully added
}
return 0;
}
/**
* edit a word's meaning
*/
int edit_meaning_from_dict(ChData *data, char *word, char *mean){
if (word == NULL || mean == NULL || data == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
char mean_rev[2000000];
int rsize;
if(btsel(data->tree_word, word, mean_rev, sizeof(mean_rev), &rsize) == 0){
// word exists on tree_word
if(btupd(data->tree_word, word, mean, strlen(mean) + 1) != 0)
return 0;
return 1; //successfully edited word
}
return 0;
}
/**
* detele a word
*/
int delete_word_from_dict(ChData *data, char *word){
if (word == NULL || data == NULL){
fprintf(stderr, "ERROR: NULL value %s:%d\n", __FILE__, __LINE__);
exit(1);
}
char mean_rev[2000000];
int rsize;
if(btsel(data->tree_word, word, mean_rev, sizeof(mean_rev), &rsize) == 0){
// word exists on tree_word
if(btdel(data->tree_word, word) != 0) //error
return 0;
char *soundex_str = soundex(word);
char series_word[200000];
int rsize;
if(btsel(data->tree_soundex, soundex_str, series_word, sizeof(series_word), &rsize) != 0){
// soundex_str does not exists on soundex_tree
fprintf(stderr, "ERROR: Word does not exist in soundex %s:%d\n", __FILE__, __LINE__);
return TRUE;
} else {
// soundex_str exists on soundex_tree
char str[1000];
int count = 0;
strcpy(str, series_word);
strcpy(series_word, "");
char *cut = strtok (str, ";");
while (cut != NULL) {
if(count == 0)
if(strcmp(cut, word) != 0)
strcat(series_word, cut);
char separated[100] = ";";
if(strcmp(cut, word) == 0){
cut = strtok (NULL, ";");
continue;
}
strcat(separated, cut);
strcat(series_word, separated);
cut = strtok (NULL, ";");
count++;
if (count == 50)
{
break;
}
}
if(count == 1)
if(btdel(data->tree_soundex, soundex_str) != 0){
return FALSE;
goto end;
}
if(btupd(data->tree_soundex, soundex_str, series_word, strlen(series_word) + 1) != 0)
return FALSE;
}
end:
free(soundex_str);
return TRUE;
}
return FALSE;
}
// Insert word into history tree view
void insert_2_history(ChData *data){
gtk_list_store_clear(data->list_store2);
GtkTreeIter iter;
node *temp;
temp = (node*)malloc(sizeof(node));
temp = data->stack;
while(temp != NULL){
//gtk_list_store_append(data->list_store2, &iter);
//gtk_list_store_set(data->list_store2, &iter, 0, temp->key, -1);
printf("%s\n", temp->key);
temp = temp->next;
}
printf("=======================\n");
free(temp);
}
/**
* Clear main window's entry
*/
void reset_entry(GtkWidget *Entry)
{
GtkEntryBuffer *buffer = gtk_entry_buffer_new (NULL, 0);
gtk_entry_set_buffer( (GtkEntry*) Entry, buffer);
}
/* Clear the TextView */
void reset_textview(GtkWidget *TextView)
{
GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
gtk_text_view_set_buffer( (GtkTextView*) TextView, buffer);
}
void status_dialog(GtkWindow *parent, gchar *message)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(parent,
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
gtk_window_set_title(GTK_WINDOW(dialog), "Status");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}