-
Notifications
You must be signed in to change notification settings - Fork 2
/
enumSubstring.cpp
140 lines (122 loc) · 2.95 KB
/
enumSubstring.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "cmdline.h"
#include "esa.hxx"
using namespace std;
int readFile(const char* fn, vector<int>& T){
FILE* fp = fopen(fn, "rb");
if (fp == NULL){
cerr << "cannot open " << fn << endl;
return -1;
}
if (fseek(fp, 0, SEEK_END) != 0){
cerr << "cannot fseek " << fn << endl;
fclose(fp);
return -1;
}
int n = ftell(fp);
rewind(fp);
if (n < 0){
cerr << "cannot ftell " << fn << endl;
fclose(fp);
return -1;
}
T.resize(n);
if (fread(&T[0], sizeof(unsigned char), (size_t)n, fp) != (size_t) n){
cerr << "fread error " << fn << endl;
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
int getID(const string& str, map<string, int>& word2id){
map<string, int>::const_iterator it = word2id.find(str);
if (it == word2id.end()){
int newID = (int)word2id.size();
word2id[str] = newID;
return newID;
} else {
return it->second;
}
}
void printSnipet(const vector<int>& T, const int beg, const int len, const vector<string>& id2word){
for (int i = 0; i < len; ++i){
int c = T[beg + i];
if (id2word.size() > 0){
cout << id2word[c] << " ";
} else {
cout << (isspace((char)c) ? '_' : (char)c);
}
}
}
int main(int argc, char* argv[]){
cmdline::parser p;
p.add("word", 'w', "word type");
if (!p.parse(argc, argv)){
cerr << p.error() << endl
<< p.usage() << endl;
return -1;
}
if (p.rest().size() > 0){
cerr << p.usage() << endl;
return -1;
}
vector<int> T;
bool isWord = p.exist("word");
map<string, int> word2id;
istreambuf_iterator<char> isit(cin);
istreambuf_iterator<char> end;
size_t origLen = 0;
if (isWord){
string word;
while (isit != end){
char c = *isit++;
if (!isspace(c)){
word += c;
} else if (word.size() > 0){
T.push_back(getID(word, word2id));
word = "";
}
++origLen;
}
if (word.size() > 0){
T.push_back(getID(word, word2id));
}
} else {
while (isit != end){
T.push_back((unsigned char)(*isit++));
}
origLen = T.size();
}
vector<string> id2word(word2id.size());
for (map<string, int>::const_iterator it = word2id.begin();
it != word2id.end(); ++it){
id2word[it->second] = it->first;
}
vector<int> SA(T.size());
vector<int> L (T.size());
vector<int> R (T.size());
vector<int> D (T.size());
int k = (isWord) ? (int)id2word.size() : 0x100;
if (isWord){
cerr << "origN:" << origLen << endl;
}
cerr << " n:" << T.size() << endl;
cerr << "alpha:" << k << endl;
int nodeNum = 0;
if (esaxx(T.begin(), SA.begin(),
L.begin(), R.begin(), D.begin(),
(int)T.size(), k, nodeNum) == -1){
return -1;
}
cerr << " node:" << nodeNum << endl;
for (int i = 0; i < nodeNum; ++i){
cout << i << "\t" << R[i] - L[i] << "\t" << D[i] << "\t";
printSnipet(T, SA[L[i]], D[i], id2word);
cout << endl;
}
return 0;
}