-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize.py
executable file
·1779 lines (1660 loc) · 66.6 KB
/
visualize.py
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# Several pieces of this code were taken from https://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/
import sqlite3, sys, pexpect, os, re
from sqlite3 import Error
bibref = None
def create_connection(db_file):
"""
Create a database connection to the SQLite database specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def latin_to_greek(latin):
greek = latin
# First, an utf-8 conversion:
greek = greek.replace("a", "α");
greek = greek.replace("b", "β");
greek = greek.replace("c", "ψ");
greek = greek.replace("d", "δ");
greek = greek.replace("e", "ε");
greek = greek.replace("f", "φ");
greek = greek.replace("g", "γ");
greek = greek.replace("h", "η");
greek = greek.replace("i", "ι");
greek = greek.replace("j", "ξ");
greek = greek.replace("k", "κ");
greek = greek.replace("l", "λ");
greek = greek.replace("m", "μ");
greek = greek.replace("n", "ν");
greek = greek.replace("o", "ο");
greek = greek.replace("p", "π");
greek = greek.replace("q", "ζ");
greek = greek.replace("r", "ρ");
greek = greek.replace("s", "σ");
greek = greek.replace("t", "τ");
greek = greek.replace("u", "θ");
greek = greek.replace("v", "ω");
greek = greek.replace("w", "ς"); # unused
greek = greek.replace("x", "χ");
greek = greek.replace("y", "υ");
return "\\selectlanguage{greek}" + greek + "\\selectlanguage{english}"
# This part is not used, because the Greek letters in math mode
# will be italicized automatically and that's too wide.
# ...Then, each utf-8 character will be converted into plain latex:
greek = greek.replace("α", "\\alpha ")
greek = greek.replace("β", "\\beta ")
greek = greek.replace("ψ", "\\psi ")
greek = greek.replace("δ", "\\delta ")
greek = greek.replace("ε", "\\varepsilon ")
greek = greek.replace("φ", "\\phi ")
greek = greek.replace("γ", "\\gamma ")
greek = greek.replace("η", "\\eta ")
greek = greek.replace("ι", "\\iota ")
greek = greek.replace("ξ", "\\xi ")
greek = greek.replace("κ", "\\kappa ")
greek = greek.replace("λ", "\\lambda ")
greek = greek.replace("μ", "\\mu ")
greek = greek.replace("ν", "\\nu ")
greek = greek.replace("ο", "o")
greek = greek.replace("π", "\\pi ")
greek = greek.replace("ζ", "\\zeta ")
greek = greek.replace("ρ", "\\rho ")
greek = greek.replace("σ", "\\sigma ")
greek = greek.replace("τ", "\\tau ")
greek = greek.replace("θ", "\\theta ")
greek = greek.replace("ω", "\\omega ")
greek = greek.replace("ς", "c") # unused
greek = greek.replace("χ", "\\chi ")
greek = greek.replace("υ", "\\upsilon ")
return "$" + greek + "$"
def psalms_report_latex(conn, method, data):
"""
Query all entries in the Psalms database rows and show them as a LaTeX table on stdout
:param conn: the Connection object
:param method: traditional, getrefs
:param data: authors or manual_nt_length (for traditional), pieces (for getrefs)
"""
cur = conn.cursor()
print ("\\documentclass{article}")
if method == "traditional" and data == "authors":
print ("\\usepackage{mathtools}")
print ("\\usepackage[dvipsnames]{xcolor}")
print ("\\newcommand\\Luke{{\\color{Lavender}\\bullet}\\mathllap{\\color{Lavender}\\circ}}")
print ("\\newcommand\\Paul{{\\color{YellowGreen}\\bullet}\\mathllap{\\color{YellowGreen}\\circ}}")
print ("\\newcommand\\Unknown{{\\color{Orchid}\\bullet}\\mathllap{\\color{Orchid}\\circ}}")
print ("\\newcommand\\Matthew{{\\color{Red}\\bullet}\\mathllap{\\color{Red}\\circ}}")
print ("\\newcommand\\Mark{{\\color{Cyan}\\bullet}\\mathllap{\\color{Cyan}\\circ}}")
print ("\\newcommand\\John{{\\color{GreenYellow}\\bullet}\\mathllap{\\color{GreenYellow}\\circ}}")
print ("\\newcommand\\Peter{{\\color{NavyBlue}\\bullet}\\mathllap{\\color{NavyBlue}\\circ}}")
if method == "traditional" and data == "manual_nt_length":
print ("\\usepackage{graphicx}")
f = open("manual_nt_length.csv", "w")
print ("\\begin{document}")
print ("\\centering")
print ("\\begin{table}")
if method == "traditional" and data == "manual_nt_length":
print ("\\scriptsize")
print ("\\begin{tabular}{r|cccccccccc}")
for i in range(0,10):
print ("&{\\bf", i+1, "}", end = '', sep = '')
print ("\\\\")
print ("\\hline")
quotations = 0
psalms = 0
for psalm in range(1,151):
if psalm % 10 == 1:
print ("{\\bf ", round(psalm/10), "}&", end = '', sep = '')
if method == "bibref":
global bibref
spawn_bibref()
bibref.timeout = 180
command = "getrefs SBLGNT LXX Psalms " + str(psalm)
bibref.sendline(command)
bibref.expect("Finished")
lines = bibref.before.decode('utf-8').splitlines()
no_lines = len(lines)
length_regex = re.compile(r'length=([0-9]+),')
# print(lines[no_lines - 1])
found = length_regex.search(lines[no_lines - 1])
maxlength0 = found.group()
maxlength1 = maxlength0.split("=")
maxlength2 = maxlength1[1]
maxlength3 = maxlength2.split(",")
maxlength = maxlength3[0]
bibref.timeout = 5
print ("\n", "% Psalm ", psalm, sep = '')
print (maxlength)
else:
cur.execute("SELECT q.ot_id, q.nt_id, q.nt_book, a.name" +
" FROM quotations_with_introduction q, authors a, books b " +
" WHERE q.psalm = " + str(psalm) +
" AND q.found_method = '" + method + "'" +
" AND a.name = b.author" +
" AND b.name = q.nt_book" +
" GROUP BY q.ot_id, q.nt_id" +
" ORDER BY q.ot_startpos, q.ot_id, b.number")
rows = cur.fetchall()
if len(rows) > 0:
print
print ("\n", "% Psalm ", psalm, sep = '')
old_ot_id = 0
for row in rows:
author = row[3]
ot_id = row[0]
nt_id = row[1]
if method == "getrefs" and data == 'pieces':
cur.execute("SELECT COUNT(*) FROM quotations q" +
" WHERE ot_id = " + str(ot_id) +
" AND nt_id = " + str(nt_id) +
" AND q.found_method = '" + method + "'");
info = cur.fetchall()
info = info[0][0]
if method == "traditional" and data == "manual_nt_length":
cur.execute("SELECT nt_length FROM quotations q" +
" WHERE ot_id = " + str(ot_id) +
" AND nt_id = " + str(nt_id) +
" AND q.found_method = 'manual'");
info = cur.fetchall()
number_str = str(info[0][0])
f.write(number_str + "\n")
info = "\\scalebox{.6}[1.0]{" + number_str + "}"
if old_ot_id > 0 and ot_id != old_ot_id:
print(",", sep = '', end = '')
else:
if method == "traditional" and data == "manual_nt_length":
if old_ot_id != 0:
info = "\\," + info
if data != 'authors':
print (info, sep='', end='')
else:
print ("$\\", author, "$", sep = '', end = '')
old_ot_id = ot_id
print("%", row)
quotations += 1
psalms += 1
if psalm % 10 == 0:
print ("\\\\")
else:
print ("&", end = '', sep = '')
print ("%", quotations, "quotations in", psalms, "psalms")
print ("\\end{tabular}")
print ("\\end{table}")
print ("\\end{document}")
if method == "traditional" and data == "manual_nt_length":
f.close()
def nt_report_ppm(conn, book, book_length, ppm_rows, ppm_columns, mode):
"""
Query all entries from a New Testament book and show them as PPM data in the file book-mode.ppm
:param conn: the Connection object
:param book: NT book name
:param book_length: NT book length in characters
:param ppm_rows: number of pixel rows in PPM output
:param ppm_columns: number of pixel columns in PPM output
"""
f = open(book + "-" + mode + ".ppm", "w")
cur = conn.cursor()
book_letters = [0] * book_length
cur.execute("SELECT q.nt_startpos, q.nt_length, q.ot_book, q.ot_passage, q.nt_passage" +
" FROM quotations q, quotations_classifications qc " +
" WHERE qc.classification = 'quotation'" +
" AND qc.quotation_ot_id = q.ot_id" +
" AND qc.quotation_nt_id = q.nt_id" +
" AND q.found_method = 'manual'" +
" AND q.nt_book = '" + book + "'" +
" ORDER BY q.nt_startpos")
rows = cur.fetchall()
q = 0
f.write("P3\n")
f.write(f"# Report on {book}, mode {mode}\n")
# Meaning of entries for the various modes:
# manual: 0: no quotation, 1: one quotation, 2: two quotations
# getrefs:
# 0: no quotation
# 1: first quotation without getrefs-match
# 2: second quotation without getrefs-match
# 3: getrefs incorrectly identified some characters
# 4: first quotation with getrefs-match
# 5: second quotation with getrefs-match
# 7: first quotation with two getrefs-matches
# TODO: add repetition
# FIXME: This system should be improved. Instead of numbers, flags should be used first.
for row in rows:
start = row[0]
length = row[1]
ot_book = row[2]
ot_passage = row[3]
nt_passage = row[4]
print("start =", start, "length =", length, f"({ot_passage} -> {nt_passage})")
for l in range(length):
book_letters[start+l] += 1
q += 1
if mode == "getrefs":
cur.execute("SELECT q.nt_startpos, q.nt_length, q.ot_book, q.ot_passage, q.nt_passage" +
" FROM quotations_with_introduction q " +
" WHERE q.found_method = 'getrefs'" +
" AND q.nt_book = '" + book + "'" +
" ORDER BY q.nt_startpos")
rows2 = cur.fetchall()
for row2 in rows2:
start = row2[0]
length = row2[1]
for l in range(length):
book_letters[start+l] += 3
r = 0
f.write(f"{ppm_columns} {ppm_rows}\n15\n")
for l in range(book_length):
info = book_letters[l]
if info == 0:
f.write("7 7 7\n") # no quotation at that position
else:
r += 1
if info == 1:
f.write("7 0 0\n") # 1. quotation
if info == 2:
f.write("0 7 0\n") # 2. quotation
if info == 3:
f.write("0 0 0\n") # false identification
r -= 1
if info == 4:
f.write("15 0 0\n") # 1. quotation with getrefs-match
if info == 5:
f.write("0 15 0\n") # 2. quotation with getrefs-match
if info == 7:
f.write("15 0 15\n") # 1. quotation with two getrefs-matches
if info == 6 or info >= 8:
print("Warning, ambiguous situation: position",l,"modesum",info)
f.write("15 15 0\n") # TODO: investigate this situation
# exit(1)
for l in range(ppm_rows*ppm_columns-book_length):
f.write("15 15 15\n") # empty cell
print(r, "characters of", q, "manually verified quotations out of", book_length, f"({100*r/book_length:.3g}%)")
f.close()
def statements(conn, linebreaks=True):
"""
Query all entries and print them as statements on stdout
:param conn: the Connection object
"""
global bibref
spawn_bibref()
cur = conn.cursor()
cur.execute("SELECT q.nt_quotation_id, q.nt_book" +
" FROM quotations_with_introduction q, books b" +
" WHERE q.found_method = 'manual'" +
" AND q.ot_startpos IS NOT NULL" +
" AND q.nt_startpos IS NOT NULL" +
" AND q.nt_book = b.name" +
" GROUP BY q.nt_quotation_id" +
" ORDER BY b.number, q.nt_startpos")
rows = cur.fetchall()
q = 1
for row in rows:
nt_quotation_id, nt_book = row
cur.execute("SELECT min(q.nt_startpos), q.nt_passage, q.nt_length, q.ot_id, q.nt_id " +
" FROM quotations_with_introduction q" +
" WHERE q.found_method = 'manual'" +
" AND q.nt_quotation_id = " + str(nt_quotation_id))
rows10 = cur.fetchall()
start, nt_passage_start, length, ot_id, nt_id = rows10[0]
cur.execute("SELECT max(nt_startpos+nt_length-1), nt_passage " +
" FROM clasps" +
" WHERE nt_quotation_id = " + str(nt_quotation_id))
rows11 = cur.fetchall()
end, nt_passage_end = rows11[0]
# Use some tricks to build nt_passage...
if nt_passage_end != None:
nt_passage_start_words = nt_passage_start.split(" ")
nt_passage_end_words = nt_passage_end.split(" ")
nt_passage = nt_passage_start_words[0] + " " + \
nt_passage_start_words[1] + " " + nt_passage_start_words[2]
nt_passage_end_words_last = nt_passage_end_words[len(nt_passage_end_words) - 1]
if nt_passage_start_words[2] != nt_passage_end_words_last:
nt_passage += " " + nt_passage_end_words_last
nt_passage += f" ({start}-{end})"
else:
end = start + length - 1
nt_passage = nt_passage_start + f" ({start}-{end})"
statement = f"Statement q{q}_{nt_quotation_id} connects";
if linebreaks:
statement += "\n"
statement += f" {nt_passage} with"
if linebreaks:
statement += "\n"
cur.execute("SELECT q.ot_passage, ot_startpos, ot_length" +
" FROM quotations_with_introduction q, books b" +
" WHERE q.nt_quotation_id = " + str(nt_quotation_id) +
" AND q.found_method = 'manual'" +
" AND q.ot_book = b.name" +
" ORDER BY b.number, q.ot_startpos")
rows12 = cur.fetchall()
ot_no = 0
for row12 in rows12:
ot_passage, ot_startpos, ot_length = row12
if ot_no > 0:
statement += " and"
if linebreaks:
statement += "\n"
statement += f" {ot_passage}"
if ot_startpos != None:
ot_end = int(ot_startpos)+int(ot_length)-1
statement += f" ({ot_startpos}-{ot_end})"
ot_no += 1
statement += " based on"
cur.execute("SELECT nt_passage, nt_startpos, nt_endpos" +
" FROM nt_quotation_introductions" +
" WHERE nt_quotation_id = " + str(nt_quotation_id) +
" ORDER BY nt_startpos")
rows2 = cur.fetchall()
intro_no = 0
if len(rows2) > 0:
for row2 in rows2:
intro_no += 1
if intro_no > 1:
statement += " and"
intro_passage, intro_startpos, intro_endpos = row2
intro_passage1 = intro_passage.split(" ")
if linebreaks:
statement += "\n "
statement += f" introduction {intro_passage1[2]} {intro_passage1[3]} ({intro_startpos}-{intro_endpos})"
command = "lookup1 " + intro_passage
bibref.sendline(command)
bibref.expect("Stored internally as (\\w+).")
form = bibref.match.groups()
statement += f" a-y form {form[0].decode('utf-8')}"
cur.execute("SELECT source_given, as_it_is_written" +
" FROM quotations_properties qp" +
f" WHERE qp.quotation_ot_id = {ot_id}" +
f" AND qp.quotation_nt_id = {nt_id}")
rows21 = cur.fetchall()
if len(rows21) > 0:
source_given, as_it_is_written = rows21[0]
# Work around an issue in the database: If there are multiple introductions,
# it is usually the first one which contains the source_given and as_it_is_written information,
# except for certain items.
if intro_no > 1:
as_it_is_written = False
if intro_no > 1 and not nt_quotation_id == 21134:
source_given = False
if nt_quotation_id == 1033:
if intro_no == 1:
as_it_is_written = "γαρ"
if intro_no == 2:
as_it_is_written = "καθως γεγραπται"
if nt_quotation_id == 1251:
if intro_no == 1:
as_it_is_written = "διο"
source_given = False
if intro_no == 2:
as_it_is_written = "λεγει"
source_given = "κυριος"
if nt_quotation_id == 1281:
as_it_is_written = "και παλιν"
source_given = False
if nt_quotation_id == 24042:
if intro_no == 1:
as_it_is_written = "ειποντα"
if intro_no == 2:
as_it_is_written = "και παλιν"
if nt_quotation_id == 21036:
if intro_no == 1:
as_it_is_written = "ο γαρ ειπων"
if intro_no == 2:
as_it_is_written = "ειπεν και"
# End of workaround.
if as_it_is_written or source_given:
statement += " that"
if as_it_is_written:
if linebreaks:
statement += "\n "
statement += f" declares a quotation with '{as_it_is_written}'"
if source_given:
if as_it_is_written:
statement += " and"
if linebreaks:
statement += "\n "
statement += f" identifies the source with '{source_given}'"
# Find a bound for all clasps...
cur.execute("SELECT min(nt_startpos), max(nt_startpos+nt_length-1)" +
" FROM clasps" +
" WHERE nt_quotation_id = " + str(nt_quotation_id))
rows2 = cur.fetchall()
clasp_no = 0
if len(rows2) > 0 and rows2[0][0] != None: # and rows2[0][1] != None (automatic)
nt_startpos_min, nt_startpos_max = rows2[0]
clasps_length = nt_startpos_max - nt_startpos_min + 1
letters = [0] * clasps_length
# Process each clasp...
cur.execute("SELECT ot_passage, nt_passage, ot_startpos, ot_length, nt_startpos, nt_length" +
" FROM clasps" +
" WHERE nt_quotation_id = " + str(nt_quotation_id) +
" ORDER BY nt_startpos")
rows3 = cur.fetchall()
if len(rows3) > 0:
for row3 in rows3:
clasp_no += 1
if intro_no > 0 or clasp_no > 1:
if clasp_no == 1:
statement += " moreover"
else:
statement += " and"
clasp_ot_passage, clasp_nt_passage, clasp_ot_startpos, clasp_ot_length, clasp_nt_startpos, clasp_nt_length = row3
clasp_nt_passage1 = clasp_nt_passage.split(" ")
if linebreaks:
statement += "\n "
statement += " fragment"
if len(clasp_nt_passage1) >= 4:
statement += f" {clasp_nt_passage1[2]} {clasp_nt_passage1[3]}"
else:
statement += f" {clasp_nt_passage1[2]}"
statement += f" ({clasp_nt_startpos}-{clasp_nt_startpos+clasp_nt_length-1}, length {clasp_nt_length})"
for i in range(clasp_nt_length):
letters[clasp_nt_startpos - nt_startpos_min + i] += 1
command = "lookup1 " + clasp_nt_passage
bibref.sendline(command)
bibref.expect("Stored internally as (\\w+).")
form = bibref.match.groups()
statement += f" a-y form {form[0].decode('utf-8')}"
if linebreaks:
statement += "\n "
statement += f" matches {clasp_ot_passage}"
statement += f" ({clasp_ot_startpos}-{clasp_ot_startpos+clasp_ot_length-1}, length {clasp_ot_length})"
command = "lookup2 " + clasp_ot_passage
bibref.sendline(command)
bibref.expect("Stored internally as (\\w+).")
form = bibref.match.groups()
statement += f" a-y form {form[0].decode('utf-8')}"
command = "jaccard12"
bibref.sendline(command)
bibref.expect("Jaccard distance is ([0-9]+\\.[0-9]+).")
jaccard12 = bibref.match.groups()
jaccard = float(jaccard12[0]) * 100
if linebreaks:
statement += "\n "
if jaccard == 0:
statement += " verbatim"
else:
statement += f" differing by {jaccard:4.2f}%"
if clasp_no > 0:
# Report covering...
covering = 0
for i in range(clasps_length):
if letters[i] > 0:
covering += 1
covering *= 100
covering /= clasps_length
if linebreaks:
statement += "\n "
statement += f" providing an overall cover of {covering:4.2f}%"
if intro_no + clasp_no == 0:
if linebreaks:
statement += "\n "
statement += " no evidence"
# TODO: Indicate method...
statement += "."
if linebreaks:
statement += "\n"
print(statement)
q = q + 1
def nt_report_latex(conn, book):
"""
Query all entries from a New Testament book and show them as a LaTeX table on stdout
:param conn: the Connection object
:param book: NT book name
"""
print ("\\documentclass{article}")
print ("\\usepackage{geometry}")
print ("\\geometry{a4paper,margin=1.5cm}")
print ("\\begin{document}")
print ("\\thispagestyle{empty}")
print ("\\begin{table}")
print ("\\centering")
print ("\\begin{tabular}{rrlc}")
print (f"&&&{{\\bf getrefs}}\\\\")
print (f"{{\\bf No.}}&{{\\bf {book} passage}}&{{\\bf OT passage}}&{{\\bf chunks}}\\\\")
print ("\\hline")
cur = conn.cursor()
cur.execute("SELECT q.nt_startpos, q.nt_length, q.ot_book, q.ot_passage, q.nt_passage, q.ot_id, q.nt_id" +
" FROM quotations_with_introduction q" +
" WHERE q.found_method = 'manual'" +
" AND q.ot_startpos IS NOT NULL" +
" AND q.nt_book = '" + book + "'" +
" ORDER BY q.nt_startpos")
rows = cur.fetchall()
q = 1
f = open("nt-manual_" + book + "_length.csv", "w")
for row in rows:
start = row[0]
length = row[1]
f.write(str(length) + "\n")
ot_book = row[2]
ot_passage = row[3].replace("_", " ")
nt_passage = row[4]
# Remove book name from the beginning:
nt_passage = nt_passage[(nt_passage.find(book) + len(book)):]
ot_id = row[5]
nt_id = row[6]
cur.execute("SELECT COUNT(*) from quotations q" +
" WHERE q.ot_id = " + str(ot_id) +
" AND q.nt_id = " + str(nt_id) +
" AND q.found_method = 'getrefs'")
rows2 = cur.fetchall()
chunks = rows2[0][0]
print(f"{q}&{nt_passage}&{ot_passage}&{chunks}\\\\")
q = q + 1
print ("\\end{tabular}")
print ("\\end{table}")
print ("\\end{document}")
f.close()
def ot_report_latex(conn, book):
"""
Query all entries from an Old Testament book and show them as a LaTeX table on stdout
:param conn: the Connection object
:param book: OT book name
"""
print ("\\documentclass{article}")
print ("\\usepackage{geometry}")
print ("\\geometry{a4paper,margin=1.5cm}")
print ("\\begin{document}")
print ("\\thispagestyle{empty}")
print ("\\begin{table}")
print ("\\centering")
print ("\\begin{tabular}{rrlc}")
print (f"&&&{{\\bf getrefs}}\\\\")
print (f"{{\\bf No.}}&{{\\bf {book} passage}}&{{\\bf NT passage}}&{{\\bf chunks}}\\\\")
print ("\\hline")
cur = conn.cursor()
cur.execute("SELECT q.ot_startpos, q.ot_length, q.nt_book, q.nt_passage, q.ot_passage, q.ot_id, q.nt_id" +
" FROM quotations_with_introduction q" +
" WHERE q.found_method = 'manual'" +
" AND q.ot_startpos IS NOT NULL" +
" AND q.ot_book = '" + book + "'" +
" ORDER BY q.ot_startpos")
rows = cur.fetchall()
q = 1
f = open("ot-manual_" + book + "_length.csv", "w")
for row in rows:
start = row[0]
length = row[1]
f.write(str(length) + "\n")
nt_book = row[2]
nt_passage = row[3].replace("_", " ")
ot_passage = row[4]
# Remove book name from the beginning:
ot_passage = ot_passage[(ot_passage.find(book) + len(book)):]
ot_id = row[5]
nt_id = row[6]
cur.execute("SELECT COUNT(*) from quotations q" +
" WHERE q.ot_id = " + str(ot_id) +
" AND q.nt_id = " + str(nt_id) +
" AND q.found_method = 'getrefs'")
rows2 = cur.fetchall()
chunks = rows2[0][0]
print(f"{q}&{ot_passage}&{nt_passage}&{chunks}\\\\")
q = q + 1
print ("\\end{tabular}")
print ("\\end{table}")
print ("\\end{document}")
f.close()
def nt_frequencies_csv(conn):
"""
Query all New Testament books and show them as a CSV table
:param conn: the Connection object
"""
f = open("nt_frequencies.csv", "w")
f.write("Book,No. of quotations\n")
cur = conn.cursor()
cur.execute("SELECT name, COUNT(*) FROM" +
" (SELECT b.number as number, b.name as name" +
" FROM quotations_with_introduction q, books b " +
" WHERE q.nt_book = b.name" +
" GROUP BY b.number, q.ot_id, q.nt_id" +
" ORDER BY b.number)" +
" GROUP BY name ORDER BY number")
rows = cur.fetchall()
total = 0
for row in rows:
book = row[0]
frequency = row[1]
f.write(book + "," + str(frequency) + "\n")
total += frequency
f.write("Total," + str(total) + "\n")
f.close()
def ot_frequencies_csv(conn):
"""
Query all Old Testament books and show them as a CSV table
:param conn: the Connection object
"""
f = open("ot_frequencies.csv", "w")
f.write("Book,No. of quotations\n")
cur = conn.cursor()
cur.execute("SELECT name, COUNT(*) FROM" +
" (SELECT b.number as number, b.name as name" +
" FROM quotations_with_introduction q, books b " +
" WHERE q.ot_book = b.name" +
" GROUP BY b.number, q.ot_id" +
" ORDER BY b.number)" +
" GROUP BY name ORDER BY number")
rows = cur.fetchall()
total = 0
for row in rows:
book = row[0]
frequency = row[1]
f.write(book + "," + str(frequency) + "\n")
total += frequency
f.write("Total," + str(total) + "\n")
f.close()
def ot_nt_graphviz(conn, lang=None):
"""
Show all connections in a GraphViz diagram
:param conn: the Connection object
:param lang: use language lang
"""
fontsize = 24
if lang == "hu":
f = open("ot_nt-hu.dot", "w")
else:
if lang == "en":
f = open("ot_nt-en.dot", "w")
else:
f = open("ot_nt.dot", "w")
fontsize = 14
f.write("digraph G {\n")
f.write(" start=\"random1\";\n") # ensure deterministic output
f.write(" outputorder=\"edgesfirst\";\n")
f.write(f" node [style=filled, color=cyan, fontsize=\"{fontsize}pt\"];\n")
cur = conn.cursor()
# NT books that contain quotations:
cur.execute("SELECT DISTINCT q.nt_book " +
" FROM quotations_with_introduction q, books b" +
" WHERE q.nt_book = b.name" +
" ORDER BY b.number")
rows = cur.fetchall()
for row in rows:
# The following lines are the same in all 4 cases, TODO: unify
book = row[0]
pos = graphviz_positions[book]
label = book
if lang == "hu":
label = books_translated_hu[book]
if lang == "en":
label = books_translated_en[book]
f.write(f" {book} [label=\"{label}\", pos=\"{pos}\"];\n")
# NT books that do not contain quotations:
cur.execute("SELECT name, number FROM books WHERE number > 100 EXCEPT SELECT DISTINCT q.nt_book, b.number" +
" FROM quotations_with_introduction_manual q, books b" +
" WHERE q.nt_book = b.name" +
" ORDER BY number")
rows = cur.fetchall()
for row in rows:
book = row[0]
pos = graphviz_positions[book]
label = book
if lang == "hu":
label = books_translated_hu[book]
if lang == "en":
label = books_translated_en[book]
f.write(f" {book} [label=\"{label}\", style=filled, color=cyan3, pos=\"{pos}\"];\n")
# OT books that contain quotations:
cur.execute("SELECT DISTINCT q.ot_book " +
" FROM quotations_with_introduction_manual q, books b" +
" WHERE q.ot_book = b.name" +
" ORDER BY b.number")
rows = cur.fetchall()
for row in rows:
book = row[0]
pos = graphviz_positions[book]
label = book
if lang == "hu":
label = books_translated_hu[book]
if lang == "en":
label = books_translated_en[book]
f.write(f" {book} [label=\"{label}\", style=filled, color=yellow, pos=\"{pos}\"];\n")
# OT books that do not contain quotations:
cur.execute("SELECT name, number FROM books WHERE number < 100 EXCEPT SELECT DISTINCT q.ot_book, b.number" +
" FROM quotations_with_introduction_manual q, books b" +
" WHERE q.ot_book = b.name" +
" ORDER BY number")
rows = cur.fetchall()
for row in rows:
book = row[0]
pos = graphviz_positions[book]
label = book
if lang == "hu":
label = books_translated_hu[book]
if lang == "en":
label = books_translated_en[book]
f.write(f" {book} [label=\"{label}\", style=filled, color=yellow3, pos=\"{pos}\"];\n")
cur.execute("SELECT q.ot_book, q.nt_book, COUNT(*), b1.number, b2.number " +
" FROM quotations_with_introduction_manual q, books b1, books b2" +
" WHERE b1.name = q.ot_book AND b2.name = q.nt_book"
" GROUP BY q.ot_book, q.nt_book" +
" ORDER BY b2.number, b1.number")
rows = cur.fetchall()
for row in rows:
ot_book, nt_book, c = row[0],row[1],row[2]
if c == 1:
c = " " # do not label single connections
f.write(f" {nt_book}->{ot_book} [labeljust=\"r\", label=\"{c}\", style=\"dotted\"];\n")
f.write("}\n")
f.close()
def spawn_bibref():
global bibref
if bibref is not None:
return
sys.stderr.write("Waiting for bibref's full startup...\n")
for subdir, dirs, files in os.walk('../..'):
for file in files:
fullpath = os.path.join(subdir, file)
if os.access(fullpath, os.X_OK) and file == "bibref":
sys.stderr.write(f"Spawning {fullpath}...\n");
bibref = pexpect.spawn(fullpath + " -a")
bibref.expect("Done loading books of SBLGNT.")
bibref.timeout = 5
return
sys.stderr.write("Unsuccessful\n");
exit(1)
def nt_jaccard_csv(conn, nt_book):
"""
Collect all quotations from an NT book that are obtained manually and
determine the Jaccard distances to the OT passages in a CSV file
:param conn: the Connection object
:param nt_book: NT book name
"""
global bibref
cur = conn.cursor()
cur.execute("SELECT q.ot_passage, q.nt_passage" +
" FROM quotations_with_introduction q" +
" WHERE q.found_method = 'manual'" +
" AND q.nt_book = '" + nt_book + "'" +
" AND INSTR(q.ot_passage, 'LXX') > 0" +
" ORDER BY q.nt_startpos")
rows = cur.fetchall()
spawn_bibref()
f = open("nt-jaccard_" + nt_book + ".csv", "w")
g = open("nt-jaccard_" + nt_book + ".txt", "w")
r = 0
for row in rows:
ot_passage = row[0]
nt_passage = row[1]
command1 = "lookup1 " + ot_passage
bibref.sendline(command1)
bibref.expect("Stored internally as \\w.")
command2 = "lookup2 " + nt_passage
bibref.sendline(command2)
bibref.expect("Stored internally as \\w.")
# command3 = "compare12"
command3 = "jaccard12"
bibref.sendline(command3)
# bibref.expect("difference = ([0-9]+\.[0-9]+).")
bibref.expect("Jaccard distance is ([0-9]+\\.[0-9]+).")
jaccard12 = bibref.match.groups()
jaccard = float(jaccard12[0])
percent = int(r/len(rows)*100)
print(f"{percent}% done\u001b[{0}K\r", end='')
f.write(f"{jaccard}\n")
g.write(f"{jaccard:.6f}\t{ot_passage}\t{nt_passage}\n")
r += 1
print(f"Done\u001b[{0}K")
f.close()
g.close()
def ot_jaccard_csv(conn, ot_book):
"""
Collect all quotations from an OT book that are obtained manually and
determine the Jaccard distances to the NT passages in a CSV file
:param conn: the Connection object
:param ot_book: OT book name
"""
global bibref
cur = conn.cursor()
cur.execute("SELECT q.ot_passage, q.nt_passage" +
" FROM quotations_with_introduction q" +
" WHERE q.found_method = 'manual'" +
" AND q.ot_startpos IS NOT NULL" +
" AND q.ot_book = '" + ot_book + "'" +
" AND INSTR(q.nt_passage, 'SBLGNT') > 0" +
" ORDER BY q.ot_startpos")
rows = cur.fetchall()
spawn_bibref()
f = open("ot-jaccard_" + ot_book + ".csv", "w")
g = open("ot-jaccard_" + ot_book + ".txt", "w")
r = 0
for row in rows:
ot_passage = row[0]
nt_passage = row[1]
command1 = "lookup1 " + ot_passage
bibref.sendline(command1)
bibref.expect("Stored internally as \\w.")
command2 = "lookup2 " + nt_passage
bibref.sendline(command2)
bibref.expect("Stored internally as \\w.")
# command3 = "compare12"
command3 = "jaccard12"
bibref.sendline(command3)
# bibref.expect("difference = ([0-9]+\\.[0-9]+).")
bibref.expect("Jaccard distance is ([0-9]+\\.[0-9]+).")
jaccard12 = bibref.match.groups()
jaccard = float(jaccard12[0])
percent = int(r/len(rows)*100)
print(f"{percent}% done\u001b[{0}K\r", end='')
f.write(f"{jaccard}\n")
g.write(f"{jaccard:.6f}\t{ot_passage}\t{nt_passage}\n")
r += 1
print(f"Done\u001b[{0}K")
f.close()
g.close()
def sort_positions():
global nt_positions, nt_positions_info, nt_passages, ot_positions, ot_positions_info, ot_passages
# Order the NT positions (bubble sort)
s = 0
for k in range(len(nt_positions)):
for l in range(k):
# print(k,l,nt_positions[l],nt_positions[k])
if nt_positions[l] > nt_positions[k]:
# swap them
d = nt_positions[l]
di = nt_positions_info[l]
dp = nt_passages[l]
nt_positions[l] = nt_positions[k]
nt_positions_info[l] = nt_positions_info[k]
nt_passages[l] = nt_passages[k]
nt_positions[k] = d
nt_positions_info[k] = di
nt_passages[k] = dp
s += 1
# Order the OT positions (bubble sort)
for m in ot_positions.keys():
for k in range(len(ot_positions[m])):
for l in range(k):
# print(k,l,nt_positions[l],nt_positions[k])
if ot_positions[m][l] > ot_positions[m][k]:
# swap them
d = ot_positions[m][l]
di = ot_positions_info[m][l]
dp = ot_passages[m][l]
ot_positions[m][l] = ot_positions[m][k]
ot_positions_info[m][l] = ot_positions_info[m][k]
ot_passages[m][l] = ot_passages[m][k]
ot_positions[m][k] = d
ot_positions_info[m][k] = di
ot_passages[m][k] = dp
s += 1
# print('#', s, 'swaps')
def comment(text, format):
"""
Print a comment to stdout according to the given format
:param text: the text of comment
:param format: expected format (latex, text, html, svg)
"""
threshold = 2000 # this is needed for LaTeX since it cannot handle too long lines
output = str(text)
if len(output) > threshold and (format == "latex" or format == "svg"):
output = str(output[0:threshold]) + "..."
if format == "latex" or format == "svg":
print("%", output)
if format == "html":
print("<!--", output, "-->")
if format == "text":
print("#", output)
def print_passage_header():
print ("\\usepackage[utf8]{inputenc}")
print ("\\usepackage[greek,english]{babel}")
print ("\\usetikzlibrary{positioning}")
print ("\\renewcommand{\\familydefault}{\\sfdefault}")
print ("\\tikzstyle{nt_passage}=[rectangle,left color=cyan!50,right color=white,minimum size=6mm]")
print ("\\tikzstyle{ot_passage}=[rectangle,left color=yellow!50,right color=white,minimum size=6mm]")
print ("\\tikzstyle{intro}=[rectangle,draw=blue!50,fill=blue!20,thick,inner sep=1mm,minimum size=6mm]")
print ("\\tikzstyle{clasp}=[rectangle,draw=blue!50,fill=ForestGreen!20,thick,inner sep=1mm,minimum size=6mm]")
print ("\\tikzstyle{overlap}=[rectangle,draw=blue!50,fill=OliveGreen,thick,inner sep=1mm,minimum size=6mm]")
print ("\\tikzstyle{unidentified}=[rectangle,draw=blue!20,fill=black!5,thick,inner sep=1mm,minimum size=6mm]")
print ("\\tikzstyle{unquoted}=[rectangle,draw=blue!20,fill=black!5,thick,inner sep=1mm,minimum size=6mm]")
print ("\\begin{document}")
def nt_passage_info(conn, nt_quotation_id, format, texts=0):
"""
Show all relevant information on a given NT quotation
:param conn: the Connection object
:param nt_quotation_id: NT quotation identifier
:param format: latex, svg, html, text
:texts: 0 if the length is used, 1 for Latin text, 2 for Greek text
"""
global bibref