-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.c
75 lines (66 loc) · 1.6 KB
/
dictionary.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
#pragma once
#pragma execution_character_set("UTF-8")
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dictionary.h"
#include "trietree.h"
/*Initialize dictionary*/
DICTIONARY* dict_initialize()
{
TRIETREE* ptrees = NULL;
DICTIONARY* pdict = (DICTIONARY*)calloc(1, sizeof(DICTIONARY));
ptrees = (TRIETREE*)calloc(0xff,sizeof(TRIETREE));
if(pdict==NULL || ptrees==NULL)
{
fprintf(stderr, "Error allocating memory\n");
free(pdict);
free(ptrees);
return NULL;
}
pdict->dic_capacity = 0;
pdict->ptrees=ptrees;
return pdict;
}
void dict_from_file(DICTIONARY* pdict, const char* filename)
{
const int max_lenth = 150;//the maximum number of bytes for each word
uchar* buf;
FILE* fp;
unsigned long lenth;
fp = fopen(filename,"rt");
if(fp==NULL)
{
fprintf(stderr, "failed to open file %s\n", filename);
return;
}
buf=(uchar*)calloc(max_lenth+1, sizeof(uchar));
if(buf==NULL)
{
fprintf(stderr, "Error allocating memory\n");
return;
}
while(fgets((char*)buf, max_lenth+1, fp))
{
lenth = strlen((char*)buf);
buf[lenth-1] = 0;//remove '\n'
if(trietree_insert(pdict->ptrees, buf)) //insert a new word
++(pdict->dic_capacity); //increase the number of words in dictionary
}
fclose(fp);
free(buf);
}
//int dict_find(const DICTIONARY* pdict, const uchar* chars)
//{
// TRIENODE* ptr;
// if(pdict == NULL)
// return 0;
// ptr = trietree_find(pdict->ptrees, chars);
// return (ptr!=NULL && ptr->stat==STAT_WORD)? 1:0;
//}
int dict_match_prefix(const DICTIONARY* pdict, const uchar* chars)
{
if(pdict==NULL)
return 0;
return trietree_match_prefix(pdict->ptrees, chars);
}