-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.cpp
116 lines (95 loc) · 2.59 KB
/
train.cpp
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
#include<iostream>
#include<fstream>
using namespace std;
#define MAX 100
#define FOR(i,n) for(int i=0;i<n;++i)
// --- trie node
struct node
{
node* ptr[36];
char* eng_str; //don't use statically declared arrays since not every node will have a conversion
node( )
{
FOR(i,36) ptr[i]=NULL;
eng_str = NULL;
}
};
// --- Write trie to xml in dfs fashion
void write_trie(node* root, FILE* out, int spaces=0)
{
fprintf(out,"{");
if(root != NULL)
{
fprintf(out, "\n\"ptr\" : [");
FOR(i,36)
{
if( i > 0 && i < 36)
fprintf(out, ", ");
write_trie(root->ptr[i], out, spaces+2);
}
fprintf(out, "\n]");
if( root->eng_str != NULL)
{
fprintf(out, ", \n \"eng_str\" : \"%s\"", root->eng_str);
}
}
else
{
fprintf(out,"");
}
fprintf(out,"}");
}
//depth first
void print_trie(node* root, int n = -1)
{
if(root != NULL)
{
if ( n != -1)
printf("[debug] %c\n", n + 'a');
FOR(i, 36)
{
print_trie(root->ptr[i], i);
}
}
}
// --- Main function trains the trie
int main()
{
node* root = new node();
FILE* in = fopen("dictionary.txt","r+");
FILE* out = fopen("trie.json","w");
char new_str[MAX], eng_str[MAX], str[MAX];
if(!in)
{
std::cerr<<"Error opening the dictionary\n";
return 1;
}
while(fscanf(in, "%s", new_str)!=EOF)
{
fscanf(in, "%s", eng_str);
cout<<"[debug] "<<new_str<<", "<<eng_str<<endl;
int len = strlen(new_str), index;
node* cur = root;
FOR(i,len)
{
if(new_str[i] >= 'a' && new_str[i] <= 'z')
index = new_str[i]-'a';
else
index = 26 + new_str[i] - '0';
if((cur->ptr)[index]==NULL)
{
(cur->ptr)[index] = new node();
//cout<<"pointer was not found at "<<i<<" for "<<new_str<<endl;
//system("Pause");
}
cur = (cur->ptr)[index];
//cout<<"[debug] "<<cur<<endl;
}
cur->eng_str = new char[MAX];
strcpy(cur->eng_str, eng_str);
}
print_trie(root);
write_trie(root, out);
system("pause");
return 0;
}