Skip to content

Instantly share code, notes, and snippets.

@toanalien
Last active August 29, 2015 14:22
Show Gist options
  • Save toanalien/cf758b89f0128adff470 to your computer and use it in GitHub Desktop.
Save toanalien/cf758b89f0128adff470 to your computer and use it in GitHub Desktop.
extract phrases from dictionary file and add to hash table
/**
* Created by toanalien on 6/7/2015.
*/
// download dictionary at http://blackberryvietnam.net/threads/du-lieu-tu-dien-cho-ung-dung-ddict.897/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 29
struct SPharse
{
char *en, *vi;
struct SPharse *pNext;
};
typedef struct SPharse *Pharse;
typedef Pharse HashTable[MAX];
int HashFunc(char*);
void Init(HashTable);
void ReadFile(HashTable ht);
Pharse Add(HashTable, Pharse);
int Delete(HashTable, Pharse);
Pharse Search(HashTable, Pharse);
void Input(HashTable);
void Output(HashTable, char*);
void Print(HashTable);
void Output(HashTable ht, char *name)
{
FILE *dest_file;
Pharse q;
dest_file = fopen(name, "w+");
for (int i = 0; i < MAX; i++)
{
fprintf(dest_file, "Key %d \n", i);
q = ht[i];
while (q != NULL)
{
fprintf(dest_file, "\t%s\t%s", q->en, q->vi);
q = q->pNext;
}
fprintf(dest_file, "\n");
}
printf("Ghi file thanh cong !\n");
}
void Input(HashTable ht)
{
char *tmp = (char*)malloc(sizeof(char) * 100);
Pharse p = (Pharse)malloc(sizeof(struct SPharse));
printf("Nhap tu tieng anh: ");
fflush(stdin);
gets(tmp);
p->en = strdup(tmp);
printf("Nhap tu tieng viet: ");
fflush(stdin);
gets(tmp);
p->vi = strdup(tmp);
p->pNext = NULL;
Add(ht, p);
}
int Delete(HashTable ht, Pharse p)
{
int k = HashFunc(p->en);
Pharse q, r;
if (ht[k] == NULL)
{
printf("Khong tim thay key");
return 0;
}
if (ht[k]->en = p->en)
{
q = ht[k];
ht[k] = ht[k]->pNext;
free(q); // free memory of pharse
return 1;
}
else
{
q = ht[k];
r = ht[k]->pNext;
while (r != NULL)
{
if (r->en == p->en)
break;
q->pNext = r->pNext;
free(r);
return 1;
}
if (r == NULL)
{
printf("Khong tim thay key");
return 0;
}
}
}
int HashFunc(char* key)
{
int i, n = strlen(key);
int m = 0;
for (i = 0; i<n; i++)
{
m = (m * 29 + (key[i] - 'A')) % MAX;
}
return m;
}
void Init(HashTable ht)
{
for (int i = 0; i < MAX; i++)
ht[i] = NULL;
}
void ReadFile(HashTable ht)
{
FILE *src_file;
char s[300];
char *s1, *s2, *s3;
char en[300], vi[300];
src_file = fopen("av.dd", "r");
Pharse p;
p = (Pharse)malloc(sizeof(struct SPharse));
while (fgets(s, 300, src_file) != nullptr)
{
s1 = strstr(s, "##");
if (s1 != NULL)
{
strncpy(en, s, strlen(s) - strlen(s1));
en[strlen(s) - strlen(s1)] = '\0'; // get en pharses
s2 = strstr(s1, "|-");
if (s2 != NULL)
{
s3 = strstr(s2 + 3, "|"); // trim vi pharses after |
if (s3 != NULL)
{
strncpy(vi, s2 + 3, strlen(s2) - strlen(s3) - 3);
vi[strlen(s2) - strlen(s3) - 3] = '\0'; // get vi pharses
p->en = en;
p->vi = vi;
}
else
{
p->en = en;
strcpy(vi, s2 + 3);
p->vi = vi;
}
Add(ht, p);
}
}
}
printf("Doc File thanh cong!\n");
}
Pharse Search(HashTable ht, Pharse p)
{
Pharse q = (Pharse)malloc(sizeof(struct SPharse));
int k = HashFunc(p->en);
q = ht[k];
while (q != NULL)
{
if (strcmp(p->en, q->en) == 0)
return q;
else
q = q->pNext;
}
return NULL;
}
Pharse Add(HashTable ht, Pharse p)
{
int k = HashFunc(p->en);
Pharse q, r;
q = (Pharse)malloc(sizeof(struct SPharse));
q->pNext = NULL;
q->en = strdup(p->en);
q->vi = strdup(p->vi);
if (ht[k] == NULL)
ht[k] = q;
else
{
r = ht[k];
ht[k] = q;
ht[k]->pNext = r;
}
//cout << ht[k]->en<<"\t";
return ht[k];
}
void Print(HashTable ht)
{
printf("\nXuat thong tin key\n");
Pharse q;
for (int i = 0; i < MAX; i++)
{
printf("Key %d \n", i);
q = ht[i];
while (q != NULL)
{
printf("\t%s\t%s\n", q->en, q->vi);
q = q->pNext;
}
printf("\n");
}
}
int main()
{
HashTable ht;
Pharse p, r;
r = (Pharse)malloc(sizeof(struct SPharse));
Init(ht);
int c = 0;
char tmp[100];
printf("\n1. Doc tu file\n");
printf("2. Nhap tu\n");
printf("3. Tim kiem\n");
printf("4. Xoa\n");
printf("5. In ra man hinh\n");
printf("6. In ra file\n");
printf("0. Thoat\n");
do
{
printf("\nNhap lua chon cua ban: ");
do
{
scanf("%d", &c);
if ((c < 0) || (c>6))
printf("\nLua chon ko dung, nhap lai: ");
} while ((c < 0) || (c>6));
switch (c)
{
case 1:
ReadFile(ht);
break;
case 2:
Input(ht);
break;
case 3:
printf("Nhap tu can tim: ");
fflush(stdin);
gets(tmp);
r->en = strdup(tmp);
p = Search(ht, r);
if (p != NULL)
printf("\n%s\t%s\n", p->en, p->vi);
break;
case 4:
printf("Nhap tu can xoa: ");
fflush(stdin);
gets(tmp);
r->en = strdup(tmp);
if (Delete(ht, r))
printf("\nXoa thanh cong tu %s\n", tmp);
break;
case 5:
Print(ht);
break;
case 6:
printf("Nhap ten file de luu: ");
fflush(stdin);
gets(tmp);
Output(ht, tmp);
break;
}
} while (c != 0);
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment