-
Notifications
You must be signed in to change notification settings - Fork 0
/
PositionsTest.cpp
301 lines (291 loc) · 8.35 KB
/
PositionsTest.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
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
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
typedef std::pair<char, int> tr;
using namespace std;
// A single state in our DFA, which represents an equivalence class.
struct State {
int len;
int link;
int first;
bool clone = false;
vector<tr> transitions;
vector<int> suffixreferences;
bool terminal = false;
int index;
void AddTransition(char c, int i)
{
transitions.push_back(tr(c, i));
}
// Returns the index of a state or -1 if no transition exists for c
int GetTransition(char c)
{
for (auto& t : transitions)
{
if (t.first == c)
{
return t.second;
}
}
return -1;
}
// Updates the transition through c to a new index i
void UpdateTransition(char c, int i)
{
for (auto& t : transitions)
{
if (t.first == c)
{
t.second = i;
return;
}
}
}
};
struct SuffixAutomaton {
bool suffixreferences = false;
vector<State> states;
// Returns the state at index i
State GetState(int i)
{
return states[i];
}
// Create a new state and return its index (requires t0 already initialized)
int AddState(int len)
{
State a;
a.len = len;
a.index = states.back().index + 1;
states.push_back(a);
return a.index;
}
// Populate each state with a vector of its children in the link tree
void ComputeSuffixReferences()
{
for (int i = 1; i < states.size(); i++)
{
states[states[i].link].suffixreferences.push_back(i);
}
suffixreferences = true;
}
SuffixAutomaton(string s) {
// Initial state t0 will be initialized as last
State l;
l.len = 0;
l.link = -1;
l.index = 0;
states.push_back(l);
int last = 0;
for (auto& c : s)
{
bool done = false;
// Create a new state for a new equivalence class
int cur = AddState(states[last].len + 1);
// Mark the ending position of the first occurrence of this state
states[cur].first = states[last].len;
// Keep following links until we find a transition through c
int linked = last;
int t = states[linked].GetTransition(c);
while (t == -1)
{
states[linked].AddTransition(c, states[cur].index);
if (states[linked].link != -1)
{
linked = states[linked].link;
t = states[linked].GetTransition(c);
}
else // We have climbed the link tree to the root
{
// Add cur as a child of the root in the link tree and
// process the next character
states[cur].link = 0;
last = cur;
done = true;
break;
}
}
if (done) continue;
// If we have reached here, we have found a state p
// such that p transitions through c to some state q at index t
int p = linked;
int q = t;
if (states[q].len == states[p].len + 1)
{
// Cur is a child of q in the link tree, process next character
states[cur].link = states[q].index;
last = cur;
continue;
}
// Cur is not a child of q in the link tree, we must create a new
// state that will be the parent of both q and cur in the link tree
int clone = AddState(states[p].len + 1);
states[clone].link = states[q].link;
states[clone].transitions = states[q].transitions;
states[clone].first = states[q].first;
states[clone].clone = true;
states[cur].link = states[clone].index;
states[q].link = states[clone].index;
// Updates transitions through c to q to match our new state
// TODO: Double check that p needs to be updated as well
linked = p;
while (t == q)
{
states[linked].UpdateTransition(c, clone);
linked = states[linked].link;
if (linked != -1)
{
t = states[linked].GetTransition(c);
}
else
{
break;
}
}
// We are finished, advance last to the new state and continue
last = cur;
}
// We now want to mark every terminal state. We start with last, as
// it is obviously a terminal state. By climbing the suffix links, we
// find the state that corresponds to the next largest suffix that
// is of a different equivalence class. This will be a terminal state
// as well. So on and so forth until we hit the root of the link tree.
states[last].terminal = true;
int link = states[last].link;
while (link != -1)
{
int linked = link;
states[linked].terminal = true;
link = states[linked].link;
}
}
// O(s) query to see if our source text contains a substring s
bool contains(string s)
{
int i = 0;
for (auto& c : s)
{
i = states[i].GetTransition(c);
if (i == -1)
{
return false;
}
}
return true;
}
// Returns the position of the first occurrence of a non-empty string s,
// or -1 if it does not occur
int first(string s)
{
int next = 0;
for (int i = 0; i < s.size(); i++)
{
next = states[next].GetTransition(s[i]);
if (next == -1) return -1;
}
return states[next].first - s.size() + 1;
}
// Return a vector of positions where a non-empty string s occurs
vector<int> positions(string s)
{
vector<int> p;
int sz = s.size();
if (!suffixreferences) ComputeSuffixReferences();
int next = 0;
for (int i = 0; i < sz; i++)
{
next = states[next].GetTransition(s[i]);
if (next == -1) return {};
}
// Traverse link tree down from first occurrence to find all others
vector<int> stack = {next};
while (stack.size() > 0)
{
next = stack.back();
stack.pop_back();
if (!states[next].clone) p.push_back(states[next].first - sz + 1);
for (auto& i : states[next].suffixreferences)
{
stack.push_back(i);
}
}
sort(p.begin(), p.end());
return p;
}
};
#include <fstream>
int main()
{
string current;
vector<pair<string, string>> source;
vector<vector<pair<string, int>>> search;
int o;
vector<vector<string>> results;
ifstream file ("positions.in");
if (file.is_open())
{
while (getline (file, current))
{ // Get the pair<title, body> for each source text
pair<string, string> src;
src.first = current;
getline(file, current);
src.second = current;
source.push_back(src);
file >> o; // Get the number of searches for this text
getline(file, current);
vector<pair<string, int>> schs;
for (int i = 0; i < o; i++)
{ // Get the pair<string, numoccurrences> for each search
int oo;
pair<string, int> sch;
getline(file, current);
sch.first = current;
file >> oo;
getline(file, current);
sch.second = oo;
schs.push_back(sch);
}
search.push_back(schs);
}
file.close();
}
for (int i = 0; i < source.size(); i++)
{
cout << "Constructing an automaton of size " << source[i].second.size() << " for " << source[i].first << "..." << endl;
SuffixAutomaton sa = SuffixAutomaton(source[i].second);
cout << "Computing suffix references for " << sa.states.size() << " states..." << endl;
for (int t = 0; t < search[i].size(); t++)
{
vector<int> positions = sa.positions(search[i][t].first);
string passed = "passed";
// Check that we got the correct number of positions
if (positions.size() != search[i][t].second) passed = "failed";
for (int j = 0; j < positions.size(); j++)// For each reported position:
{
for (int k = 0; k < search[i][t].first.size(); k++)
{ // Check that the position contains the search term
if (search[i][t].first[k] != source[i].second[positions[j]+k]) passed = "failed";
}
}
results.push_back({source[i].first, search[i][t].first, to_string(positions.size()), to_string(search[i][t].second), passed});
if (passed == "passed")
{
cout << "PASSED: Searching for \"" << search[i][t].first << "\" in " << source[i].first << " found " << positions.size() << " of " << search[i][t].second << " positions and all matched the substring." << endl;
}
else
{
cout << "FAILURE. Well that's embarassing. I swear it worked on my machine :)" << endl;
}
}
}
ofstream sr("positionsresults.csv");
if (sr.is_open())
{
sr << "Source Title:" << ",Search String:" << ",Found:" << ",Expected:" << ",Result:" << endl;
for (auto& x : results)
{
sr << x[0] << "," << x[1] << "," << x[2] << "," << x[3] << "," << x[4] << endl;
}
sr.close();
}
}