-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollins0.cpp
1577 lines (1324 loc) · 38.5 KB
/
collins0.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
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
/*
* =====================================================================================
*
* Filename: collins0.cpp
*
* Description:
*
* Version: 1.0
* Created: 2013-03-10 18:26:19
* Revision: none
* Compiler: gcc
*
* Author: Didzis Gosko (dg), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <set>
#include <boost/algorithm/string.hpp>
#include "features.hpp"
#include "collins0.hpp"
using namespace std;
// Maksimāla ātruma optimizācijai pieejas tiesības ievērot nevajag, bez dinamiskiem atmiņas pieprasījumiem.
class Span
{
public:
// g, h, m tokeni
union
{
// Complete Span
struct {
const Span* ics; // Incomplete Span
const Span* cs; // Complete Span
} cs;
// Incomplete Span
struct {
const Span* cs; // Complete Span
const Span* rcs; // Reverse complete span
} ics;
struct {
const Span* first;
const Span* second;
};
};
typedef enum {
Undefined,
Terminal,
Complete,
Incomplete
} Type;
Type type;
const Token* gtoken;
const Token* htoken;
union {
const Token* mtoken;
const Token* etoken;
};
FeatureVector::Value score;
// TODO: kopēšana
Span()
{
/*
type = Undefined;
score = 0;
gtoken = nullptr;
htoken = nullptr;
mtoken = nullptr;
cs.ics = nullptr;
cs.cs = nullptr;
// nav vajadzīgs, jo cs un ics ir union
// ics.cs = nullptr;
// ics.rcs = nullptr;
*/
}
void terminal(const Token* g, const Token* h)
{
type = Terminal;
gtoken = g;
htoken = h;
mtoken = h; // vai labāk nullptr ?
cs.ics = nullptr;
cs.cs = nullptr;
score = 0;
}
void set(const Span& firstSpan, const Span& secondSpan, const FeatureVector::Value& score_ = 0)
{
first = &firstSpan;
second = &secondSpan;
score = score_;
}
void complete()
{
type = Complete;
gtoken = first->gtoken;
htoken = first->htoken;
mtoken = first->mtoken; // mtoken vai labāk etoken ? (var ar union)
}
void incomplete()
{
type = Incomplete;
gtoken = first->gtoken;
htoken = first->htoken;
mtoken = second->htoken;
}
void incomplete(const FeatureVector& features, vector<Feature>& localFeatures)
{
type = Incomplete;
gtoken = first->gtoken;
htoken = first->htoken;
mtoken = second->htoken;
scoreSelf(features, localFeatures);
}
void complete(const Span* incomplete, const Span* complete, FeatureVector::Value score_ = 0)
{
#ifdef DEBUG
if(incomplete->type != Incomplete)
cout << "WARNING!!! Incomplete type expected, got " << (int)incomplete->type << endl;
if(complete->type != Complete && complete->type != Terminal)
cout << "WARNING!!! Incomplete or Terminal type expected, got " << (int)complete->type << endl;
#endif
type = Complete;
cs.ics = incomplete;
cs.cs = complete;
gtoken = incomplete->gtoken;
htoken = incomplete->htoken;
mtoken = incomplete->mtoken; // mtoken vai labāk etoken ? (var ar union)
// score = incomplete->score + complete->score;
score = score_;
}
void incomplete(const Span* complete, const Span* reverse, FeatureVector::Value score_ = 0)
{
#ifdef DEBUG
if(complete->type != Complete && complete->type != Terminal)
cout << "WARNING!!! Incomplete or Terminal type expected, got " << (int)complete->type << endl;
if(reverse->type != Complete && reverse->type != Terminal)
cout << "WARNING!!! Incomplete or Terminal type expected, got " << (int)reverse->type << endl;
#endif
type = Incomplete;
ics.cs = complete;
ics.rcs = reverse;
gtoken = complete->gtoken;
htoken = complete->htoken;
mtoken = reverse->htoken;
// score = complete->score + reverse->score;
score = score_;
}
void scoreSelf(const FeatureVector& features, vector<Feature>& localFeatures)
{
// if(type == Incomplete)
{
localFeatures.clear();
getFeatures(localFeatures, gtoken, htoken, mtoken);
for(const Feature& feature : localFeatures)
score += features(feature);
}
}
void print(int level = 0) const;
};
class SpanStore
{
public:
// SpanStore() { szg = 0; szi = 0; szj = 0; }
// SpanStore(int sizeg, int sizei, int sizej) { resize(sizeg, sizei, sizej); }
SpanStore(int size) { resize(size, size, size); }
void resize(int sizeg, int sizei, int sizej) { szg = sizeg+1; szi = sizei; szj = sizej; store.reserve(szg*szi*szj); store.resize(szg*szi*szj); }
typedef struct Spans {
Span complete;
Span incomplete;
// Spans() { complete.type = Span::Complete; incomplete.type = Span::Incomplete; }
} Spans;
Spans& operator()(int g, int i, int j) { return store[(g+1)*szi*szj + i*szj + j]; }
private:
int szg, szi, szj;
std::vector<Spans> store;
};
// iezīmju tipu nosaka pēc pašas funkcijas nosaukuma
// TODO: restricted pointeri
void getFeatures(vector<Feature>& features, const Token* gtoken, const Token* htoken, const Token* mtoken, bool dh, bool dim, bool dom)
{
#if 0
const int maxFeatureCount = 10;
// rezervē vēl dažus baitus
if(features.capacity() - features.size() < maxFeatureCount)
features.reserve(features.size() + maxFeatureCount);
#endif
// formā...
//features.emplace_back(byte/word/dword elements...);
#define f(...) features.emplace_back(__VA_ARGS__);
const int g = gtoken ? gtoken->index() : -1;
const int h = htoken->index();
const int m = mtoken->index();
const Feature::word gftag = gtoken ? gtoken->fullTagID() : -1;
const Feature::word gtag = gtoken ? gtoken->tagID() : -2;
const Feature::byte gptag = gtoken ? gtoken->tag()[0] : -1;
const Feature::word hftag = htoken->fullTagID();
const Feature::word htag = htoken->tagID();
const Feature::byte hptag = htoken->tag()[0];
const Feature::word mftag = mtoken->fullTagID();
const Feature::word mtag = mtoken->tagID();
const Feature::byte mptag = mtoken->tag()[0];
const Feature::word gword = gtoken ? gtoken->wordID() : -3;
const Feature::word hword = htoken->wordID();
const Feature::word mword = mtoken->wordID();
const Feature::byte hm = h < m ? 0 : 1;
const Feature::byte ghm = (g < h ? 0 : 2) + (h < m ? 0 : 1);
const Feature::byte zero = 0;
Feature::byte hmdist = 0;
{
int d = h-m;
if(d < 0) d = -d;
if(d > 1) hmdist++;
if(d > 2) hmdist++;
// if(d > 3) hmdist++;
if(d > 5) hmdist++;
if(d > 10) hmdist++;
if(d > 20) hmdist++;
if(d > 30) hmdist++;
if(d > 40) hmdist++;
}
Feature::byte ghdist = 0;
{
int d = g-h;
if(d < 0) d = -d;
// if(d > 1) ghdist++;
if(d > 2) ghdist++;
// if(d > 3) ghdist++;
if(d > 5) ghdist++;
if(d > 10) ghdist++;
if(d > 20) ghdist++;
if(d > 30) ghdist++;
if(d > 40) ghdist++;
}
// potenciālās iezīmes
//
// Nepieciešamais bitu apjoms uz vienību:
//
// hm, ghm - 2 biti (T)
// ghdist|hmdist|degen - 4 biti (S)
// tagP - 8 biti (N)
// tag, fulltag, word, lemma - 16 biti (L)
//
// Iezīmes:
//
// 1.k. dep.
// [-|ghdist|hmdist], [-|degen], fulltag, fulltag, [hm]
// [-|ghdist|hmdist], [-|degen], tagP, tagP, [hm]
// [-|ghdist|hmdist], [-|degen], tag, tag, [hm]
// [-|hmdist], [word], tag, [word], tag, [hm]
//
// 2.k. dep.
// [dgen], fulltag, fulltag, fulltag, [ghm]
// [dgen], tagP, tagP, tagP, [ghm]
// [dgen], tag, tag, tag, [ghm]
//
// NOTE: word, tag, fullTag savā starpā nekonfliktē, jo tie ir zem viena indeksa
// izņemot gadījumus, kad kāds no tagiem sakrīt ar vārdu, bet lai būtu reāls konflikts, arī citām sakritībām jānotiek
//
// TODO: lai nekur nebūtu nejauša pārklāšanās, var pievienot pirmo elementu kā tipu,
// kurus iepriekš definē kā const Feature::byte TT, WTWT, ... (tag-tag, word-tag-word-tag, ...)
//
f(hftag, mftag);
f(htag, mtag);
f(hptag, mptag);
f(hftag, mftag, hm);
f(htag, mtag, hm);
f(hptag, mptag, hm);
f(hmdist, zero, hftag, mftag);
f(hmdist, zero, htag, mtag);
f(hmdist, zero, hptag, mptag);
f(hmdist, zero, hftag, mftag, hm);
f(hmdist, zero, htag, mtag, hm);
f(hmdist, zero, hptag, mptag, hm);
// ar word
f(hword, htag, mword, mtag);
f(htag, mword, mtag);
f(hword, htag, mtag);
f(hword, htag, mword, mtag, hm);
f(htag, mword, mtag, hm);
f(hword, htag, mtag, hm);
// ar word
f(hmdist, zero, hword, htag, mword, mtag);
f(hmdist, zero, htag, mword, mtag);
f(hmdist, zero, hword, htag, mtag);
f(hmdist, zero, hword, htag, mword, mtag, hm);
f(hmdist, zero, htag, mword, mtag, hm);
f(hmdist, zero, hword, htag, mtag, hm);
// 2.k. dep.
// [dgen], fulltag, fulltag, fulltag, [ghm]
// [dgen], tagP, tagP, tagP, [ghm]
// [dgen], tag, tag, tag, [ghm]
f(gftag, hftag, mftag);
f(gptag, hptag, mptag);
f(gtag, htag, mtag);
f(gftag, hftag, mftag, ghm);
f(gptag, hptag, mptag, ghm);
f(gtag, htag, mtag, ghm);
// te var redzēt kā pareizā secībā izsaukt deģenerēto gadījumu funkcijas
if(dom)
getFeaturesDegenCS(features, gtoken, htoken, mtoken, dim, dh);
if(dh || dim)
getFeaturesDegenICS(features, gtoken, htoken, mtoken, dh, dim);
}
// par degenouterm neko nezina
void getFeaturesDegenICS(vector<Feature>& features, const Token* gtoken, const Token* htoken, const Token* mtoken, bool dh, bool dim)
{
#if 0
const int maxFeatureCount = 10;
// rezervē vēl dažus baitus
if(features.capacity() - features.size() < maxFeatureCount)
features.reserve(features.size() + maxFeatureCount);
#endif
const int g = gtoken ? gtoken->index() : -1;
const int h = htoken->index();
const int m = mtoken->index();
const Feature::word gftag = gtoken ? gtoken->fullTagID() : -1;
const Feature::word gtag = gtoken ? gtoken->tagID() : -2;
const Feature::byte gptag = gtoken ? gtoken->tag()[0] : -1;
const Feature::word hftag = htoken->fullTagID();
const Feature::word htag = htoken->tagID();
const Feature::byte hptag = htoken->tag()[0];
const Feature::word mftag = mtoken->fullTagID();
const Feature::word mtag = mtoken->tagID();
const Feature::byte mptag = mtoken->tag()[0];
const Feature::word gword = gtoken ? gtoken->wordID() : -3;
const Feature::word hword = htoken->wordID();
const Feature::word mword = mtoken->wordID();
const Feature::byte hm = h < m ? 0 : 1;
const Feature::byte ghm = (g < h ? 0 : 2) + (h < m ? 0 : 1);
const Feature::byte zero = 0;
Feature::byte hmdist = 0;
{
int d = h-m;
if(d < 0) d = -d;
if(d > 1) hmdist++;
if(d > 2) hmdist++;
// if(d > 3) hmdist++;
if(d > 5) hmdist++;
if(d > 10) hmdist++;
if(d > 20) hmdist++;
if(d > 30) hmdist++;
if(d > 40) hmdist++;
}
Feature::byte ghdist = 0;
{
int d = g-h;
if(d < 0) d = -d;
// if(d > 1) ghdist++;
if(d > 2) ghdist++;
// if(d > 3) ghdist++;
if(d > 5) ghdist++;
if(d > 10) ghdist++;
if(d > 20) ghdist++;
if(d > 30) ghdist++;
if(d > 40) ghdist++;
}
// dh, dim
// katram deģenerētajam savs bits
const Feature::byte dgen = (dh ? 4 : 0) + (dim ? 2 : 0);
// dgen pietiek ar S - small (4 bitiem !!!)
// 1.k. dep.
// [-|ghdist|hmdist], [-|degen], fulltag, fulltag, [hm]
// [-|ghdist|hmdist], [-|degen], tagP, tagP, [hm]
// [-|ghdist|hmdist], [-|degen], tag, tag, [hm]
// [-|hmdist], [word], tag, [word], tag, [hm]
f(zero, dgen, hftag, mftag);
f(zero, dgen, htag, mtag);
f(zero, dgen, hptag, mptag);
f(zero, dgen, hftag, mftag, hm);
f(zero, dgen, htag, mtag, hm);
f(zero, dgen, hptag, mptag, hm);
f(hmdist, dgen, hftag, mftag);
f(hmdist, dgen, htag, mtag);
f(hmdist, dgen, hptag, mptag);
f(hmdist, dgen, hftag, mftag, hm);
f(hmdist, dgen, htag, mtag, hm);
f(hmdist, dgen, hptag, mptag, hm);
// ar word
f(zero, dgen, hword, htag, mword, mtag);
f(zero, dgen, htag, mword, mtag);
f(zero, dgen, hword, htag, mtag);
f(zero, dgen, hword, htag, mword, mtag, hm);
f(zero, dgen, htag, mword, mtag, hm);
f(zero, dgen, hword, htag, mtag, hm);
// ar word
f(hmdist, dgen, hword, htag, mword, mtag);
f(hmdist, dgen, htag, mword, mtag);
f(hmdist, dgen, hword, htag, mtag);
f(hmdist, dgen, hword, htag, mword, mtag, hm);
f(hmdist, dgen, htag, mword, mtag, hm);
f(hmdist, dgen, hword, htag, mtag, hm);
// 2.k. dep.
// [dgen], fulltag, fulltag, fulltag, [ghm]
// [dgen], tagP, tagP, tagP, [ghm]
// [dgen], tag, tag, tag, [ghm]
f(dgen, gftag, hftag, mftag);
f(dgen, gptag, hptag, mptag);
f(dgen, gtag, htag, mtag);
f(dgen, gftag, hftag, mftag, ghm);
f(dgen, gptag, hptag, mptag, ghm);
f(dgen, gtag, htag, mtag, ghm);
}
// degenouterm = true tiek pieņemts automātiski
void getFeaturesDegenCS(vector<Feature>& features, const Token* gtoken, const Token* htoken, const Token* mtoken, bool dim, bool dh)
{
#if 0
const int maxFeatureCount = 10;
// rezervē vēl dažus baitus
if(features.capacity() - features.size() < maxFeatureCount)
features.reserve(features.size() + maxFeatureCount);
#endif
const int g = gtoken ? gtoken->index() : -1;
const int h = htoken->index();
const int m = mtoken->index();
const Feature::word gftag = gtoken ? gtoken->fullTagID() : -1;
const Feature::word gtag = gtoken ? gtoken->tagID() : -2;
const Feature::byte gptag = gtoken ? gtoken->tag()[0] : -1;
const Feature::word hftag = htoken->fullTagID();
const Feature::word htag = htoken->tagID();
const Feature::byte hptag = htoken->tag()[0];
const Feature::word mftag = mtoken->fullTagID();
const Feature::word mtag = mtoken->tagID();
const Feature::byte mptag = mtoken->tag()[0];
const Feature::word gword = gtoken ? gtoken->wordID() : -3;
const Feature::word hword = htoken->wordID();
const Feature::word mword = mtoken->wordID();
const Feature::byte hm = h < m ? 0 : 1;
const Feature::byte ghm = (g < h ? 0 : 2) + (h < m ? 0 : 1);
const Feature::byte zero = 0;
Feature::byte hmdist = 0;
{
int d = h-m;
if(d < 0) d = -d;
if(d > 1) hmdist++;
if(d > 2) hmdist++;
// if(d > 3) hmdist++;
if(d > 5) hmdist++;
if(d > 10) hmdist++;
if(d > 20) hmdist++;
if(d > 30) hmdist++;
if(d > 40) hmdist++;
}
Feature::byte ghdist = 0;
{
int d = g-h;
if(d < 0) d = -d;
// if(d > 1) ghdist++;
if(d > 2) ghdist++;
// if(d > 3) ghdist++;
if(d > 5) ghdist++;
if(d > 10) ghdist++;
if(d > 20) ghdist++;
if(d > 30) ghdist++;
if(d > 40) ghdist++;
}
const Feature::byte dgen = (dh ? 4 : 0) + (dim ? 2 : 0) + 1;
// bool dom = true;
if(dim) // m tokens ir terminal tokens (nevienā pusē nav dependency
{
}
else // tikai no ārpuses deģenerēts bez dependency uz ārpusi
{
}
// 1.k. dep.
// [-|ghdist|hmdist], [-|degen], fulltag, fulltag, [hm]
// [-|ghdist|hmdist], [-|degen], tagP, tagP, [hm]
// [-|ghdist|hmdist], [-|degen], tag, tag, [hm]
// [-|hmdist], [word], tag, [word], tag, [hm]
f(zero, dgen, hftag, mftag);
f(zero, dgen, htag, mtag);
f(zero, dgen, hptag, mptag);
f(zero, dgen, hftag, mftag, hm);
f(zero, dgen, htag, mtag, hm);
f(zero, dgen, hptag, mptag, hm);
f(hmdist, dgen, hftag, mftag);
f(hmdist, dgen, htag, mtag);
f(hmdist, dgen, hptag, mptag);
f(hmdist, dgen, hftag, mftag, hm);
f(hmdist, dgen, htag, mtag, hm);
f(hmdist, dgen, hptag, mptag, hm);
// ar word
f(zero, dgen, hword, htag, mword, mtag);
f(zero, dgen, htag, mword, mtag);
f(zero, dgen, hword, htag, mtag);
f(zero, dgen, hword, htag, mword, mtag, hm);
f(zero, dgen, htag, mword, mtag, hm);
f(zero, dgen, hword, htag, mtag, hm);
// ar word
f(hmdist, dgen, hword, htag, mword, mtag);
f(hmdist, dgen, htag, mword, mtag);
f(hmdist, dgen, hword, htag, mtag);
f(hmdist, dgen, hword, htag, mword, mtag, hm);
f(hmdist, dgen, htag, mword, mtag, hm);
f(hmdist, dgen, hword, htag, mtag, hm);
// 2.k. dep.
// [dgen], fulltag, fulltag, fulltag, [ghm]
// [dgen], tagP, tagP, tagP, [ghm]
// [dgen], tag, tag, tag, [ghm]
f(dgen, gftag, hftag, mftag);
f(dgen, gptag, hptag, mptag);
f(dgen, gtag, htag, mtag);
f(dgen, gftag, hftag, mftag, ghm);
f(dgen, gptag, hptag, mptag, ghm);
f(dgen, gtag, htag, mtag, ghm);
}
FeatureVector::Value scoreDegenICS(const FeatureVector& features, vector<Feature>& localFeatures, const Span& cs, const Span& rcs)
{
FeatureVector::Value score = 0;
const Token* gtoken = cs.gtoken;
const Token* htoken = cs.htoken;
const Token* mtoken = rcs.htoken;
localFeatures.clear();
bool dh = cs.type == Span::Terminal;
bool dim = rcs.type == Span::Terminal;
getFeaturesDegenICS(localFeatures, gtoken, htoken, mtoken, dh, dim);
for(const Feature& feature : localFeatures)
score += features(feature);
return score;
}
FeatureVector::Value scoreDegenCS(const FeatureVector& features, vector<Feature>& localFeatures, const Span& ics)
{
FeatureVector::Value score = 0;
const Token* gtoken = ics.gtoken;
const Token* htoken = ics.htoken;
const Token* mtoken = ics.mtoken;
localFeatures.clear();
bool dh = ics.ics.cs->type == Span::Terminal;
bool dim = ics.ics.rcs->type == Span::Terminal;
getFeaturesDegenCS(localFeatures, gtoken, htoken, mtoken, dim, dh);
for(const Feature& feature : localFeatures)
score += features(feature);
return score;
}
//
// Tipiski iezīmes vajag izvilkt pie incomplete span veidošanas, jo tur parārādās g-h-m saites
// Papildus var apskatīt deģenerētos gadījumus: h=r un r+1=m, tas notiek incomplete span veidošanas procesā
// Ir vēl viens gadījums, kas ir jāiekļauj complete span veidošanas procesā: m=e
//
// Algoritms:
// * funkcija, kas aprēķina score neko nezinot par deģenerētajiem gadījumiem
// * funkcija, kas zina par incomplete deģenerētajiem gadījumiem h=r un r+1=m un rēķina tikai tos
// * funkcija, kas zina par complete deģenerēto gadījumu m=e un arī incomplete, bet rēķina tikai complete
// atrākais veids kā veikt extract features ?
// jāņem vērā arī deģenerētos gadījumus, no kuriem ir izsaukumi arī no complete span veidošanas punkta
// vēl ir jautājums par atmiņas reģionu: nav vajadzības veikt jaunas atmiņas alloc, tāpēc, ka pats process neparalelizējas,
// bet ja ir vietas, kuras paralelizējas, tad pietiketu ar veinu features vektoru uz pavediena
void parse(Tokens& tokens, const FeatureVector& features, bool ner)
{
int n = tokens.size();
SpanStore spans(n);
// darbojas kā buferis, lai nav dinamiski jārezervē atmiņa
vector<Feature> localFeatures;
localFeatures.reserve(256);
// mehānisms, kas ļauj izmantot NER noteiktās spanu robežas
// struktūra, lai atzīmētu stāvokli
typedef enum {
TokenSpanNone = 0,
TokenSpanEdge = 0x10,
TokenSpanBegin = 0x11,
TokenSpanEnd = 0x12,
TokenSpanInner = 0x100
} TokenSpanType;
typedef struct {
int nr;
TokenSpanType type;
} TokenSpanInfo;
vector<TokenSpanInfo> tokenSpans(n, {0, TokenSpanNone});
if(ner)
{
// izveido tokenu spanu nr. karti
int currentNr = 1;
for(int i=1; i<n; i++) // 0-tais ir root
{
// cout << tokens[i].namedEntityType() << endl;
if(!tokens[i].namedEntityType().empty())
{
if(tokens[i].namedEntityType() != tokens[i-1].namedEntityType())
currentNr += 1;
tokenSpans[i].nr = currentNr;
}
}
// dzēš tos, kuru izmēri ir tikai viens tokens
for(int i=1; i<n-1; i++)
{
if(tokenSpans[i-1].nr != tokenSpans[i].nr && tokenSpans[i].nr != tokenSpans[i+1].nr)
tokenSpans[i].nr = 0;
}
// apstrādā pēdējo tokenu
if(n == 1 || tokenSpans[n-2].nr != tokenSpans[n-1].nr)
tokenSpans[n-1].nr = 0;
// uzstāda tipu
for(int i=1; i<n-1; i++)
{
if(tokenSpans[i].nr > 0 && tokenSpans[i].nr != tokenSpans[i-1].nr)
tokenSpans[i].type = TokenSpanBegin;
// nedrīkstētu būt tokenu kopa, kas sastāv tikai no viena tokena, tāpēc else
else if(tokenSpans[i].nr > 0 && tokenSpans[i].nr != tokenSpans[i+1].nr)
tokenSpans[i].type = TokenSpanEnd;
else if(tokenSpans[i].nr > 0)
tokenSpans[i].type = TokenSpanInner;
}
// apstrādā pēdējo tokenu
if(tokenSpans[n-1].nr > 0)
tokenSpans[n-1].type = TokenSpanEnd;
// debug output
// for(int i=0; i<n; i++) // 0-tais ir root
// {
// cout << i << " : " << tokenSpans[i].nr << " type=" << tokenSpans[i].type << endl;
// }
}
// potenciāli nelegāli spani ir tikai tie, kuriem abos galos ir atšķirīgi nr
// nelegāls spans, ja viens no galiem ir inner un otrs no galiem ir ar citu nr
// nelegāls spans, ja viens no galiem ir edge, bet otrs ir ar citu nr tajā pašā pusē, t.i.,
// ja begin, tad ar mazāku indeksu, ja end, tad ar lielāku indeksu
#define INVALID_SPAN(i,j) (tokenSpans[i].nr != tokenSpans[j].nr && ((tokenSpans[i].type != TokenSpanBegin && tokenSpans[i].type != TokenSpanNone) || (tokenSpans[j].type != TokenSpanEnd && tokenSpans[j].type != TokenSpanNone)))
FeatureVector::Value score;
for(int w=0; w<n; w++) // span'a platums
{
for(int i=0; i<n-w; i++) // i indekss
{
int j = i + w; // j indekss
// if(INVALID_SPAN(i,j))
// continue;
// multiroot gadījumā g ir no -1
// for(int g=-1; g<n; g++) // parent/grandparent - g indekss
// single root gadījumā g ir no 0, kas atbilst root tokenam
for(int g=0; g<n; g++) // parent/grandparent - g indekss
{
if(g >= i && g <= j) // jābūt ārpus [i,j] intervāla
continue;
// terminal spans
if(w == 0) // i == j - TerminalSpan
{
spans(g, i, i).complete.terminal(g == -1 ? nullptr : &tokens[g], &tokens[i]);
continue;
}
// forward
{
SpanStore::Spans& gij = spans(g, i, j);
// incomplete = complete + reverse complete
{
Span& span = gij.incomplete;
// Span& span = spans(g, i, j).incomplete;
bool first = true;
for(int r=i; r<j; r++)
{
const Span& cs = spans(g, i, r).complete;
const Span& rcs = spans(i, j, r+1).complete;
if(cs.type == Span::Undefined || rcs.type == Span::Undefined)
continue;
score = cs.score + rcs.score;
if(r==i || r+1 == j) // deģenerētie gadījumi
score += scoreDegenICS(features, localFeatures, cs, rcs);
// if(r == i || score > span.score) // bez r == i neiztikt, jo score var būt mazāks par 0, nav zināma minimālā vērtība
if(first || score > span.score) // bez r == i neiztikt, jo score var būt mazāks par 0, nav zināma minimālā vērtība
span.set(cs, rcs, score);
first = false;
}
if(!first)
span.incomplete(features, localFeatures);
}
// complete = incomplete + complete
if(!INVALID_SPAN(i,j))
{
Span& span = gij.complete;
// Span& span = spans(g, i, j).complete;
bool first = true;
for(int m=i+1; m<=j; m++)
{
const Span& ics = spans(g, i, m).incomplete;
const Span& cs = spans(i, m, j).complete;
if(cs.type == Span::Undefined || ics.type == Span::Undefined)
continue;
score = ics.score + cs.score;
if(m == j)
score += scoreDegenCS(features, localFeatures, ics);
// if(m == i+1 || score > span.score)
if(first || score > span.score)
span.set(ics, cs, score);
first = false;
}
if(!first)
span.complete();
}
}
// reverse
{
SpanStore::Spans& gji = spans(g, j, i);
// reverse incomplete = complete + reverse complete
{
Span& span = gji.incomplete;
// Span& span = spans(g, j, i).incomplete;
bool first = true;
for(int r=i; r<j; r++)
{
if(INVALID_SPAN(i,r) || INVALID_SPAN(r+1,j))
continue;
const Span& cs = spans(g, j, r+1).complete;
const Span& rcs = spans(j, i, r).complete;
if(cs.type == Span::Undefined || rcs.type == Span::Undefined)
continue;
score = cs.score + rcs.score;
if(r==i || r+1 == j)
score += scoreDegenICS(features, localFeatures, cs, rcs);
// if(r == i || score > span.score)
if(first || score > span.score)
span.set(cs, rcs, score);
first = false;
}
if(!first)
span.incomplete(features, localFeatures);
}
// reverse complete = incomplete + complete
if(!INVALID_SPAN(i,j))
{
Span& span = gji.complete;
// Span& span = spans(g, j, i).complete;
bool first = true;
for(int m=i; m<j; m++)
{
const Span& ics = spans(g, j, m).incomplete;
const Span& cs = spans(j, m, i).complete;
if(cs.type == Span::Undefined || ics.type == Span::Undefined)
continue;
score = ics.score + cs.score;
if(m == i)
score += scoreDegenCS(features, localFeatures, ics);
// if(m == i || score > span.score)
if(first || score > span.score)
span.set(ics, cs, score);
first = false;
}
if(!first)
span.complete();
}
}
}
}
}
// salinko parentus kokā
// vispārīgāks gadījums: multiroot
// tokens.link(&spans(-1, 0, tokens.size()-1).complete);
// ja grib single root, tad ērtākais būs manuāli sameklēt dalījuma punktu un uzbūvēt pagaidu pilno spanu, kas ietvers incomplete spanu
// (ar bultu uz savienojuma vietu) ar deģenerētu root spanu un pa kreisi vērsto spanu complete spanu + pa labi vērsto komplete spanu
int maxm;
for(int m=1; m<n; m++)
{
const Span& rcs = spans(0, m, 1).complete;
const Span& cs = spans(0, m, n-1).complete;
if(cs.type == Span::Undefined || rcs.type == Span::Undefined)
continue;
if(INVALID_SPAN(1,m) || INVALID_SPAN(m,n-1))
continue;
if(m == 1 || rcs.score + cs.score > score)
{
maxm = m;
score = rcs.score + cs.score;
}
}
// TODO: single root gadījumā nav vajadzīgi tie complete spani,
// kuriem g = 0 un vismaz viens no galapunktiem nepieskaras teikuma galapunktiem, t.i., =1 vai =n-1
Span ics;
Span root;
root.terminal(nullptr, &tokens[0]);
ics.ics.cs = &root;
ics.ics.rcs = &spans(0, maxm, 1).complete;
ics.incomplete();
Span rootcs;
rootcs.cs.ics = &ics;
rootcs.cs.cs = &spans(0, maxm, n-1).complete;
rootcs.complete();
tokens.link(&rootcs);
}
string normalizedTag(const string& tag)
{
string t;
t += tag[0]; // pirmais simbols būs vienmēr
// #define T(i) t += tag[i]
// Gunta variants
if(tag[0] == 'a')
{
// mpn(P,a,MPN) :- P=[a,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
t += tag[2];
t += tag[3];
t += tag[4];
}
else if(tag[0] == 'm')
{
// mpn(P,m,MPN) :- P=[m,_,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
t += tag[3];
t += tag[4];
t += tag[5];
}
else if(tag[0] == 'n')
{
// mpn(P,n,MPN) :- P=[n,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
t += tag[2]; /* 0.89 */
t += tag[3]; /* 0.91 */
t += tag[4]; /* 0.98 */
}
else if(tag[0] == 'p')
{
// mpn(P,p,MPN) :- P=[p,_,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
t += tag[3];
t += tag[4];
t += tag[5];
}
else if(tag[0] == 'v' && tag[3] == 'p')
{
// mpn(P,vp,MPN) :- P=[v,_,_,p,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
t[0] = 'd';
// t += 'p';
// t += tag[3]; // p
t += tag[5]; /* 0.42 */
t += tag[6]; /* 0.42 */
t += tag[7];
}
// else if(tag[0] == 'v')
// {
// // mpn(P,vp,MPN) :- P=[v,_,_,p,_,M,P,N|_],atom_chars(MPN,[M,P,N]),!.
//
// // t += tag[1]; // tips
// // t += tag[3]; // izteiksme