-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniarbylmachine.d
1477 lines (951 loc) · 28.1 KB
/
niarbylmachine.d
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
/*
* This program is part of the Niarbyl software, which is a free
* software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*
* USES :
* ======
*
* This is the niarbyl machine used to read various kind of data.
*/
module niarbylmachine; // module declared
/** imports **/
// tango
import tango.io.Stdout;
// phobos
import std.getopt, std.stdio, std.string,
std.math, std.conv, std.range,
std.algorithm, std.array, std.stream,
std.regex;
// Further add-on -s
import array_manip, file_io, string_libs;
/** globals **/
// structs
// ---------- needed by the markup reader
struct grammar { // grammar
string[] enclosers; // enclosers
string separator; // separator between key and value
string entry_sep; // separator between entries
string content_key; // the key that lists the contents
string id_key; // the key that contains the ID as a value
string[] trg_keyname_h; // check targets, for has command
string[] trg_keyname_l; // check targets, for lacks command
string[] has; // check whether targets contain the elements of this array
string[] lacks; // check whether targets lack the elements of this array
}
struct result { // result
string[] raw; // the raw data as picked up by grammar
string res; // the formatted output
}
// ----------- needed by the heirarchical reader
struct tree { // retrun a tree structure
}
struct knot(T, U) { // knot is same as node, but the name node is already taken
// multiple knots make up an arc
T prec; // precedessor
short type; // which of the following options
T ID; // either an integer ID for the knot
U cnt; // content of the knot
arc!(T, U) * arcp; // or a point to another arc
T succ; // successor
}
struct arc(T, U) { // the arc, that is supposed to be made by knots
T initmark; // at which identifier does it start?
knot!(T, U)[] knots; // ...
T endmark; // ...
short cnttp;
}
struct fractalSet_symbolic_1Dbase(T, U) { // return a fractal set, constructed upon surnumber tuples
// a single set for a sinlge readout contains the whole hierarchy.
string raw; // the raw readout.
arc!(T, U) [] arcs; // and finally, the arc that make the set.
}
// ----------- needed by the command parser
struct parseRes {
short type;
string res;
parseRes[] compoundRes;
}
// classes
class markupReader {
string inputFl; // input file
string grmrFl; // grammar file
string searchTrgt; // search target in the file
grammar[] Grammars; // list of grammars to be used
result[] Results; // list of results
int[][] GrammarBoundaries;
this()
{
}
~this()
{
}
//------------------ methods
void initiate(string inFl, string grFl) // Specifically a function and not the default constructor
{
inputFl = inFl;
grmrFl = grFl;
}
void loadGrammar() // load all the grammars from the grammarfile
{
auto f_ = std.stdio.File(grmrFl, "r"); // open the grammarfile
short i_ = 1; // a counter to keep track of the lines
string ln; // string to hold one line read from the file at a time
grammar * Grammar = new grammar; // a grammar that we will need
while(!f_.eof()) // while can read file
{
while(i_ <= 9 && !f_.eof()) // while all 9 lines are not read yet
{
switch(i_) // based on which line we are reading
{
case 1:
// read the enclosing characters
ln = f_.readln().chomp();
Grammar.enclosers = ln.split(" ");
break;
case 2:
// read the string differentiating the
// key and the value;
ln = f_.readln().chomp();
Grammar.separator = ln;
break;
case 3:
// read the entry separator
ln = f_.readln().chomp();
Grammar.entry_sep = ln;
break;
// if the next ones are not specified,
// we have an EOF
// the inner while loop wont reach here
case 4:
// read the content key
ln = f_.readln().chomp();
Grammar.content_key = ln;
break;
case 5:
// read the ID key
ln = f_.readln().chomp();
Grammar.id_key = ln;
break;
case 6:
// read the trg_keyname
ln = f_.readln().chomp();
if (ln != "")
Grammar.trg_keyname_h = ln.split(";");
else
Grammar.trg_keyname_h = [];
break;
case 7:
// read the has or lacks command,
// and edit it as necessary
ln = f_.readln().chomp();
if(ln != "")
{
if(ln[0 .. 4] == "has:")
Grammar.has = ln[4 .. $].split(";");
else if(ln[0 .. 6] == "lacks:")
Grammar.lacks = ln[6 .. $].split(";");
}
break;
case 8:
// read the trg_keyname
ln = f_.readln().chomp();
if (ln != "")
Grammar.trg_keyname_l = ln.split(";");
else
Grammar.trg_keyname_l = [];
break;
case 9:
// read the has or lacks command,
// and edit it as necessary
ln = f_.readln().chomp();
if(ln != ""){
if(ln[0 .. 4] == "has:")
Grammar.has = ln[4 .. $].split(";");
else if(ln[0 .. 6] == "lacks:")
Grammar.lacks = ln[6 .. $].split(";");
}
break;
default:
break;
}
i_++;
}
Grammars.assumeSafeAppend(); // take care of things
// TODO : Look up why assumesafeappend is necessary
Grammars ~= *Grammar; // append grammar Grammar
i_ = 0; // reset i
Grammar = new grammar; // reset Grammar
}
f_.close();
// this will automatically call expandGrammar ;
expandGrammar();
}
void expandGrammar() // expand the grammars based on there has or lack options,
// each has or lack target should have a SINGLE value
{
grammar[] G;
grammar * g;
string[] hs;
string[] ls;
int andCnt, orCnt;
string[][] hh;
string[][] hk;
string[][] ll;
string[][] lk;
G = []; // no grammar in the list of modified, expanded grammars
foreach(grammar Grm; Grammars)
{
if(Grm.has.count() != 0) // we have has keywords
{
hh = []; // empty list of all has
foreach(h; Grm.has)
{
hs = h.split(",");
hh ~= hs;
}
// now we have the jagged array hh
hk = flatten_array(hh);
foreach(hhk; hk)
{
// set the grammar
g = new grammar;
g.enclosers = Grm.enclosers;
g.separator = Grm.separator;
g.entry_sep = Grm.entry_sep;
g.content_key = Grm.content_key;
g.id_key = Grm.id_key;
g.trg_keyname_h = Grm.trg_keyname_h;
g.trg_keyname_l = Grm.trg_keyname_l;
g.has = hhk;
g.lacks = Grm.lacks;
G ~= *g;
}
}
else
{
G ~= Grm;
}
}
Grammars = G;
G = []; // reset again, do the same with lacks
foreach(grammar Grm; Grammars)
{
if(Grm.lacks.count() != 0)
{
ll = []; // empty
foreach(l; Grm.lacks)
{
ls = l.split(",");
ll ~= ls;
}
// now we have the jagged array hh
lk = flatten_array(ll);
foreach(llk; lk)
{
g = new grammar;
g.enclosers = Grm.enclosers;
g.separator = Grm.separator;
g.entry_sep = Grm.entry_sep;
g.content_key = Grm.content_key;
g.id_key = Grm.id_key;
g.trg_keyname_l = Grm.trg_keyname_l;
g.trg_keyname_h = Grm.trg_keyname_h;
g.lacks = llk;
g.has = Grm.has;
G ~= *g;
}
}
else
{
G ~= Grm;
}
}
Grammars = G;
}
void setGrammar()
{
int[][] boundaries = extractBoundaries();
int bbi = 0;
for(bbi = 0; bbi <Grammars.count(); bbi ++)
{
boundaries[bbi][1] = boundaries[bbi][1] + to!int(Grammars[bbi].enclosers[1].count()) - 1;
}
// foreach grammar;
// pick list of boundaries - each grammar can have multiple boundaries
// foreach boundary
// if the grammar does not apply
// kill that boundary
// then we have an ordered list of grammars
// and a corresponding ordered list of boundaries
// then in order to find target
// one can scan the file, picking up the correct
// grammars between boundaries
grammar G, Grm;
grammar[] G_;
int[] b;
string rdout;
string chkTrgt;
auto f = new std.stream.File(inputFl);
int i,j,k, rj;
int tc, ti;
ulong chkrs;
string hj;
// Stdout("*****").nl();
for(i = 0; i < Grammars.count(); i++)
{
b = boundaries[i];
rdout = "";
// form the check target
tc = to!int(Grammars[i].trg_keyname_h.count());
rj = j; // intial remove target
for(j = 0; j < b.count() ; j = j+2)
{
chkrs = 1;
for(ti = 0; ti < tc; ti ++)
{
// make the chk trg
chkTrgt = Grammars[i].trg_keyname_h[ti] ~ Grammars[i].separator ~ Grammars[i].has[ti]; // each key has a single value. multiple values have been splitted
// extract from file
f.seek(b[j], SeekPos.Set) ;
k = j;
while(k <= b[j+1] && !f.eof())
{
rdout = rdout ~ f.getc();
k++;
}
// regexMatchCnt
chkrs = chkrs * regexMatchCnt(rdout, chkTrgt);
}
if (chkrs == 0) // check failed
{
boundaries[i] = remove(boundaries[i], rj);
boundaries[i] = remove(boundaries[i], rj+1);
}
else // perhaps you will have to kill the next j
{
rj = j;
}
}
}
f.close();
GrammarBoundaries = boundaries;
}
void readTarget(string target, string Format)
{
// figure out, if any of the grammars can identify the target
// if Not, then break
// if at this point then parse the readout to a the required output format
string ID, key;
string IDSep, keySep;
string readout;
int i,j,k;
int p = 0, q;
char ch;
ulong regxcount;
string[] regexMatches;
switch(Format)
{
case "raw":
// find ID from target
ID = target[0 .. target.count() - find(target,'.').count()];
key = target[ target.count() - find(target,'.').count()+1 .. $];
auto f_ = new std.stream.File(inputFl);
for (j = 0; j < Grammars.count(); j++)
{
// construct ID + seperator
IDSep = Grammars[j].id_key ~ Grammars[j].separator ~ ID;
// construct key + seperator
keySep = key ~ Grammars[j].separator;
for(i = 0; i < GrammarBoundaries[j].count(); i++){
f_.seek(GrammarBoundaries[j][i], SeekPos.Set);
k = GrammarBoundaries[j][i];
// get balanced readout
readout = "";
// the first one is ALWAYS an opening brace
q = GrammarBoundaries[j][i];
while(true)
{
ch = f_.getc();
readout ~= ch;
p = to!int(readout.count(Grammars[j].enclosers[0]) - readout.count(Grammars[j].enclosers[1]));
if(p == 0)
break;
q ++;
}
// now figure out which boundary location you are in;
for(int m = 0; m < GrammarBoundaries[j].count(); m++)
{
if (q >= GrammarBoundaries[j][m]) // so we have touched the start of encloser, and copied also the further chars of the enclosers
{
i = m; // start with next i;
}
}
regxcount = regexMatchCnt(readout, IDSep);
if(regxcount != 0)
{
// the ID matches
regexMatches = regexGetMatch(readout, keySep);
if (regexMatches.count() != 0) // we found something, the first one suffices
{
result* res;
res = new result;
res.raw ~= readout;
res.res = find(readout, regexMatches[0])[regexMatches[0].count() - 1 .. $];
res.res = res.res[0 .. res.res.count() - find(res.res, Grammars[j].entry_sep).count()];
Results ~= *res;
}
}
}
}
f_.close();
break;
case "parse":
break;
default:
break;
}
}
int[] removeUpto(int[] trgar, int lm)
{
int[] k = find(trgar, lm);
if(k == [])
return [];
else
return k[1 .. $];
}
int[][] extractBoundaries()
{
int[] e1;
int[] e2;
int[] ee1;
int[] ee2;
int[] e;
int[] e_trk;
int[][] te;
int[][] te_trk;
int[][] boundaries;
// first get the boundaries
foreach(grammar Grm; Grammars)
{
// find where the enclosers are
e = [];
e_trk = [];
//locate opening enclosers
e1 = getLocations_inFile(Grm.enclosers[0], inputFl);
//locate closing enclosers
e2 = getLocations_inFile(Grm.enclosers[1], inputFl) ;
// arrange it
ee1 = e1;
ee2 = e2;
while(1)
{
// if we have 0, then break;
if (ee1.count() == 0 && ee2.count() == 0)
{
break;
}
if (ee1.count() == 0)
{
// if we dont have the openning any more
e ~= ee2[0]; // our encloser is just the closing encloser, copy the location
e_trk ~= 2; // and mention that we are copying the second encloser
ee2 = ee2[1 .. $]; //pop
continue;
}
if (ee2.count() == 0)
{
// same as above, but just the other indices
e ~= ee1[0];
e_trk ~= 1;
ee1 = ee1[1 .. $];
continue;
}
// select the one of ee1 and ee2, which ever appears first in the stream
if(ee1[0] < ee2[0])
{
e ~= ee1[0];
e_trk ~= 1;
ee1 = ee1[1 .. $];
}
else if(ee1[0] > ee2[0])
{
e ~= ee2[0];
e_trk ~= 2;
ee2 = ee2[1 .. $];
}
}
te ~= e;
te_trk ~= e_trk; // saved both e and e_trk in the bigger array, each i-th element of te is an array, and
// are the locations of enclosers of the i-th grammar.
}
// TESTED :so far so good
// Now use the comparators,
// to find supersets
int cntr;
int cntr2;
string[] subs, sups;
grammar * subg, supg;
int ie1, je1;
int ie2, je2;
int l = -1;
// string subs, sups;
int jk, kk;
bool skip = false;
for (cntr = 0; cntr < Grammars.count() ; cntr++)
{
// find the absolute supper grammar
subg = new grammar;
subg = &Grammars[cntr];
subs = subg.enclosers;
for(cntr2 = 0; cntr2 < Grammars.count() ; cntr2++)
{
if(cntr == cntr2)
{ skip = true; continue;}
if(cntr != cntr2)
{
supg = new grammar;
supg = &Grammars[cntr2];
sups = supg.enclosers;
if(canFind(sups[0], subs[0]) && canFind(sups[1], subs[1]))
{
l = cntr2;
subs = sups;
continue;
}
}
}
// now sups is the maximum super enclosers
// grammars[l] = supg
// cntr is the current grammar against which we look for supgs.
// we assume that it is ballanced
// if(cntr == cntr2-1){
// Stdout(cntr2 <= Grammars.count()-1).nl();
// Stdout(Grammars[cntr].has).nl();
// Stdout(cntr2).nl();
// Stdout(cntr).nl();
// Stdout(Grammars.count()).nl();
// }
if (l != -1 )//&& cntr != cntr2-1) // l has been found, and cntr is not the last one,
// in the last one, where cntr2 breaks out of the for loop,
// then cntr is one more
{
ie1 = -1;
ie2 = -1;
for(kk =0 ; kk < te[l].count(); kk++)
{
for(jk = 0 ; jk < te[cntr].count(); jk++)
{
if(jk != 0)
{
if (te[l][kk] <= te[cntr][jk] && te[l][kk] >= te[cntr][jk-1])
{ ie1 = jk; break; }
}
else
{
if (te[l][kk] <= te[cntr][jk])
{ ie1 = jk; break; }
}
}
for(jk = 0 ; jk < te[cntr].count(); jk++)
{
if(jk != te[cntr].count()-1)
{
if (te[l][kk+1] >= te[cntr][jk] && te[l][kk+1]+supg.enclosers[1].count()-1 <= te[cntr][jk+1]){
ie2 = to!int(te[cntr].count() - find(te[cntr],te[l][kk+1]+supg.enclosers[1].count()-1).count());
if(ie2 == -1)
ie2 = jk;
break; }
}
else
{
if (te[l][kk+1] >= te[cntr][jk])
{ ie2 = jk; break; }
}
}
// // now we have the boundary
if(ie1 != -1 && ie2 != -1)
{
if (ie1 == 0 && ie2 == te[cntr].count()-1)
{ //te[cntr] = []; te_trk[cntr] = [];
}
else if (ie1 == 0 && ie2 != te[cntr].count()-1)
{ te[cntr] = te[cntr][ie2+1 .. $]; te_trk[cntr] = te_trk[cntr][ie2+1 .. $];}
else if (ie1 != 0 && ie2 == te[cntr].count()-1)
{ te[cntr] = te[cntr][0 .. ie1-1]; te_trk[cntr] = te_trk[cntr][0 .. ie1-1];}
else if (ie1 != 0 && ie2 != te[cntr].count()-1)
{ te[cntr] = te[cntr][0 .. ie1-1] ~ te[cntr][ie2+1 .. $]; te_trk[cntr] = te_trk[cntr][0 .. ie1-1] ~ te_trk[cntr][ie2+1 .. $];}
}
kk++ ;
}
}
}
boundaries = te;
//Stdout(te).nl();
return boundaries;
}
ulong regexMatchCnt(string haystack, string needle)
{
auto r = regex(needle, "g");
auto m = match(haystack, r);
int i;
foreach(c; m) {
i++; }
return i;
}
string[] regexGetMatch(string haystack, string needle)
{
auto r = regex(needle, "g");
auto m = match(haystack, r);
string[] res;
foreach(c; m) {
res ~= c.hit; }
return res;
}
}
class hierarchicalReader
{
/*
Hierarchical readers are specifically designed to generate a fractal set
to represent the hierarchical relation between grammars.
While it is possible to emulate hierarchical structure of data points
using any markup, the hierarchical reader can exploit a more concise syntax,
because it expects a certain hierarchy in the data, thus interpretes,
without the aid of a markup, certain tokens of the syntax as certain objects
related to the hierarchy.
*/
// globally accessible stuff
string inputFl;
// stuff that needs to be set during initialization;
short fractalSet_basedim; // base dinension of the fractal set.
// this is the basic initialization variable
// all other varibales related to initialization follow this.
// besides cnttp
string arc_type; // arc type
string cnttp; // content type
// other stuff
string all_raw;
fractalSet_symbolic_1Dbase!(string, string[]) fractalSet_sym_1D;
this(short bdm, string cntt, string atp)
{
fractalSet_basedim = bdm;
cnttp = cntt;
if(bdm == 1)
{
arc_type = atp;
}