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
a937724
 
 
 
 
9e3e64a
a937724
 
 
 
9e3e64a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a937724
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

from typing import List, Sequence, Tuple, Optional, Dict, Union, Callable
import pandas as pd
import spacy
from spacy.language import Language
from skbio import diversity as dv

SPAN_ATTRS = ["text", "label_", "start", "end"]
CATEGORIES = ['ATTRIBUTION', "CITATION", "COUNTER", "DENY", "ENDOPHORIC", "ENTERTAIN", "JUSTIFYING", "MONOGLOSS", "PROCLAIM", "SOURCES"]


def simple_table(doc: Union[spacy.tokens.Doc, Dict[str, str]],
                 spans_key: str = "sc",
                 attrs: List[str] = SPAN_ATTRS):
    columns = attrs + ["Conf. score"]
    data = [
        [str(getattr(span, attr))
         for attr in attrs] + [score]  # [f'{score:.5f}']
        for span, score in zip(doc.spans[spans_key], doc.spans[spans_key].attrs['scores'])
    ]
    return data, columns


# def span_info_aggregator()

def construction_classifier(doc, span):
    category = None
    spanroot = span.root

    ## Grabbing lexico-grammatical information
    span_t_dep_ = ["_".join([t.norm_, t.dep_]) for t in span]
    span_dep = [t.dep_ for t in span]
    span_token = [t.norm_ for t in span]
    span_tag = [t.tag_ for t in span]


    c = [c for c in spanroot.children]
    c_t_dep_ = ["_".join([t.norm_, t.dep_]) for t in spanroot.children]

    c_norm = [c.norm_ for c in spanroot.children]
    c_dep = [c.dep_ for c in spanroot.children]
    c_pos = [c.pos_ for c in spanroot.children]
    c_tag = [c.tag_ for c in spanroot.children]

    right_dep = [c.dep_ for c in spanroot.rights]

    #conditionals
    subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
    argmentless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass', "dobj", 'ccomp', 'xcomp', 'dative', "attr", "oprd", "acomp"] for c in spanroot.children)
    argless_span = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass', "dobj", 'ccomp', 'xcomp', 'dative', "attr", "oprd", "acomp"] for c in span)

    ## nesting classifiers
    if spanroot.dep_ == "conj":
        while spanroot.dep_ == 'conj':
            spanroot = spanroot.head
    # if spanroot.dep_ == "poss":
    #     while spanroot.dep_ == 'poss':
    #         spanroot = spanroot.head

    ## Conjunctions 
    # Preconjunctions
    if spanroot.dep_ in ['preconj', 'cc']:
        category = "Conjunction"

    ## NOUN PHRASES
    # adverbial phrases
    if spanroot.dep_ in ['amod']:
        category = "Adjectival modifier"
        # adverbial phrases
    if spanroot.dep_ in ['compound']:
        category = "Compound noun"

    ## Nominal category
    if spanroot.dep_ in ["pobj", "dobj", "obj", "iobj", "dative"]:
        if "acl" in c_dep:
            category = "Noun + Complement (Object)"
        else:
            category = "Object"

    if spanroot.dep_ in ["nsubj", "nsubjpass"]:
        if "acl" in c_dep:
            category = "Noun + Complement (Subject)"
        else:
            category = "Subject"

    ## ADJUNCTS
    # prep phrases
    if spanroot.dep_ in ['prep', 'agent']:
        category = 'Prepositional phrase'
    # adverbial phrases
    if spanroot.dep_ in ['advmod', "npadvmod", "nmod", "npmod", 'quantmod']:
        category = "Adverbial phrase"

    ## Predication patterns
    if spanroot.dep_ in ['acomp', 'oprd']:
        if "xcomp" in c_dep:
            category = "Subject predicate to-cl"
        else:
            category = "Adjectival complement"

    if spanroot.dep_ in ['attr']:
        subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)

        c_head = [c.dep_ for c in spanroot.head.children]
        if "expl" in c_head and "no_det" in span_t_dep_:
            category = "There is/are no NOUN"
        elif "expl" in c_head and spanroot.pos_ in ["NOUN"]:
            category = "There is/are + Noun complement"
        elif "expl" in c_head and spanroot.tag_ in ["NN", "NNS"]:
            category = "There is/are + Noun complement"
        
        elif spanroot.pos_ in ["NOUN", "PRON"]:
            if "acl" in c_dep:
                category = "Noun + Complement (attr)"
            else:
                category = "Nominal complement"

        elif not subjless and spanroot.pos_ in ['VERB', "AUX"]:
            category = "Main verb 4"

        elif spanroot.tag_ in ['NNP']:
            category = "Nominal complement"


    ####################################
    ### clausal ####
    ####################################
    if spanroot.dep_ in ["ROOT", "advcl", "ccomp", 'acl', 'pcomp', 'relcl']:

        _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
        _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]
        root_before_ccomp = [c.i > spanroot.i for c in spanroot.children if c.dep_ == "ccomp"]

        _check_for_to = ["_".join([c.norm_, c.dep_])  for c in spanroot.subtree if c.head.dep_ == "advcl" and (c.dep_=="mark" or c.dep_ == "aux")]
        entire_cl = spanroot.left_edge.i == span.start and spanroot.right_edge.i == span.end

        ## Start with broad category, which is then re-evaluated for specific constructions.
        if spanroot.dep_ in ['advcl', 'mark', 'acl', 'pcomp']:
            ## Adverbial clauses
            ### Finite-adverbial clauses
            ### Non-finite adverbial clauses
            subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
            entire_cl = spanroot.left_edge.i == span.start and spanroot.right_edge.i == span.end

            if "mark" in span_dep and spanroot.pos_ in ['VERB', "AUX"]:
                category = "Finite adverbial clause"
            elif "mark" in span_dep and "aux" in span_dep :
                category = "Finite adverbial clause"

            elif "mark" in span_dep and spanroot.pos_ in ['VERB', "AUX"] and "expl" in c_dep:
                category = "Finite adverbial clause"

            elif "advmod" in span_dep and ("WRB" in span_tag or "WDT" in span_tag):
                if spanroot.pos_ in ['VERB', "AUX"]:
                    category = "Finite adverbial clause"

                elif spanroot.pos_ not in ['VERB', "AUX"] and subjless:
                    category = "Non-finite adv clause 1"

                elif entire_cl:
                    category = "Finite adverbial clause"

            elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part", "Aspect=Perf|Tense=Past|VerbForm=Part"] and "aux" not in c_dep:
                # he doing his job
                if argmentless:
                    #e.g., frankly speaking, strictly speaking
                    category = "Adverbial Phrase"
                else:
                    category = "Non-finite adv clause 2"

            elif spanroot.pos_ not in ['VERB', "AUX"] and "mark" in span_dep and subjless:

                category = "Non-finite adv clause 3"
            
            elif "aux" in c_dep and "TO" in c_tag:
                category = "Adverbial Phrase"


            elif "mark" not in span_dep and spanroot.pos_ in ['VERB', "AUX"]:
                category = "Dependent Verb phrase"
            
            elif not argmentless:
                category =  "Adverbial clause"
            
            elif spanroot.dep_ == "advcl":
                category =  "Adverbial phrase"


        if spanroot.dep_ in ['relcl', 'ccomp', 'acl']:

            head = spanroot.head
            if ";" in [t.norm_ for t in head.children]:
                category = "Main verb 3"
            elif "nsubj" not in span_dep:
                category = "Dependent verb 1"
            elif "mark" in span_dep:
                category = "Complement clause"
            elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part", "Aspect=Perf|Tense=Past|VerbForm=Part"] and "aux" not in c_dep:
                category = "Non-finite complement clause"
            elif spanroot.dep_ in ['relcl']:
                category = "Relative clause"
            elif spanroot.dep_ in ['ccomp']:
                category = "Complement clause"
            elif spanroot.dep_ in ['acl']:
                category = "Noun Complement clause"
            else:
                # print(_check_for_to)
                category = "this one"

        ## Specific constructions
        # Extraposed that-clause or to-infinitives
        if ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and spanroot.pos_ in ["VERB", "AUX"]:
            print(c_dep)
            if ("acomp" in c_dep or "oprd" in c_dep) and "ccomp" in c_dep:
                #eg it seems odd (oprd) that X.
                #eg it is certain (acomp) that X.
                category = "Extraposed that-cl (adj-complement)" #e.g., it is certain that X.

            elif "xcomp" in c_dep or ("advcl" in c_dep):
                if "for_mark" in _check_for_to:
                    category = "Extraposed to-cl (explicit subj)" #eg It is possible to .
                elif _check_to:
                    category = "Extraposed to-cl 1" #eg It is possible to .
                elif _check_ing:
                    category = "Extraposed -ing 1" #eg It is possible to .
            elif ("prep" in right_dep or "npadvmod" in right_dep) and "ccomp" in right_dep and spanroot.lemma_ == "be":
                category = "Cleft construction" 

            elif "attr" in c_dep:
                category = "Extraposed that-cl (copula)" #eg It is a wonder that X.

            else:
                category = "Extraposed that-cl (VERB)" 

        # if "ccomp" in c_dep and "auxpass" in c_dep and ("it_nsubjpass" in span_t_dep_ or "it_nsubj" in span_t_dep_):
        #     category = "Extraposed that-cl (VERB)1" #e.g., it has been shown that X.
        elif ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and "acomp" in c_dep:
            if "xcomp" in c_dep:
                if _check_to:
                    category = "Extraposed to-cl 2" #eg it is difficult to decide.
                elif _check_ing:
                    category = "Extraposed -ing 2" #eg it is difficult to decide.
            
            else:
                category = "Extraposed that-cl (adj-complement) 2"

        elif ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and "oprd" in c_dep:

            category = "Extraposed that-cl (adj-complement) 3" #eg it seems odd that X.

        
        # something without dummy subject "it"
        elif (("nsubj" in c_dep and spanroot.lemma_ in ['be']) or "nsubjpass" in c_dep) and spanroot.pos_ in ["AUX", 'VERB'] and "it" not in c_norm:
            
            # store xcomp, if the head of the xcomp is acomp
            _check_xcomp = [c.dep_ for c in spanroot.subtree if c.dep_ in ["xcomp"] and c.head.dep_ == "acomp"]
            _check_ccomp = [c.dep_ for c in spanroot.subtree if c.dep_ in ["ccomp"] and c.head.dep_ == "acomp"]
            # _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
            # _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]
            

            if ("attr" in c_dep or "acomp" in c_dep) and "ccomp" in c_dep:
                if any(root_before_ccomp):
                    category = "Post-predicate that-cl"
                else:
                    category = "Comment clause"

            elif ("attr" in c_dep or "acomp" in c_dep) and "ccomp" in _check_ccomp:
                category = "Post-predicate that-cl 2"

            elif ("attr" in c_dep or "acomp" in c_dep) and "xcomp" in _check_xcomp:
                category = "Post-predicate to-cl"

            elif "xcomp" in c_dep and spanroot.lemma_ in ['be'] and _check_to:
                category = "Subject predicate to-cl"

            elif "xcomp" in c_dep and "auxpass" in c_dep and _check_to:
                category = "Subject predicate to-cl (passive)"

            elif "xcomp" in c_dep and spanroot.lemma_ in ['be'] and _check_ing:
                category = "Subject predicate -ing"
            elif "ccomp" in c_dep:
                category = "Subject predicate that-cl"
            elif "acomp" in c_dep:
                category = "Adjectival predicate"

            elif "mark" in c_dep and ("nsubj" in c_dep or "nsubjpass" in c_dep):
                category = "Finite-adverbial clause"
            else:
                category = "Main verb 1"

        ## without dummy subject it, and lexical verbs
        elif ("nsubj" in c_dep or "nsubjpass" in c_dep)  in c_dep and spanroot.pos_ in ["AUX", 'VERB'] and "it" not in c_norm and spanroot.lemma_ not in ['be']:
            _check_wh = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["attr", "advmod", 'dobj', 'nsubj'] and c.tag_ in ["WP", "WRB", "WDT", "WP$"]) and c.head.dep_ == "ccomp"]
            _check_if = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["mark"] and c.norm_ in ["whether", "if"]) and c.head.dep_ == "ccomp"]

            # _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
            # _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]

            if "ccomp" in c_dep and (_check_wh or _check_if):
                category = "Post-predicate wh-cl"

            elif "ccomp" in c_dep:
                if any(root_before_ccomp):
                    category = "Post-predicate that-cl"
                else:
                    category = "Comment clause"

            elif "xcomp" in c_dep:
                if _check_to:
                    category = "Post-predicate to-cl"
                elif _check_ing:
                    category = "Post-predicate -ing"

        # Existential
        elif "expl" in c_dep and "NOUN" in c_pos and "mark" not in c_dep:
            category = "There is/are NOUN"
            
        elif "ccomp" in c_dep and "it_nsubj" in span_t_dep_ and spanroot.pos_ in ["AUX"]:
            category = "Cleft construction"


    if spanroot.dep_ in ['parataxis']:
        if "_".join(span_dep) in ["nsubj_parataxis", "aux_parataxis", "nsubj_aux_parataxis"]:
            category = "Comment clause"
        else:
            category = "parataxis (for now)"
    

    ## External comp
    if spanroot.dep_ in ['xcomp']:
        if spanroot.head.pos_ == 'ADJ' and "to_aux" in c_t_dep_:
            category = "Adjective complement to-cl"
        if spanroot.head.pos_ == 'VERB' and "to_aux" in c_t_dep_:
            category = "Verb complement to-cl"
    
    if spanroot.dep_ in ['pcomp']:
        if str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"] and 'ccomp' in c_dep:
            category = "Participle + that-cl"
        elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"]:
            category = "Participle"

    ## Simple classifier
    # if spanroot.dep_ in ['pcomp']:
    #     if str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"]:
    #         category = "Gerund"

    if spanroot.dep_ in ['neg']:
        category = "Negative particle"
    if spanroot.dep_ in ['aux', 'auxpass']:
        category = "Auxiliary"

    # Modal verbs
    if spanroot.tag_ == "MD":
        category = "Modal auxiliary"


    if spanroot.dep_ in ['dep', "csubj", 'csubjpass']:
        if spanroot.head.dep_ in ['ROOT', 'ccomp'] and spanroot.head.pos_ in ['AUX', 'VERB'] and spanroot.pos_ in ['AUX', 'VERB']:
            if spanroot.morph == spanroot.head.morph:
                category = "Main verb 4"
            else:
                category = "Dependent verb 2"
        elif str(spanroot.morph) == "Aspect=Prog|Tense=Pres|VerbForm=Part":
            category = "Gerund"
        elif spanroot.head.dep_ in ['conj', 'acl','relcl']:
            if spanroot.morph == spanroot.head.morph:
                category = "Main verb 4"
            else:
                category = "Dependent verb 2"
        elif "VerbForm=Fin" in str(spanroot.morph):
            category = "Dependent verb 2"

    # Appositive phrases
    if spanroot.dep_ in ['appos']:
        if "nummod" in c_dep:
            category = "Apposition"
        elif spanroot.pos_ in ["PROPN"]:
            category = "Appositive Proper Nouns"
        elif spanroot.pos_ in ["NOUN"]:
            category = "Appositive Noun Phrase"
        elif spanroot.pos_ in ["VERB", "AUX"]:
            _check = any(c.dep_ in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
            if _check:
                category = "Appositive Finite-clause"
            
    if spanroot.dep_ in ['appos', "dep", "attr"]:
        if not subjless and spanroot.pos_ in ['VERB', "AUX"]:
            category = "Main verb 5"

    if spanroot.dep_ in ["dep", "mark"]:
        if spanroot.tag_ in ["RB", "IN", "CC"]:
            category = "Conjunction"


    #sometimes the extra-clausal links are not accurate
    if spanroot.dep_ in ['aux', "auxpass", 'oprd', 'appos', "xcomp"]:
        if spanroot.head.dep_ == "ROOT":
            category = "Main verb"
        else:
            category = "dependent verb 5"

    if span.label_ == "CITATION":
        if "NNP" in span_tag or "NNPS" in span_tag:
            if span_dep[0] == 'punct' and span_dep[-1] == 'punct':
                category = "Parenthetical Citation"
            elif span_tag[0] in ["NNP", "NNPS"]:
                category = "Narrative Citation"
        else:
            category = "Other Citation"

    if category == None:
        category = spanroot.dep_

    return category


def construction_classifier2(doc, span):
    category = None
    spanroot = span.root

    ## Grabbing lexico-grammatical information
    span_t_dep_ = ["_".join([t.norm_, t.dep_]) for t in span]
    span_dep = [t.dep_ for t in span]
    span_token = [t.norm_ for t in span]
    span_tag = [t.tag_ for t in span]


    c = [c for c in spanroot.children]
    c_t_dep_ = ["_".join([t.norm_, t.dep_]) for t in spanroot.children]

    c_norm = [c.norm_ for c in spanroot.children]
    c_dep = [c.dep_ for c in spanroot.children]
    c_pos = [c.pos_ for c in spanroot.children]
    c_tag = [c.tag_ for c in spanroot.children]

    right_dep = [c.dep_ for c in spanroot.rights]

    #conditionals
    subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
    argmentless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass', "dobj", 'ccomp', 'xcomp', 'dative', "attr", "oprd", "acomp"] for c in spanroot.children)
    argless_span = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass', "dobj", 'ccomp', 'xcomp', 'dative', "attr", "oprd", "acomp"] for c in span)
    argless_span = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass', "dobj", 'ccomp', 'xcomp', 'dative', "attr", "oprd", "acomp"] for c in span)


    ## nesting classifiers
    if spanroot.dep_ == "conj":
        while spanroot.dep_ == 'conj':
            spanroot = spanroot.head

    if spanroot.dep_ == "poss":
        head = spanroot.head
        if head.dep_ in ["pobj", "dobj", "obj", "iobj" , "dative"]:
            category = "Posessive Noun (Object)"
        elif head.dep_ in ["nsubj", "nsubjpass"]:
            category = "Posessive Noun (Subject)"
        else:
            category = "Posessive Noun (Other)"


    ## Conjunctions 
    # Preconjunctions
    if spanroot.dep_ in ['preconj', 'cc']:
        category = "Conjunction"

    ## NOUN PHRASES
    # adverbial phrases
    if spanroot.dep_ in ['amod']:
        category = "Adjectival modifier"
        # adverbial phrases
    if spanroot.dep_ in ['compound']:
        category = "Compound noun"

    ## Nominal category
    if spanroot.dep_ in ["pobj", "dobj", "obj", "iobj" , "dative"]:
        if "acl" in c_dep:
            category = "Noun + Complement (Object)"
        else:
            category = "Object"

    if spanroot.dep_ in ["nsubj", "nsubjpass"]:
        if "acl" in c_dep:
            category = "Noun + Complement (Subject)"
        else:
            category = "Subject"

    ## ADJUNCTS
    # prep phrases
    if spanroot.dep_ in ['prep', 'agent']:
        category = 'Prepositional phrase'

    # adverbial phrases
    if spanroot.dep_ in ['advmod', "npadvmod", "nmod", "npmod", 'quantmod',  'nummod']:
        category = "Adverbial phrase"

    ## Predication patterns
    if spanroot.dep_ in ['acomp', 'oprd']:
        if "xcomp" in c_dep:
            category = "Subject predicate to-cl"
        else:
            category = "Adjectival complement"

    if spanroot.dep_ in ['attr']:
        subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)

        c_head = [c.dep_ for c in spanroot.head.children]
        if "expl" in c_head and "no_det" in span_t_dep_:
            category = "There is/are no NOUN"
        elif "expl" in c_head and spanroot.pos_ in ["NOUN"]:
            category = "There is/are + Noun complement"
        elif "expl" in c_head and spanroot.tag_ in ["NN", "NNS"]:
            category = "There is/are + Noun complement"
        
        elif spanroot.pos_ in ["NOUN", "PRON"]:
            if "acl" in c_dep:
                category = "Noun + Complement (attr)"
            else:
                category = "Nominal complement"

        elif not subjless and spanroot.pos_ in ['VERB', "AUX"]:
            category = "Main verb 4"

        elif spanroot.tag_ in ['NNP']:
            category = "Nominal complement"

    ## External comp
    if spanroot.dep_ in ['xcomp']:
        if spanroot.head.pos_ == 'ADJ' and "to_aux" in c_t_dep_:
            category = "Adjective complement to-cl"
        if spanroot.head.pos_ == 'VERB' and "to_aux" in c_t_dep_:
            category = "Verb complement to-cl"
    
    if spanroot.dep_ in ['pcomp']:
        if str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"] and 'ccomp' in c_dep:
            category = "Participle + that-cl"
        elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"]:
            category = "Participle"

    ## Simple classifier
    # if spanroot.dep_ in ['pcomp']:
    #     if str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part"]:
    #         category = "Gerund"

    if spanroot.dep_ in ['neg']:
        category = "Negative particle"
    if spanroot.dep_ in ['aux', 'auxpass']:
        category = "Auxiliary"

    # Modal verbs
    if spanroot.tag_ == "MD":
        category = "Modal auxiliary"


    ####################################
    ### clausal ####
    ####################################
    if spanroot.dep_ in ["ROOT", "advcl", "ccomp", 'acl', 'pcomp', 'relcl', 'punct']:

        _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
        _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]
        root_before_ccomp = [c.i > spanroot.i for c in spanroot.children if c.dep_ == "ccomp"]

        _check_for_to = ["_".join([c.norm_, c.dep_])  for c in spanroot.subtree if c.head.dep_ == "advcl" and (c.dep_=="mark" or c.dep_ == "aux")]
        entire_cl = spanroot.left_edge.i == span.start and spanroot.right_edge.i == span.end


        ## Start with broad category, which is then re-evaluated for specific constructions.
        if spanroot.dep_ in ['advcl', 'acl', 'punct', 'pcomp']: #'mark', 
            ## Adverbial clauses
            subjless = all(c.dep_ not in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
            entire_cl = spanroot.left_edge.i == span.start and spanroot.right_edge.i == span.end

            ### Finite-adverbial clauses
            if "mark" in span_dep and (spanroot.pos_ in ['VERB', "AUX"] or "aux" in span_dep ):
                category = "Finite adverbial clause"

            elif "mark" in span_dep and "aux" in span_dep :
                category = "Finite adverbial clause"

            elif "mark" in span_dep and spanroot.pos_ in ['VERB', "AUX"] and "expl" in c_dep:
                category = "Finite adverbial clause"

            elif "advmod" in span_dep and ("WRB" in span_tag or "WDT" in span_tag):
                if spanroot.pos_ in ['VERB', "AUX"]:
                    category = "Finite adverbial clause"

                elif spanroot.pos_ not in ['VERB', "AUX"] and subjless:
                    category = "Non-finite adv clause 1"

                elif not argmentless:
                    category =  "Finite adverbial clause"

            ## non-finite
            elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part", "Aspect=Perf|Tense=Past|VerbForm=Part"] and "aux" not in c_dep:
                # he doing his job
                if argmentless:
                    #e.g., frankly speaking, strictly speaking
                    category = "Adverbial Phrase"
                else:
                    category = "Non-finite adv clause 2"

            elif spanroot.pos_ not in ['VERB', "AUX"] and "mark" in span_dep and subjless:

                category = "Non-finite adv clause 3"
            
            elif "aux" in c_dep and "TO" in c_tag:
                category = "Adverbial Phrase"


            elif "mark" not in span_dep and spanroot.pos_ in ['VERB', "AUX"]:
                category = "Dependent Verb phrase"
            
            elif not argmentless:
                category =  "Adverbial clause"
            
            elif spanroot.dep_ == "advcl":
                category =  "Adverbial phrase"
            
            else:
                category = "Finite adverbial clause "

        if spanroot.dep_ in ['relcl', 'ccomp', 'acl', 'punct', "pcomp"]:
            
            head = spanroot.head
            if ";" in [t.norm_ for t in head.children]:
                category = "Main verb 3"

            elif "nsubj" not in span_dep:
                category = "Dependent verb 1"

            elif "mark" in span_dep:
                category = "Complement clause"
            elif str(spanroot.morph) in ["Aspect=Prog|Tense=Pres|VerbForm=Part", "Aspect=Perf|Tense=Past|VerbForm=Part"] and "aux" not in c_dep:
                category = "Non-finite complement clause"
            elif spanroot.dep_ in ['relcl']:
                category = "Relative clause"
            elif spanroot.dep_ in ['ccomp']:
                category = "Complement clause"
            elif spanroot.dep_ in ['acl']:
                category = "Noun Complement clause"

        ## Specific constructions
        # Extraposed that-clause or to-infinitives
        if ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and spanroot.pos_ in ["VERB", "AUX"]:
            # print(c_dep)
            if ("acomp" in c_dep or "oprd" in c_dep) and "ccomp" in c_dep:
                #eg it seems odd (oprd) that X.
                #eg it is certain (acomp) that X.
                category = "Extraposed that-cl (adj-complement)" #e.g., it is certain that X.

            elif "xcomp" in c_dep or ("advcl" in c_dep):
                if "for_mark" in _check_for_to:
                    category = "Extraposed to-cl (explicit subj)" #eg It is possible to .
                elif _check_to:
                    category = "Extraposed to-cl 1" #eg It is possible to .
                elif _check_ing:
                    category = "Extraposed -ing 1" #eg It is possible to .
            elif ("prep" in right_dep or "npadvmod" in right_dep) and "ccomp" in right_dep and spanroot.lemma_ == "be":
                category = "Cleft construction" 

            elif "attr" in c_dep:
                category = "Extraposed that-cl (copula)" #eg It is a wonder that X.

            else:
                category = "Extraposed that-cl (VERB)" 

        # if "ccomp" in c_dep and "auxpass" in c_dep and ("it_nsubjpass" in span_t_dep_ or "it_nsubj" in span_t_dep_):
        #     category = "Extraposed that-cl (VERB)1" #e.g., it has been shown that X.
        elif ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and "acomp" in c_dep:
            if "xcomp" in c_dep:
                if _check_to:
                    category = "Extraposed to-cl 2" #eg it is difficult to decide.
                elif _check_ing:
                    category = "Extraposed -ing 2" #eg it is difficult to decide.
            
            else:
                category = "Extraposed that-cl (adj-complement) 2"

        elif ("it_nsubjpass" in c_t_dep_ or "it_nsubj" in c_t_dep_) and "oprd" in c_dep:

            category = "Extraposed that-cl (adj-complement) 3" #eg it seems odd that X.

        
        # something without dummy subject "it"
        elif (("nsubj" in c_dep and spanroot.lemma_ in ['be']) or "nsubjpass" in c_dep) and spanroot.pos_ in ["AUX", 'VERB'] and "it" not in c_norm:
            
            # store xcomp, if the head of the xcomp is acomp
            _check_xcomp = [c.dep_ for c in spanroot.subtree if c.dep_ in ["xcomp"] and c.head.dep_ == "acomp"]
            _check_ccomp = [c.dep_ for c in spanroot.subtree if c.dep_ in ["ccomp"] and c.head.dep_ == "acomp"]
            # _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
            # _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]
            

            if ("attr" in c_dep or "acomp" in c_dep) and "ccomp" in c_dep:
                if any(root_before_ccomp):
                    category = "Post-predicate that-cl"
                else:
                    category = "Comment clause"

            elif ("attr" in c_dep or "acomp" in c_dep) and "ccomp" in _check_ccomp:
                category = "Post-predicate that-cl 2"

            elif ("attr" in c_dep or "acomp" in c_dep) and "xcomp" in _check_xcomp:
                category = "Post-predicate to-cl"

            elif "xcomp" in c_dep and spanroot.lemma_ in ['be'] and _check_to:
                category = "Subject predicate to-cl"

            elif "xcomp" in c_dep and "auxpass" in c_dep and _check_to:
                category = "Subject predicate to-cl (passive)"

            elif "xcomp" in c_dep and spanroot.lemma_ in ['be'] and _check_ing:
                category = "Subject predicate -ing"
            elif "ccomp" in c_dep:
                category = "Subject predicate that-cl"
            elif "acomp" in c_dep:
                category = "Adjectival predicate"

            elif "mark" in c_dep and ("nsubj" in c_dep or "nsubjpass" in c_dep):
                category = "Finite-adverbial clause"
            elif not argmentless and "SCONJ" in c_pos:
                category = "Finite-adverbial clause"
            else:
                category = "Main verb 1"

        ## without dummy subject it, and lexical verbs
        elif ("nsubj" in c_dep or "nsubjpass" in c_dep)  in c_dep and spanroot.pos_ in ["AUX", 'VERB'] and "it" not in c_norm and spanroot.lemma_ not in ['be']:
            _check_wh = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["attr", "advmod", 'dobj', 'nsubj'] and c.tag_ in ["WP", "WRB", "WDT", "WP$"]) and c.head.dep_ == "ccomp"]
            _check_if = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["mark"] and c.norm_ in ["whether", "if"]) and c.head.dep_ == "ccomp"]

            # _check_to = [c.dep_ for c in spanroot.subtree if (c.dep_ in ["aux"] and c.pos_ in ["PART", "SCONJ"]) and c.head.dep_ == "xcomp"]
            # _check_ing = [c.dep_ for c in spanroot.subtree if "Prog" in str(c.morph) and c.dep_ == "xcomp"]

            if "ccomp" in c_dep and (_check_wh or _check_if):
                category = "Post-predicate wh-cl"

            elif "ccomp" in c_dep:
                if any(root_before_ccomp):
                    category = "Post-predicate that-cl"
                else:
                    category = "Comment clause"

            elif "xcomp" in c_dep:
                if _check_to:
                    category = "Post-predicate to-cl"
                elif _check_ing:
                    category = "Post-predicate -ing"



        # Existential
        elif "expl" in c_dep and "NOUN" in c_pos and "mark" not in c_dep:
            category = "There is/are NOUN"
            
        elif "ccomp" in c_dep and "it_nsubj" in span_t_dep_ and spanroot.pos_ in ["AUX"]:
            category = "Cleft construction"

        ### The end of clausal analysis
    
    if spanroot.dep_ in ['parataxis']:
        if "_".join(span_dep) in ["nsubj_parataxis", "aux_parataxis", "nsubj_aux_parataxis"]:
            category = "Comment clause"
        else:
            category = "Parataxis"
    

    if spanroot.dep_ in ['dep', "csubj", 'csubjpass']:
        if spanroot.head.dep_ in ['ROOT', 'ccomp'] and spanroot.head.pos_ in ['AUX', 'VERB'] and spanroot.pos_ in ['AUX', 'VERB']:
            if spanroot.morph == spanroot.head.morph:
                category = "Main verb 4"
            else:
                category = "Dependent verb 2"
        elif str(spanroot.morph) == "Aspect=Prog|Tense=Pres|VerbForm=Part":
            category = "Gerund"
        elif "VerbForm=Fin" in str(spanroot.morph) or "VerbForm=Inf" in str(spanroot.morph):
            category = "Dependent verb 2"
        elif spanroot.dep_ in ["csubj", 'csubjpass']:
            category = "Dependent verb (csubj)"


    # Appositive phrases
    if spanroot.dep_ in ['appos']:
        if "nummod" in c_dep:
            category = "Apposition"
        if spanroot.pos_ in ["PROPN"]:
            category = "Appositive Proper Nouns"
        elif spanroot.pos_ in ["NOUN"]:
            category = "Appositive Noun Phrase"
        elif spanroot.pos_ in ["VERB", "AUX"]:
            _check = any(c.dep_ in ['nsubj', 'nsubjpass', 'csubj', 'csubjpass'] for c in spanroot.children)
            if _check:
                category = "Appositive Finite-clause"
    

    if spanroot.dep_ in ['appos', "dep", "attr"]:
        if not subjless and spanroot.pos_ in ['VERB', "AUX"]:
            category = "Main verb (likely parsing error)"

    #sometimes the dep are on the conjunctions
    if spanroot.dep_ in ["dep", "mark"]:
        if spanroot.tag_ in ["RB", "IN", "CC"]:
            category = "Conjunction"

    if spanroot.dep_ in ["intj"]:
        category = "Introjection"


    #sometimes the extra-clausal links are not accurate
    if spanroot.dep_ in ['aux', "auxpass", 'oprd', 'appos', "xcomp", "attr", 'dep', "meta", 'prt'] and category == None:
        if spanroot.head.dep_ == "ROOT":
            category = "Main verb"
        else:
            category = "dependent verb 5"

    if span.label_ == "CITATION":
        if "NNP" in span_tag or "NNPS" in span_tag:
            if span_dep[0] == 'punct' and span_dep[-1] == 'punct':
                category = "Parenthetical Citation"
            elif span_tag[0] in ["NNP", "NNPS"]:
                category = "Narrative Citation"
        else:
            category = "Other Citation"

    if category == None:
        category = spanroot.dep_

    return category



def const_table(doc: Union[spacy.tokens.Doc, Dict[str, str]],
                spans_key: str = "sc",
                attrs: List[str] = SPAN_ATTRS):
    columns = attrs + ["Conf. score", "sent no.", "grammatical realization", 'span dep', "ner", 
                       "POS", 'span dep seq', "TAG sequence", "POS sequence", "head", "head dep", "children", "morphology", "sent"]
    data = []
    # data = span_info_aggregator(doc, columns)
    sentences = {s: i for i, s in enumerate(doc.sents)}

    for span, score in zip(doc.spans[spans_key], doc.spans[spans_key].attrs['scores']):

        span_info = []
        span_info.extend([str(getattr(span, attr)) for attr in attrs])

        span_info.append(score)
        span_info.append(int(sentences[span.sent]))
        span_info.append(construction_classifier2(doc, span))
        span_info.append(span.root.dep_)
        span_info.append(span.root.ent_type_)
        span_info.append(span.root.tag_)
        span_info.append("_".join([t.dep_ for t in span]))
        span_info.append("_".join([t.tag_ for t in span]))
        span_info.append("_".join([t.pos_ for t in span]))
        span_info.append(span.root.head.norm_)
        span_info.append(span.root.head.dep_)
        span_info.append("_".join([c.dep_ for c in span.root.children]))
        span_info.append(span.root.morph)
        span_info.append(span.sent.text.strip())

        data.append(span_info)

    return data, columns


def ngrammar(seq: list, n=2, concat = False, sep = "-"):
    result = []
    n_item = len(seq)
    for idx, item in enumerate(seq):
        if idx + n <= n_item:
            if concat:
                result.append(sep.join(seq[idx: idx + n]))
            else:
                result.append(seq[idx: idx + n])
    return result


def diversity_values(count_vec: list):
    result = {}
    if len(count_vec) == 0:
        count_vec = [0,0,0,0,0,0,0,0,0,0]

    result['shannon'] = dv.alpha.shannon(list(count_vec), base=2)
    result['brillouin_d'] = dv.alpha.brillouin_d(list(count_vec))
    result["simpson_d"] = 1- dv.alpha.simpson(list(count_vec))    
    result['simpson_e'] = dv.alpha.simpson_e(list(count_vec))
    # result['gini_index'] = dv.alpha.gini_index(list(count_vec))
    # result['faith_pd'] = dv.alpha.faith_pd(list(count_vec))

    return result