-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
380 lines (359 loc) · 11.2 KB
/
main.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* File: main.c
* Author: Nikos Bafatakis A.E.M 2383
*!!!Gia perissoteres plirofories sxetika me thn ylopoihsh Anatrexte sthn Tekmiriwsh.!!!
* Created on 7 Ιανουαρίου 2015, 2:27 μμ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD 20
struct word {
char en[WORD];
char gr[WORD];
struct word *next;
} *head = NULL;
typedef int bool;
#define true 1
#define false 0
/*]
Dilwseis twn synarthsewn
*/
struct word *findWord();
struct word *deleteWord();
bool updateTerm();
int menu_select();
void printLex();
void translate_sentense();
struct word *insertTerm();
char *levenshtein();
void WriteBinaryFile();
struct word *ReadBinaryFile();
//H main tou programmatos mas.
int main(int argc, char** argv) {
printf("Welcome to the Dictionary!!\n\n");
int choice;
char s[2];
int c;
char eng[WORD];
char gr[WORD];
char key[WORD];
char sent[250];
head = ReadBinaryFile(head);
fflush(stdin);
while (true) {
choice = menu_select();
switch (choice) {
case 1:
printf("Give English Word:\n");
scanf("%s", eng);
printf("Give Greek Word:\n");
scanf("%s", gr);
if(findWord(eng,head)!=NULL){
printf("Sorry, this word allready exists in the Dictionary\n");
break;
}
head = insertTerm(eng, gr, head);
printf("\n\nPress any key to Return to Main Menu\n");
getchar();
break;
case 2:
printf("Give English or Greek Word to Update term:\n");
gets(key);
printf("Give English Word:\n");
gets(eng);
printf("Give Greek Word:\n");
gets(gr);
bool done = updateTerm(key, head, eng, gr);
if (done) printf("Word Updated!!!\n");
else printf("Error\n");
break;
case 3:
printf("Give English or Greek Word to Delete term:\n");
gets(key);
head = deleteWord(head, key);
printf("Word Deleted");
printf("\n\nPress any key to Return to Main Menu\n");
getchar();
break;
case 4:
printf("Translate:\n1.From Greek to English\n2.From English to Greek\n");
do {
printf("\nEnter your choice:\n");
gets(s);
c = atoi(s);
} while (c < 1 || c > 3);
bool eng;
if (c == 1) eng = true;
else eng = false;
printf("Give a Word to Translate:\n");
gets(key);
struct word *temp = findWord(key, head);
if (temp != NULL) {
printf("English Word=%s\n Greek Word=%s\n", temp->en, temp->gr);
} else {
printf("Closest word based on Levenshtein Distance\n");
char *s = levenshtein(key, eng, head);
printf("%s", s);
}
printf("\n\nPress any key to Return to Main Menu\n");
getchar();
break;
case 5:
printf("Translate:\n1.From Greek to English\n2.From English to Greek\n");
do {
printf("\nEnter your choice:\n");
gets(s);
c = atoi(s);
} while (c < 1 || c > 3);
switch (c) {
case 1:
printf("Give sentence to Translate:\n");
gets(sent);
translate_sentense(sent, false, head);
printf("\n\n");
break;
case 2:
printf("Give sentence to Translate:\n");
gets(sent);
translate_sentense(sent, true, head);
printf("\n\n");
break;
}
break;
case 6:
printLex(head);
break;
case 7:
printf("\nDo u want to save changes to Lexicon?(yes/no)\n");
gets(key);
if (strcmp(key, "yes") == 0) {
WriteBinaryFile(head);
}
exit(0);
}
}
}
/*H synartisi menu_display emfanizei to menu epilogwn sthn othonh gia na epilexei o xrhsths*/
int menu_select(void) {
fflush(stdin);
char s[2];
int c;
printf("1. New term:\n");
printf("2. Edit term:\n");
printf("3. Delete term:\n");
printf("4. Translate Word:\n");
printf("5. Translate sentence:\n");
printf("6. Print All words:\n");
printf("7. Exit.\n");
do {
printf("\nEnter your choice:\n");
gets(s);
c = atoi(s);
} while (c < 1 || c > 7);
return c;
}
//H synartisi ayth dexete mia opoiadipote lexh kai epistrefei ena struct me thn lexh ayth
struct word *findWord(char key[WORD], struct word *head) {
struct word *p = head;
while (p != NULL) {
if (strcmp(p->en, key) == 0 || strcmp(p->gr, key) == 0) {
return p;
} else {
p = p->next;
}
}
return NULL;
}
//h synarthsh ayth metafrazei mia oloklhrh protash
void translate_sentense(char sent[], bool eng, struct word *head) {
char translated[500] = "\0";
const char s[2] = " ";
char *token;
struct word *p;
if (eng) { //metafrash apo agglika se ellhnika
token = strtok(sent, s);
while (token != NULL) {
p = findWord(token, head);
if (p != NULL) {
strcat(translated, p->gr);
} else if (p == NULL) { //kathe lexh gia thn opoia den yparxei sto lexiko vrikete mia paromoia me thn apostash Leveshtein
char *lev = levenshtein(token, !eng, head); //h synarthsh levenshtein thelei !bool.
strcat(translated, lev);
}
strcat(translated, " ");
token = strtok(NULL, s);
}
} else { //metafrash apo ellhnika se agglika
token = strtok(sent, s);
while (token != NULL) {
p = findWord(token, head);
if (p != NULL) {
strcat(translated, p->en);
} else if (p == NULL) {
char *lev = levenshtein(token, !eng, head);
strcat(translated, lev);
}
strcat(translated, " ");
token = strtok(NULL, s);
}
}
printf("%s\n", translated);
}
//eisagwgi neas lexis
struct word * insertTerm(char enWord[WORD], char grWord[WORD], struct word * head) {
struct word *newWord, *p = head;
newWord = (struct word*) malloc(sizeof (struct word));
strcpy(newWord->en, enWord);
strcpy(newWord->gr, grWord);
if (head == NULL) {
newWord->next = head;
return newWord;
}
while (p->next != NULL) {
p = p->next;
}
newWord->next = p->next;
p->next = newWord;
return head;
}
//diagrafh lexis
struct word *deleteWord(struct word *head, char key[WORD]) {
struct word *p, *pp;
if (head == NULL) {
return NULL;
}
p = head->next;
if (strcmp(p->en, key) == 0 || strcmp(p->gr, key) == 0) {
free(head);
return (p);
}
pp = head;
while (p != NULL) {
if (strcmp(p->en, key) == 0 || strcmp(p->gr, key) == 0) {
pp->next = p->next;
free(p);
}
p = p->next;
pp = pp->next;
}
pp = head;
return head;
}
char *levenshtein(char wrd[], bool eng, struct word *head) { //!!!!Gia perissoteres plirofories deite tekmiriwsh selida 5.!!!
char *found[WORD];
int cost, i, minCost;
char c[WORD];
struct word *p = head;
if (eng) { //apo agglika se ellhnika
minCost = strlen(wrd);
while (p != NULL) { //diatrexete h lista
cost = 0;
strcpy(c, p->gr);
if (strlen(c) >= strlen(wrd)) { //elegxos poia lexh einai megalyterh
for (i = 0; i < strlen(wrd); i++) {
if ((c[i] == wrd[i]) == 0) {
cost++;
}
}
cost += strlen(c) - strlen(wrd); //syblirwsh kostous
} else if (strlen(wrd) > strlen(c)) {
for (i = 0; i < strlen(c); i++) {
if ((c[i] == wrd[i]) == 0) {
cost++;
}
}
cost += strlen(wrd) - strlen(c);
}
if (cost < minCost) { //ypologismos min Costous kai apothikeysh lexis
minCost = cost;
strcpy(found, p->en);
}
p = p->next;
}
return found;
} else { //apo ellhnika se agglika
minCost = strlen(wrd);
while (p != NULL) {
cost = 0;
strcpy(c, p->en);
if (strlen(c) >= strlen(wrd)) {
for (i = 0; i < strlen(wrd); i++) {
if (c[i] != wrd[i]) {
cost++;
}
}
cost += strlen(c) - strlen(wrd);
} else if (strlen(c) < strlen(wrd)) {
for (i = 0; i < strlen(c); i++) {
if (c[i] != wrd[i]) {
cost++;
}
}
cost = cost + (strlen(wrd) - strlen(c));
}
if (cost < minCost) {
minCost = cost;
strcpy(found, p->gr);
}
p = p->next;
}
return found;
}
}
//epexergasia lexis
bool updateTerm(char key[WORD], struct word *head, char enWord[WORD], char grWord[WORD]) {
struct word *p;
p = findWord(key, head);
if (p == NULL) {
return false;
}
strcpy(p->en, enWord);
strcpy(p->gr, grWord);
return true;
}
//ektypwsh listas
void printLex(struct word * head) {
printf("Printing Struct\n");
struct word *p = head;
while (p != NULL) {
printf("%s, %s\n", p->en, p->gr);
p = p->next;
}
}
//eggrafh arxeiou
void WriteBinaryFile(struct word * head) {
FILE *bin_file;
char name[50];
printf("\n Give filename(Default= lex.txt)\n");
scanf("%s", name);
if ((bin_file = fopen(name, "w")) != NULL) {
do {
fprintf(bin_file, "%s %s\n", head->en, head->gr);
head = head->next;
} while (head != NULL);
fclose(bin_file);
} else {
printf("Error opening the File");
}
}
//diavasma arxeiou
struct word * ReadBinaryFile(struct word * head) {
FILE *bin_file;
char name[50];
char en[WORD];
char gr[WORD];
if ((bin_file = fopen("lex.txt", "r+")) != NULL) {
while (feof(bin_file) == false) {
fscanf(bin_file, "%s", en);
fscanf(bin_file, " %s\n", gr);
// printf("%s %s\n", en, gr);
head = insertTerm(en, gr, head);
};
fclose(bin_file);
} else {
printf("Error opening the File Press Enter to Start a New Dictionary\n"); //to arxeio den vrethike, dimiourgia neou lexikou
getchar();
}
return head;
}