summaryrefslogtreecommitdiffstats
path: root/modules/mt-lxx-parallel/convert.pl
blob: f38c6bd88d15a3fab3bcb650a1239084890f089b (plain) (blame)
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
#!/usr/bin/perl -w

#
# This tool is supposed to convert the ccat Parallel MT/LXX
# to a valid OSIS file.
#
# @author Martin Gruner
# @copyright GPL
#

use strict;

binmode(STDOUT, ":utf8"); #see "man perluniintro"

my $prefix = "parallel/";

my $MorphologicalSegmentStart   = "<seg type=\"morph\">" ;
my $MorphologicalSegmentEnd     = "</seg>" ;
my $MorphologicalDivisionMarker = $MorphologicalSegmentEnd . $MorphologicalSegmentStart;

my $hebrewLetters="A-Z\(\)\+\#\$\*\&/"; #used in a character class of a regexp later
my %hebrew2utf8 = (
")" =>chr(0x05D0), #HEBREW LETTER ALEF

"A" =>chr(0x05D0), #HEBREW LETTER ALEF     # TODO: check, this is from an  occurrence of ABRHM

"B" =>chr(0x05D1), #HEBREW LETTER BET
"G" =>chr(0x05D2), #HEBREW LETTER GIMEL
"D" =>chr(0x05D3), #HEBREW LETTER DALET
"H" =>chr(0x05D4), #HEBREW LETTER HE
"W" =>chr(0x05D5), #HEBREW LETTER VAV
"Z" =>chr(0x05D6), #HEBREW LETTER ZAYIN
"X" =>chr(0x05D7), #HEBREW LETTER HET
"+" =>chr(0x05D8), #HEBREW LETTER TET
"Y" =>chr(0x05D9), #HEBREW LETTER YOD
#"K" =>chr(0x05DA), #HEBREW LETTER FINAL KAF  # TODO: HANDLE FINAL LETTERS
"K" =>chr(0x05DB), #HEBREW LETTER KAF
"L" =>chr(0x05DC), #HEBREW LETTER LAMED
#chr(0x6D)=>chr(0x05DD), #HEBREW LETTER FINAL MEM
"M" =>chr(0x05DE), #HEBREW LETTER MEM
#"N" =>chr(0x05DF), #HEBREW LETTER FINAL NUN
"N" =>chr(0x05E0), #HEBREW LETTER NUN
"S" =>chr(0x05E1), #HEBREW LETTER SAMEKH
"(" =>chr(0x05E2), #HEBREW LETTER AYIN
#"P" =>chr(0x05E3), #HEBREW LETTER FINAL PE
"P" =>chr(0x05E4), #HEBREW LETTER PE
#"C" =>chr(0x05E5), #HEBREW LETTER FINAL TSADI
"C" =>chr(0x05E6), #HEBREW LETTER TSADI
"Q" =>chr(0x05E7), #HEBREW LETTER QOF / KOF
"R" =>chr(0x05E8), #HEBREW LETTER RESH
"#" =>chr(0x05E9).chr(0x05C1), #HEBREW LETTER SHIN == SIN/SHIN without dot
"\$" =>chr(0x05E9).chr(0x05C1), #HEBREW LETTER SHIN + SHIN DOT == SHIN
"&" =>chr(0x05E9).chr(0x05C2), #HEBREW LETTER SHIN + SIN DOT == SIN
"T" =>chr(0x05EA), #HEBREW LETTER TAV
"-" =>chr(0x05BE), #MAQQEF

# Special stuff
"*" =>"(ketiv)", # TODO:FIX
"**" =>"(qere)", #
"/" => $MorphologicalDivisionMarker,

"," => ",", #separate words in colB

"?" => "<note type=\"textual\">Uncertain.</note>" #HACK

#"{" => "{",	# TODO: CHECK IF NECCESSARY
#	"}" => "}",
#"." => ".",

);

my %greek2utf8 = (
"\\"=>chr(0x0300), #COMBINING GRAVE ACCENT
"/" =>chr(0x0301), #COMBINING ACUTE ACCENT
"+" =>chr(0x0308), #COMBINING DIAERESIS
"=" =>chr(0x0342), #COMBINING GREEK PERISPOMENI / CIRCUMFLEX
")" =>chr(0x0313), #COMBINING COMMA ABOVE / SMOOTH BREATHING
"(" =>chr(0x0314), #COMBINING REVERSED COMMA ABOVE / ROUGH BREATHING
"|" =>chr(0x0345), #COMBINING GREEK YPOGEGRAMMENI / IOTA SUBSCRIPT

"'" => "'",
"{" => "{",	# TODO: CHECK IF NECCESSARY
"}" => "}",
"." => ".",
"^" => "^",
"?" => "?",



# "*A" =>chr(0x0391), #GREEK CAPITAL LETTER ALPHA
# "*B" =>chr(0x0392), #GREEK CAPITAL LETTER BETA
# "*G" =>chr(0x0393), #GREEK CAPITAL LETTER GAMMA
# "*D" =>chr(0x0394), #GREEK CAPITAL LETTER DELTA
# "*E" =>chr(0x0395), #GREEK CAPITAL LETTER EPSILON
# "*V" =>chr(0x03DC), #GREEK LETTER DIGAMMA
# "*Z" =>chr(0x0396), #GREEK CAPITAL LETTER ZETA
# "*H" =>chr(0x0397), #GREEK CAPITAL LETTER ETA
# "*Q" =>chr(0x0398), #GREEK CAPITAL LETTER THETA
# "*I" =>chr(0x0399), #GREEK CAPITAL LETTER IOTA
# "*K" =>chr(0x039A), #GREEK CAPITAL LETTER KAPPA
# "*L" =>chr(0x039B), #GREEK CAPITAL LETTER LAMDA
# "*M" =>chr(0x039C), #GREEK CAPITAL LETTER MU
# "*N" =>chr(0x039D), #GREEK CAPITAL LETTER NU
# "*C" =>chr(0x039E), #GREEK CAPITAL LETTER XI
# "*O" =>chr(0x039F), #GREEK CAPITAL LETTER OMICRON
# "*P" =>chr(0x03A0), #GREEK CAPITAL LETTER PI
# "*R" =>chr(0x03A1), #GREEK CAPITAL LETTER RHO
# "*S" =>chr(0x03A3), #GREEK CAPITAL LETTER SIGMA
# "*J" =>chr(0x03A3), #GREEK CAPITAL LETTER SIGMA #at end of Word
# "*T" =>chr(0x03A4), #GREEK CAPITAL LETTER TAU
# "*U" =>chr(0x03A5), #GREEK CAPITAL LETTER UPSILON
# "*F" =>chr(0x03A6), #GREEK CAPITAL LETTER PHI
# "*X" =>chr(0x03A7), #GREEK CAPITAL LETTER CHI
# "*Y" =>chr(0x03A8), #GREEK CAPITAL LETTER PSI
# "*W" =>chr(0x03A9), #GREEK CAPITAL LETTER OMEGA

"A" =>chr(0x03B1), #GREEK SMALL LETTER ALPHA
"B" =>chr(0x03B2), #GREEK SM LETT BETA / SM LETTER BETA BEGINNING OF WORD
"G" =>chr(0x03B3), #GREEK SMALL LETTER GAMMA
"D" =>chr(0x03B4), #GREEK SMALL LETTER DELTA
"E" =>chr(0x03B5), #GREEK SMALL LETTER EPSILON
"V" =>chr(0x03DD), #GREEK SMALL LETTER DIGAMMA
"Z" =>chr(0x03B6), #GREEK SMALL LETTER ZETA
"H" =>chr(0x03B7), #GREEK SMALL LETTER ETA
"Q" =>chr(0x03B8), #GREEK SMALL LETTER THETA
"I" =>chr(0x03B9), #GREEK SMALL LETTER IOTA
"K" =>chr(0x03BA), #GREEK SMALL LETTER KAPPA
"L" =>chr(0x03BB), #GREEK SMALL LETTER LAMDA
"M" =>chr(0x03BC), #GREEK SMALL LETTER MU
"N" =>chr(0x03BD), #GREEK SMALL LETTER NU
"C" =>chr(0x03BE), #GREEK SMALL LETTER XI
"O" =>chr(0x03BF), #GREEK SMALL LETTER OMICRON
"P" =>chr(0x03C0), #GREEK SMALL LETTER PI
"#3"=>chr(0x03DF), #GREEK SMALL LETTER KOPPA
"R" =>chr(0x03C1), #GREEK SMALL LETTER RHO
"S" =>chr(0x03C3), #GREEK SMALL LETTER SIGMA
"J" =>chr(0x03C2), #GREEK SM LETT FINAL SIGMA / SM LETT SIGMA END OF WORD
"T" =>chr(0x03C4), #GREEK SMALL LETTER TAU
"U" =>chr(0x03C5), #GREEK SMALL LETTER UPSILON
"F" =>chr(0x03C6), #GREEK SMALL LETTER PHI
"X" =>chr(0x03C7), #GREEK SMALL LETTER CHI
"Y" =>chr(0x03C8), #GREEK SMALL LETTER PSI
"W" =>chr(0x03C9), #GREEK SMALL LETTER OMEGA
);

my %notes = (
"{#}" => "Asterized passage (in Job).",
"{g}" => "Reference to difference between the text of Rahlfs and that of the relevant Göttingen edition.",
"..a" => "Word included in one of the Aramaic sections.",
"*" => "Ketib.",
"**" => "Qere.",
"*z" => "Qere wela ketib, ketib wela qere.",
"[ ]" => "Reference of number of verse in LXX, different from MT.",  # TODO: MAKE USE OF IT
"[[ ]]" => "Reference number of verse in MT, different from the LXX.",
"{x}" => "UNKNOWN",		# TODO: FIX
"--- {x}" => "Apparent minus created by lack of equivalence between long stretches of text in the LXX and MT.",
"--+ {x}" => "Apparent plus created by lack of equivalence between long stretches of text in the LXX and MT.",
"{...}" => "Equivalent reflected elsewhere in the text, disregarded by indexing program.",
"~" => "Difference in sequence between MT and LXX, denoted after the first Hebrew word and before the second one, as well as between two Greek words.",
"~~~" => "Equivalent of the Hebrew or Greek word(s) occurring elsewhere in the verse or context (transposition).",
"{..~}" => "Stylistic or grammatical transposition.",
"{..}" => "Stylistic or grammatical transposition.", # TODO: occurs in the text, unknown meaning
"---" => "In the Greek column:  Hebrew counterpart lacking in the LXX (minus in the LXX).",
"--" => "In the Greek column:  Hebrew counterpart lacking in the LXX (minus in the LXX).",  # TODO: my addition, check, probably wrong
"--+" => "In col a. of the Hebrew:  element \"added\" in the Greek (plus in the LXX).",
"---+" => "In col a. of the Hebrew:  element \"added\" in the Greek (plus in the LXX).", # TODO: my addition
"'" => "Long minus or plus (at least four lines).", # TODO: my addition, check
"''" => "Long minus or plus (at least four lines).",
"{d}" => "Reference to doublet (occurring between the two elements of the doublet).",
"{d?}" => "Reference to doublet (occurring between the two elements of the doublet)?",
"{..d}" => "Distributive rendering, occurring once in the translation but referring to more than one Hebrew word.",
"{..r}" => "Notation in Hebrew column of elements repeated in the translation.",
"?" => "Questionable notation, equivalent, etc.",
"??" => "Questionable notation, equivalent, etc.", # TODO: my addition
"{p}" => "Greek preverb representing Hebrew preposition.",
"{..p}" => "Preposition added in the LXX in accordance with the rules of the Greek language or translational habits.",

"{!}" => "Infinitive absolute.",
"{!}na" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}ad" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}aj" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}nad" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}nd" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}nd+" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}p" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}p+" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}pd" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}-" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning
"{!}v" => "Infinitive absolute.", # TODO: occurs in the text, but unknown meaning

"{s}" => "Hebrew M/, MN (comparative, superlative) reflected by Greek comparative or superlative.", # TODO: UTF-8
"{t}" => "Transliterated Hebrew word.",
"\#" => "Long line continuing in next one, placed both at the end of the line running over and at the beginning of the following line in the opposite column.",
"{v}" => "The reading of the main text of the LXX seems to reflect a secondary text, while the \"original\" reading is reflected in a variant.",

# Notes regarding ColB of the Hebrew
"=" => "Introducing col. b of the Hebrew (a selection of retroverted readings, presumably found in the parent text of the LXX).",
"={d}" => "Reference to doublet (occurring between the two elements of the doublet).", # TODO: my addition, check
"={d?}" => "Reference to doublet (occurring between the two elements of the doublet)?", # TODO: my addition, check
"=\%" => "Introducing categories of translation technique recorded in col. b.",
"=\%vap" => "Change from active to passive form in verbs.",
"=\%vpa" => "Change from passive to active form in verbs.",
"=\%p" => "Difference in preposition or particle.",
"=\%pa" => "Difference in preposition or particle.", # TODO: my addition, check
"=\%p+" => "Addition of preposition or particle.",
"=\%p+?" => "Addition of preposition or particle?",
"=\%p-" => "Omission of preposition or particle.",
"=;" => "Retroversion in col. b based on equivalence occurring in immediate or remote context.",
#"G" => "Hebrew variant, but at this stage no plausible retroversion is suggested.",
"=+" => "Difference in numbers between MT and the LXX.",
"=\@" => "Etymological exegesis.",
"=?\@" => "Etymological exegesis?", #my addition
"=\@?" => "Etymological exegesis?", #my addition
"=\@...a" => "Etymological exegesis according to Aramaic.",
"=:" => "Introducing reconstructed proper noun.",
"=v" => "Difference in vocalization (reading).",
"=vs" => "Difference in vocalization (reading).", # TODO: check, occurs in text
"=r" => "Incomplete retroversion.",

"{*}" => "Agreement of LXX with ketib.",
"{**}" => "Agreement of LXX with qere.",

"." => "Interchange of consonants between MT and the presumed Hebrew parent text of the LXX.",

".a" => "Interchange of consonants between MT and the presumed Hebrew parent text of the LXX.",  # TODO: occurs, unknown
".m" => "Metathesis of consonants between MT and the presumed Hebrew parent text of the LXX.",
".z" => "Possible abbreviation.",
".s" => "One word of MT separated into two or more words in the parent text of the LXX.",
".j" => "Two words of MT joined into one word in the parent text of the LXX.",
".w" => "Different word-division reflected in the parent text of the LXX.",

"<sp" => "<sp",  #TODO: FIX, occurs in text
"<sp>" => "<sp>",  #TODO: FIX, occurs in text
"<sp^>" => "<sp^>",  #TODO: FIX, occurs in text

"^" => "^",			#Notsure what these are
"^^^" => "^^^", 

);

sub createNote(){
	my $noteText = shift;
	return("<note type=\"textual\">$noteText</note> ");
}
sub openNote(){
	my $noteText = shift;
	return("<note type=\"textual\">$noteText ");
}
sub closeNote(){
	my $noteText = shift;
	return("$noteText</note> ");
}


sub translateHebrewNote(){
	my $origNote = shift;
#	print("TranslateHebrewNote $origNote\n");

	($origNote =~ m/^=?\.([a-z()\$+-])([a-z()\$+-])$/) and 
		return( &createNote("Interchange of consonants (" . 
			&translateHebrewLetter( uc( $1 ) ) . "/" . &translateHebrewLetter( uc( $2 ) ) .
			") between MT and the presumed Hebrew parent text of the LXX.") );


	($origNote =~ m/^{\.\.d(.+)}/) and 
		return( &createNote("(".&translateHebrewWordorNote( $1 ).") ". $notes{ "{..d}" }) );

	($origNote =~ m/^{\.\.r(.+)}/) and 
		return( &createNote("(".&translateHebrewWordorNote( $1 ).") ". $notes{ "{..r}" }) );

	($origNote =~ m/^{\.\.\.(.+)}/) and 
		return( &createNote("(".&translateHebrewWordorNote( $1 ).") ". $notes{ "{...}" }) );

	($origNote =~ m/^{\.\.(.+)}/) and 
		return( &createNote("(".&translateHebrewWordorNote( $1 ).") ". $notes{ "{..}" }) );

	#Special cases: the note includes more than one hebrew word, "cat" the results together
	($origNote =~ m/^{\.\.\.([^}]+)$/) and 
		return( &openNote("(".&translateHebrewWordorNote($1).") ".$notes{"{...}"} ) );
	($origNote =~ m/^([^{]+)}$/) and 
		return( &closeNote("(".&translateHebrewWordorNote( $1 ).") ". $notes{ "{...}" }) );

	($origNote =~ m/^@([?$hebrewLetters]+)/) and
		return( &createNote( $notes{ "=\@" } ) . &translateHebrewWordorNote( $1 ) );

	($origNote =~ m/^\^([?$hebrewLetters]+)/) and
		return( "^" . &translateHebrewWordorNote( $1 ) );   # TODO: check, what is ^?

	#
	# Special handling for the = colB Notes
	#
	if (($origNote =~ m/^=/) and (not $notes{ $origNote } )) { #only split if the note does not exist, to avoid parsing problems
		print("note reads $origNote\n");
		if ($origNote =~ m/^=(<[0-9.a-z]+>)$/){
			return( &translateHebrewWordorNote( $1 ) );
		}
		elsif ($origNote =~m/^=(.+)$/ and $notes{ $1 }){
			return( &translateHebrewWordorNote( $1 ) );
		}
		elsif ($origNote =~ m/^=([?$hebrewLetters]+)/){
			return( &translateHebrewWordorNote( $1 ) );
		}
		elsif ($origNote =~ m/^=([^?$hebrewLetters]+)([?$hebrewLetters]+)/){ #Note + Hebrew text, split up
			if ($notes{ $1 }){
				return( &translateHebrewNote( $1 ) . &translateHebrewWordorNote( $2 ) );
			}
			elsif( $notes{ "=$1" }){
				return( &translateHebrewNote( "=$1" ) . &translateHebrewWordorNote( $2 ) );
			}
			else { die("Could not parse note.\n"); }
		}
		else { die("Could not parse note.\n"); }
	}

	#special case: no note, but a crossref (no book ID)     # TODO: for now OSIS refs are not parsed
 	($origNote =~ m/^<|>$/) and
#		return("<reference osisRef=\"$1.$2\"/>");
		return $origNote;

# 	#Special cases: osisREf with bookID, split because of space char, so put them together again
# 	($origNote =~ m/^<\^?(\w+)$/) and 
# #		return( "<reference osisRef=\"$1." );  # TODO: check if <reference/> exists
# 		return $origNote;
# 	($origNote =~ m/^(\d+)[.:](\d+)>?/) and 
# #		return( "$1.$2\"/> " );
# 		return $origNote;



	#special case: no note, but a crossref (with book ID)
# 	($origNote =~ m/^<\^?(\w+)\s?(\d+)[.:](\d+)>?/) and
#		return("<reference osisRef=\"$1.$2.$3\">&lt;$1&gt;</reference> ");

	($origNote =~ m/^[?].*/) and 
		return( &createNote( $notes{"?"} ) . &translateHebrewWordorNote( $1 ) );

	($notes{ $origNote }) and return( &createNote( $notes{$origNote} ) );

	for my $i ( 1 .. (length($origNote)-1) ){ #last try, split up into chunks
		if ( $notes{ substr($origNote,0,$i) } ){
			return( &translateHebrewNote(substr($origNote,0,$i)) . &translateHebrewWordorNote(substr($origNote,$i, length($origNote) - $i) ) );
		}
	}
	
	die("Note $origNote not found.\n");
}

sub translateHebrewLetter(){ #will return unicode hebrew without morph separation
	my $hebrew = shift;

	my $result;
	$result = $hebrew2utf8{ $hebrew } || die("Could not find Hebrew letter $hebrew\n");

	return $result;
}


sub translateHebrewWordorNote(){ #will return unicode hebrew with morph separation
	my $hebrew = shift;

# 	print("TranslateHebrew of: $hebrew\n");

	( $hebrew =~ m/^[^$hebrewLetters]/ ) and return &translateHebrewNote( $hebrew );
	( $hebrew =~ m/[}]$/ ) and return &translateHebrewNote( $hebrew );


	my $length = length($hebrew);
	my $index = 0;
	my $result = $MorphologicalSegmentStart;
	CHAR_LOOP: while ( $index < $length ) {
		my $hsubstr = substr( $hebrew, $index, 1);
		$hebrew2utf8{ $hsubstr } || die("could not find Hebrew: $hsubstr of word $hebrew at index $index length $length\n");
		$result .= $hebrew2utf8{ $hsubstr };
		++$index;
	}
	$result .= $MorphologicalSegmentEnd;
	return $result;
}

sub translateGreekWordorNote(){

	return;	# TODO: remove

	my $greek = shift;

	foreach my $key (keys %notes){
		if ( $greek eq $key ){ #we have a note and not a word
		return $notes{ $greek }; # TODO: format OSIS note
		}
	}

	my $length = length($greek);
	my $index = 0;
	my $result ="";
	CHAR_LOOP: while ( $index < $length ) {
# 		if ( $greek2utf8{ substr( $greek, $index, 1) } eq "*"){
# 			die("FOUND capital character in $greek\n");
# 			$result .= $greek2utf8{ substr( $greek, $index, 2) };
# 			$index += 2;
# 		}
# 		else{
			my $gsubstr = substr( $greek, $index, 1);
#			$greek2utf8{ $gsubstr } or die("Could not find greek: $gsubstr\n");
			$result .= $greek2utf8{ $gsubstr };
			++$index;
# 		}
	}
	return $result;
}

sub parseLine(){
	my $origLine = shift;
	my $result;

	printf("parsing %s\n", $origLine);

	$origLine =~ s/--=/--+ =/;	# TODO: UGLY HACK, this appears in the text but not the notes; this seems most reasonable

	($origLine =~ m/^([^=]+)?([=].+)?\t(.+)$/) or die("No match in parseLine().\n");
	($1 or $2) or die("Hebrew not found.\n");
	$3 or die("Greek not found.\n");
	$1 and my @hebrewWordsColA = split(/\s+/, $1);
	$2 and my @hebrewWordsColB = split(/\s+/, $2);
	my @greekWords = split(/\s+/, $3);

#	printf("1: $1 2: $2 3: $3\n");

	# 3 columns= Hebrew ColA, Hebrew ColB, Greek

	$result .= "<row>\n  <cell>";
	foreach my $wordA (@hebrewWordsColA){
		$result .= &translateHebrewWordorNote( $wordA );
	}
	$result .= "</cell>\n  <cell>";

	foreach my $wordB (@hebrewWordsColB){
#		if ( $wordB =~ m/^=/ ){
#			$wordB =~ m/(=[^$hebrewLetters()]*)([$hebrewLetters].*)?/ or die("No match in ColB.\n"); #added ( and ) in the first expression, because they can occur in notes also
#			$1 and $result .= &translateHebrewWordorNote( $1 ); #This isolates the notes introducing colB (=*)
#			$result .= &translateHebrewWordorNote( $2 );
#		}
#		else {
			$result .= &translateHebrewWordorNote( $wordB );
#		}
	}
	$result .= "</cell>\n  <cell>";

	my $index = 0;
	foreach my $wordG (@greekWords){
		if ( $greekWords[$index] eq "{x}" ){ #special case: note containing a space, has to be handled together
#			$result .= &translateGreekWordorNote( "$wordG $greekWords[$index+1]" );
			$index += 2;
		}
		elsif ( $wordG eq "{x}" ){	#skip
			++$index;
		}
		else{
#			$result .= &translateGreekWordorNote( $wordG );
			++$index;
		}
	}
	$result .= "  </cell>\n</row>";
#	printf("Result: %s\n", $result);
	return $result;
}

#
# grabVerseContent - if the Verse can be found, returns its Content, otherwise nothing
#
sub grabVerseContent(){  #Bookname, chapter, verse, @list
	my @result;
	my $bookname = shift; my $chapter = shift; my $verse = shift; my @buffer = @_;

	my $index=0;

	printf("Parsing $bookname $chapter:$verse...\n");

	if ($bookname eq "Obad"){ #special handling, no chapter:verse structure
		LOOP: foreach my $current_item (@buffer){
			if ($chapter == 1 and $current_item =~ m/^$bookname $verse/){ #only for the first chapter
				while ( not $buffer[++$index] =~ m/^\n|^\s*$/ ){
					push(@result, &parseLine( $buffer[$index] ) );
				}
				return @result;
			}
			$index++;
		}
	}
	else{
		LOOP: foreach my $current_item (@buffer){
			if ($current_item =~ m/^$bookname $chapter:$verse/){
				while ( not $buffer[++$index] =~ m/^\n|^\s*$/ ){
					push(@result, &parseLine( $buffer[$index] ) );
				}
				return @result;
			}
			$index++;
		}
	}
	return;
}	#Nothing found, don't return a value.

sub processBook(){
# File	File id		ThML id		OSIS id		Short Book Title

	my $filename = shift;
	my $bookname_infile = shift;
	my $thml_id = shift;
	my $osis_id = shift;
	my $short_book_title = shift;

	open( FILE, "$prefix/$filename") or die("Could not open file $prefix/$filename");
	my @BUF = <FILE>; chomp(@BUF); close( FILE );

	my @result;

	CHAPTER: foreach my $chapter(1..1000){
		print("Processing $bookname_infile chapter $chapter.\n");
		my $verse_found;
		VERSE: foreach my $verse(1..1000){
			my @verseContent = &grabVerseContent($bookname_infile, $chapter, $verse, @BUF);
			if (@verseContent) {
				if ($bookname_infile eq "Obad"){
					push(@result, "$osis_id $verse"); #chapter will be ignored for >1 by grabVerseContent
				}
				else{
					push(@result, "$osis_id $chapter:$verse");
				}
				push(@result, @verseContent);
				$verse_found = 1;
			}
			else{ #verse nonexistent, goto next chapter
				last VERSE;
			}
		}
		if (not $verse_found){ #chapter empty, stop here
			if ($chapter == 1) { die("Error: no content in $bookname_infile"); }
			last CHAPTER;
		}
	}
	return(@result);
	print("done.\n");
}

sub processBookVariant(){
# FileA	File_id_A		VariantNameA		FileB		File_id_B		VariantNameB		ThML id		OSIS id		Short Book Title

	my $filenameA = shift;
	my $bookname_infile_A = shift;
	my $variantNameA = shift;
	my $filenameB = shift;
	my $bookname_infile_B = shift;
	my $variantNameB = shift;
	my $thml_id = shift;
	my $osis_id = shift;
	my $short_book_title = shift;

#	print("Processing $booknameA $filenameA $booknameB $filenameB $neutralBookname... \n");

	open( FILE, "$prefix/$filenameA") or die("Could not open file $prefix/$filenameA");
	my @BUFA = <FILE>; chomp(@BUFA); close( FILE );

	open( FILE, "$prefix/$filenameB") or die("Could not open file $prefix/$filenameB");
	my @BUFB = <FILE>; chomp(@BUFB); close( FILE );

	my @result;

	CHAPTER: foreach my $chapter(1..1000){
		print("Processing $bookname_infile_A and $bookname_infile_B chapter $chapter.\n");
		my $verse_found;
		VERSE: foreach my $verse(1..1000){
			my @verseContentA = &grabVerseContent($bookname_infile_A, $chapter, $verse, @BUFA);
			my @verseContentB = &grabVerseContent($bookname_infile_B, $chapter, $verse, @BUFB);
			if (@verseContentA or @verseContentB) { 
				push(@result, "$osis_id $chapter:$verse");
				$verse_found = 1;
			}
			else{ #verse nonexistent, goto next chapter
				last VERSE;
			}
			if (@verseContentA){
				if (@verseContentB){ push(@result, $variantNameA) };
				push(@result, @verseContentA);
				if (@verseContentB){ push(@result, "") };
			}
			if (@verseContentB){
				if (@verseContentA){ push(@result, $variantNameB) };
				push(@result, @verseContentB);
			}
		}
		if (not $verse_found){ #chapter empty, stop here
			if ($chapter == 1) { die("Error: no content in $bookname_infile_A and $bookname_infile_B"); }
			last CHAPTER;
		}
	}
	return(@result);
	print("done.\n");

}

sub fixDaniel(){ #@buffer
	my @buffer = @_;
	my @result;
	my $index = 0;
	foreach my $currentItem (@buffer){
		if ($buffer[$index] =~ m/^DANIHL/){}#Do not add this line to the result
		else{
			if ($buffer[$index + 1] =~ m/^DANIHL/){ #Push both lines on one
				push(@result, $buffer[$index] . $buffer[$index +1] );
			}
			else{
				push(@result, $buffer[$index] ); #The normal case
			}
		}
		$index++;
	}
	return @result;
}

my @result;


	# File				File id			ThML id		OSIS id		Short Book Title
push(@result, &processBook("01.Genesis.par", "Gen", "Gen", "Gen", "Genesis") );
die "Finished Genesis\n"; 

push(@result, &processBook("02.Exodus.par", "Exod", "Exod", "Exod", "Exodus") );
push(@result, &processBook("03.Lev.par", "Lev", "Lev", "Lev", "Leviticus") );
push(@result, &processBook("04.Num.par", "Num", "Num", "Num", "Numbers") );
push(@result, &processBook("05.Deut.par", "Deut", "Deut", "Deut", "Deuteronomy") );

push(@result, &processBookVariant("07.JoshA.par", "JoshA", "Codex Alexandrinus:", "06.JoshB.par", "JoshB", "Codex Vaticanus:", "Josh", "Josh", "Joshua") );
push(@result, &processBookVariant("09.JudgesA.par", "JudgA", "Codex Alexandrinus:", "08.JudgesB.par", "JudgB", "Codex Vaticanus:", "Judg", "Judg", "Judges") );
 
push(@result, &processBook("10.Ruth.par", "Ruth", "Ruth", "Ruth", "Ruth") );
push(@result, &processBook("11.1Sam.par", "1Sam/K", "iSam", "1Sam", "1 Samuel") );
push(@result, &processBook("12.2Sam.par", "2Sam/K", "iiSam", "2Sam", "2 Samuel") );
push(@result, &processBook("13.1Kings.par", "1/3Kgs", "iKgs", "1Kgs", "1 Kings") );
push(@result, &processBook("14.2Kings.par", "2/4Kgs", "iiKgs", "2Kgs", "2 Kings") );
push(@result, &processBook("15.1Chron.par", "1Chr", "iChr", "1Chr", "1 Chronicles") );
push(@result, &processBook("16.2Chron.par", "2Chr", "iiChr", "2Chr", "2 Chronicles") );
push(@result, &processBook("18.Ezra.par", "Ezr", "Ezra", "Ezra", "Ezra") );
push(@result, &processBook("19.Neh.par", "Neh", "Neh", "Neh", "Nehemiah") );
push(@result, &processBook("18.Esther.par", "Esth", "Esth", "Esth", "Esther") );
push(@result, &processBook("26.Job.par", "Job", "Job", "Job", "Job") );
 
 #This might need special handling
 #push(@result, &processBook("Psalms.par", "Ps", "Ps", "Ps", "Psalms",
 
push(@result, &processBook("23.Prov.par", "Prov", "Prov", "Prov", "Proverbs") );
push(@result, &processBook("24.Qoh.par", "Qoh", "Eccl", "Eccl", "Ecclesiastes") );
push(@result, &processBook("25.Cant.par", "Song", "Song", "Song", "Song of Solomon") );
push(@result, &processBook("40.Isaiah.par", "Isa", "Isa", "Isa", "Isaiah") );
push(@result, &processBook("41.Jer.par", "Jer", "Jer", "Jer", "Jeremiah") );
push(@result, &processBook("43.Lam.par", "Lam", "Lam", "Lam", "Lamentations") );
push(@result, &processBook("44.Ezekiel.par", "Ezek", "Ezek", "Ezek", "Ezekiel") );

my @danielTmp = &processBookVariant("45.DanielOG.par", "Dan", "Old Greek:", "46.DanielTh.par", "DanTh", "Theodotion:", "Dan", "Dan", "Daniel");
push(@result, &fixDaniel( @danielTmp ) );

push(@result, &processBook("28.Hosea.par", "Hos", "Hos", "Hos", "Hosea") );
push(@result, &processBook("31.Joel.par", "Joel", "Joel", "Joel", "Joel") );
push(@result, &processBook("30.Amos.par", "Amos", "Amos", "Amos", "Amos") );
push(@result, &processBook("33.Obadiah.par", "Obad", "Obad", "Obad", "Obadiah") );
push(@result, &processBook("32.Jonah.par", "Jonah", "Jonah", "Jonah", "Jonah") );
push(@result, &processBook("29.Micah.par", "Mic", "Mic", "Mic", "Micah") );
push(@result, &processBook("34.Nahum.par", "Nah", "Nah", "Nah", "Nahum") );
push(@result, &processBook("35.Hab.par", "Hab", "Hab", "Hab", "Habakkuk") );
push(@result, &processBook("36.Zeph.par", "Zeph", "Zeph", "Zeph", "Zephaniah") );
push(@result, &processBook("37.Haggai.par", "Hag", "Hag", "Hag", "Haggai") );
push(@result, &processBook("38.Zech.par", "Zech", "Zech", "Zech", "Zechariah") );
push(@result, &processBook("39.Malachi.par", "Mal", "Mal", "Mal", "Malachi") );


print( join("\n", @result) );