-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
335 lines (303 loc) · 9.52 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* LXX, a project dedicated to find citations of the
* Septuagint in the Greek New Testament.
* This code is in the public domain
* and contributed by Zoltán Kovács <zoltan@geogebra.org>.
*/
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include "swmgr.h"
#include "swmodule.h"
#include "swtext.h"
#include "markupfiltmgr.h"
#include "utf8greekaccents.h"
using namespace std;
using namespace sword;
// The number of matching characters must be at least...
#define MIN_CHARS_MATCH 20
// This is hardcoded, it contains the index of the last verse
// in the New Testament in Sword.
#define MAX_VERSES 32360
string keys[MAX_VERSES];
string verses[MAX_VERSES];
long sStart;
long sEnd;
long lStart;
long lEnd;
// show percentage or not
int p = 0;
int previousPercentage = 0;
string processWord(string word) {
string rewritten;
for (int i = 0; i < word.length(); i += 2) {
auto c1 = static_cast<unsigned char>(word[i]);
auto c2 = static_cast<unsigned char>(word[i + 1]);
// keep care of special characters
if (c1 == 0x80) {
i--;
}
// keep only Greek characters
if (c1 == 0xCE || c1 == 0xCF) {
// force lowercase
if (c1 == 0xCE && c2 < 0xB1 &&
!(c2 >= 0xA0 && c2 <= 0xA9)) {
// letters before Pi (excluding Chi, Pi, Omega, Y, Sigma, Tau, Psi, Phi)
c2 += 0x20;
}
if (c1 == 0xCE && c2 >= 0xA0 && c2 <= 0xA9) {
// Chi, Pi, Omega, Y, Sigma, Tau, Psi, Phi
c1 = 0xCF;
c2 -= 0x20;
}
if (c1 == 0xCF && c2 < 0x80) {
c2 += 0x20;
}
rewritten.push_back(c1);
rewritten.push_back(c2);
}
}
return rewritten;
}
/* Remove double spaces. */
string printVerse(const string &verse) {
string out;
string word;
stringstream ss(verse);
while (ss >> word) {
out.append(word).append(" ");
}
// remove last space
out.pop_back();
return out;
}
/* Add double spaces. */
string processVerse(const string &verse) {
string out;
string word;
stringstream ss(verse);
while (ss >> word) {
word = processWord(word);
out.append(word);
// use double space to ease comparison later
out.append(" ");
}
return out;
}
void loadVerses(string ls, string Ls, string ss, string Ss) {
SWMgr library(new MarkupFilterMgr(FMT_PLAIN));
SWModule *lxx, *sblgnt;
lxx = library.getModule("LXX");
if (!lxx) {
cerr << "The SWORD module LXX is not installed" << "\n";
exit(1);
}
sblgnt = library.getModule("SBLGNT");
if (!sblgnt) {
cerr << "The SWORD module SBLGNT is not installed" << "\n";
exit(1);
}
if (ls.length() == 0) {
ls = "Genesis 1:1";
}
if (Ls.length() == 0) {
Ls = "Malachi 4:6";
}
if (ss.length() == 0) {
ss = "Matthew 1:1";
}
if (Ss.length() == 0) {
Ss = "Revelation 22:21";
}
lxx->setKey(ls.c_str());
lStart = lxx->getIndex(); // 4
lxx->setKey(Ls.c_str());
lEnd = lxx->getIndex(); // 24114
sblgnt->setKey(ss.c_str());
sStart = sblgnt->getIndex(); // 24118
sblgnt->setKey(Ss.c_str());
sEnd = sblgnt->getIndex(); // 32359
SWBuf lVerse, sVerse;
SWOptionFilter *filter = new UTF8GreekAccents();
filter->setOptionValue("off");
cerr << "Loading LXX..." << flush;
// Iterate on verses of LXX:
for (long l = lStart; l <= lEnd; ++l) {
lxx->setIndex(l);
lVerse = lxx->renderText();
// Some entries may be empty (e.g. book or chapter titles):
if (lVerse.size() > 0) {
keys[l] = lxx->getKeyText();
verses[l] = processVerse(lVerse.c_str());
}
}
cerr << " done" << "\n";
cerr << "Loading SBLGNT (without accents)..." << flush;
// Iterate on verses of NT:
for (long s = sStart; s <= sEnd; ++s) {
// Lookup NT text:
sblgnt->setIndex(s);
sVerse = sblgnt->renderText();
if (sVerse.size() > 0) {
// Remove accents from the NT text:
filter->processText(sVerse);
keys[s] = sblgnt->getKeyText();
verses[s] = processVerse(sVerse.c_str());
}
}
cerr << " done" << "\n";
}
int compareVerses(long v1i, long v2i) {
string v1 = verses[v1i];
string v2 = verses[v2i];
int v1l = v1.length() / 2;
for (int w = v1l; w >= MIN_CHARS_MATCH; --w) {
for (int s = 0; s <= v1l - w; ++s) {
string sub = v1.substr(static_cast<unsigned long>(s * 2),
static_cast<unsigned long>(w * 2));
int start_ok = false;
if (s == 0) {
start_ok = true;
} else {
string start = v1.substr(s * 2 - 2, 2);
if (start == " ") {
start_ok = true;
}
}
if (start_ok) {
string end = sub.substr(sub.length() - 2, 2);
if (end == " " || (s + w) * 2 == v1.length()) {
// Do comparison only for whole words
int found = v2.find(sub);
if (found != string::npos) {
sub = sub.substr(0, sub.length() - 2);
string out;
out = keys[v1i];
out.append(": ");
if (s > 0) {
out.append(v1.substr(0, s * 2));
}
out.append("**").append(sub).append("**");
if ((s + w) * 2 < v1.length()) {
out.append(" ").append(v1.substr((s + w) * 2));
}
out.append(" → ").append(keys[v2i]).append(": ");
if (found > 0) {
out.append(v2.substr(0, found));
}
out.append("**").append(sub).append("**");
if (found + w * 2 < v2.length()) {
out.append(" ").append(v2.substr(found + w * 2));
}
out.append("\n");
cout << printVerse(out) << "\n" << flush;
return true;
}
}
}
}
}
return false;
}
void showPercentage(double d) {
int di = d * 100;
if (previousPercentage != di) {
cerr << di << "% done" << "\n" << flush;
}
previousPercentage = di;
}
void compareVerses(long l1, long lN, long s1, long sN) {
for (long l = l1; l <= lN; ++l) {
if (p == 1) {
showPercentage((l - l1 + 0.0) / (lN - l1 + 1));
}
if (verses[l].length() > 0) {
for (long s = s1; s <= sN; ++s) {
compareVerses(l, s);
}
}
}
if (p == 1) {
showPercentage(1);
}
}
void compareVersesSwapLoops(long l1, long lN, long s1, long sN) {
for (long s = s1; s <= sN; ++s) {
if (p == 1) {
showPercentage((s - s1 + 0.0) / (sN - s1 + 1));
}
if (verses[s].length() > 0) {
for (long l = l1; l <= lN; ++l) {
compareVerses(l, s);
}
}
}
if (p == 1) {
showPercentage(1);
}
}
// @author Iain Hull (https://stackoverflow.com/a/868894/1044586)
class InputParser {
public:
InputParser(int &argc, char **argv) {
for (int i = 1; i < argc; ++i)
this->tokens.emplace_back(argv[i]);
}
const string &getCmdOption(const string &option) const {
vector<string>::const_iterator itr;
itr = find(this->tokens.cbegin(), this->tokens.cend(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
return *itr;
}
static const string empty_string;
return empty_string;
}
bool cmdOptionExists(const string &option) const {
return find(this->tokens.begin(), this->tokens.end(), option)
!= this->tokens.end();
}
private:
vector<string> tokens;
};
void showHelp(const string &executable) {
cout << "Usage: " << executable << " [options]\n";
cout << " where options can be:\n";
cout << " -h this help\n";
cout << " -l <verse> first LXX verse to lookup (e.g. \"Genesis 1:1\")\n";
cout << " -L <verse> last LXX verse to lookup\n";
cout << " -s <verse> first SBLGNT verse to lookup\n";
cout << " -S <verse> last SBLGNT verse to lookup\n";
cout << " -n sort the output by New Testament matches\n";
cout << " -p show process percentage\n";
}
int main(int argc, char **argv) {
InputParser input(argc, argv);
if (input.cmdOptionExists("-h")) {
showHelp(argv[0]);
exit(0);
}
string l, L, s, S;
if (input.cmdOptionExists("-l")) {
l = input.getCmdOption("-l");
}
if (input.cmdOptionExists("-L")) {
L = input.getCmdOption("-L");
}
if (input.cmdOptionExists("-s")) {
s = input.getCmdOption("-s");
}
if (input.cmdOptionExists("-S")) {
S = input.getCmdOption("-S");
}
if (input.cmdOptionExists("-p")) {
p = 1;
}
loadVerses(l, L, s, S);
// compareVerses(30,25605);
if (input.cmdOptionExists("-n")) {
compareVersesSwapLoops(lStart, lEnd, sStart, sEnd);
} else {
compareVerses(lStart, lEnd, sStart, sEnd);
}
return 0;
}