-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
treebanks.py
1600 lines (1208 loc) · 63.7 KB
/
treebanks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import re
from pathlib import Path
from typing import List, Optional, Union
import flair
from flair.data import Corpus, FlairDataset, Sentence, Token
from flair.datasets.base import find_train_dev_test_files
from flair.file_utils import cached_path
log = logging.getLogger("flair")
class UniversalDependenciesCorpus(Corpus):
def __init__(
self,
data_folder: Union[str, Path],
train_file=None,
test_file=None,
dev_file=None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
"""Instantiates a Corpus from CoNLL-U column-formatted task data such as the UD corpora.
:param data_folder: base folder with the task data
:param train_file: the name of the train file
:param test_file: the name of the test file
:param dev_file: the name of the dev file, if None, dev data is sampled from train
:param in_memory: If set to True, keeps full dataset in memory, otherwise does disk reads
:param split_multiwords: If set to True, multiwords are split (default), otherwise kept as single tokens
:return: a Corpus with annotated train, dev and test data
"""
# find train, dev and test files if not specified
dev_file, test_file, train_file = find_train_dev_test_files(data_folder, dev_file, test_file, train_file)
# get train data
train = UniversalDependenciesDataset(train_file, in_memory=in_memory, split_multiwords=split_multiwords)
# get test data
test = (
UniversalDependenciesDataset(test_file, in_memory=in_memory, split_multiwords=split_multiwords)
if test_file is not None
else None
)
# get dev data
dev = (
UniversalDependenciesDataset(dev_file, in_memory=in_memory, split_multiwords=split_multiwords)
if dev_file is not None
else None
)
super().__init__(train, dev, test, name=str(data_folder))
class UniversalDependenciesDataset(FlairDataset):
def __init__(
self,
path_to_conll_file: Union[str, Path],
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
"""Instantiates a column dataset in CoNLL-U format.
:param path_to_conll_file: Path to the CoNLL-U formatted file
:param in_memory: If set to True, keeps full dataset in memory, otherwise does disk reads
"""
path_to_conll_file = Path(path_to_conll_file)
assert path_to_conll_file.exists()
self.in_memory: bool = in_memory
self.split_multiwords: bool = split_multiwords
self.path_to_conll_file = path_to_conll_file
self.total_sentence_count: int = 0
with open(str(self.path_to_conll_file), encoding="utf-8") as file:
# option 1: read only sentence boundaries as offset positions
if not self.in_memory:
self.indices: List[int] = []
line = file.readline()
position = 0
while line:
line = line.strip()
if line == "":
self.indices.append(position)
position = file.tell()
line = file.readline()
self.total_sentence_count = len(self.indices)
# option 2: keep everything in memory
if self.in_memory:
self.sentences: List[Sentence] = []
while True:
sentence = self._read_next_sentence(file)
if not sentence:
break
self.sentences.append(sentence)
self.total_sentence_count = len(self.sentences)
def is_in_memory(self) -> bool:
return self.in_memory
def __len__(self) -> int:
return self.total_sentence_count
def __getitem__(self, index: int = 0) -> Sentence:
# if in memory, retrieve parsed sentence
if self.in_memory:
sentence = self.sentences[index]
# else skip to position in file where sentence begins
else:
with open(str(self.path_to_conll_file), encoding="utf-8") as file:
file.seek(self.indices[index])
sentence = self._read_next_sentence(file)
return sentence
def _read_next_sentence(self, file) -> Sentence:
line = file.readline()
tokens: List[Token] = []
# current token ID
token_idx = 0
# handling for the awful UD multiword format
current_multiword_text = ""
current_multiword_sequence = ""
current_multiword_first_token = 0
current_multiword_last_token = 0
while line:
line = line.strip()
fields: List[str] = re.split("\t+", line)
# end of sentence
if line == "":
if len(tokens) > 0:
break
# comments or ellipsis
elif line.startswith("#") or "." in fields[0]:
line = file.readline()
continue
# if token is a multi-word
elif "-" in fields[0]:
line = file.readline()
current_multiword_first_token = int(fields[0].split("-")[0])
current_multiword_last_token = int(fields[0].split("-")[1])
current_multiword_text = fields[1]
current_multiword_sequence = ""
if self.split_multiwords:
continue
else:
token = Token(fields[1])
token.add_label("lemma", str(fields[2]))
if len(fields) > 9 and "SpaceAfter=No" in fields[9]:
token.whitespace_after = 0
tokens.append(token)
token_idx += 1
# normal single-word tokens
else:
# if we don't split multiwords, skip over component words
if not self.split_multiwords and token_idx < current_multiword_last_token:
token_idx += 1
line = file.readline()
continue
# add token
token = Token(fields[1], head_id=int(fields[6]))
token.add_label("lemma", str(fields[2]))
token.add_label("upos", str(fields[3]))
token.add_label("pos", str(fields[4]))
token.add_label("dependency", str(fields[7]))
if len(fields) > 9 and "SpaceAfter=No" in fields[9]:
token.whitespace_after = 0
# add morphological tags
for morph in str(fields[5]).split("|"):
if "=" not in morph:
continue
token.add_label(morph.split("=")[0].lower(), morph.split("=")[1])
if len(fields) > 10 and str(fields[10]) == "Y":
token.add_label("frame", str(fields[11]))
token_idx += 1
# derive whitespace logic for multiwords
if token_idx <= current_multiword_last_token:
current_multiword_sequence += token.text
# print(token)
# print(current_multiword_last_token)
# print(current_multiword_first_token)
# if multi-word equals component tokens, there should be no whitespace
if token_idx == current_multiword_last_token and current_multiword_sequence == current_multiword_text:
# go through all tokens in subword and set whitespace_after information
for i in range(current_multiword_last_token - current_multiword_first_token):
# print(i)
tokens[-(i + 1)].whitespace_after = 0
tokens.append(token)
line = file.readline()
return Sentence(tokens)
class UD_ENGLISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_English-EWT/master"
cached_path(f"{web_path}/en_ewt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/en_ewt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/en_ewt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_GALICIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Galician-TreeGal/master"
cached_path(f"{web_path}/gl_treegal-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/gl_treegal-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ANCIENT_GREEK(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Ancient_Greek-PROIEL/master"
cached_path(f"{web_path}/grc_proiel-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/grc_proiel-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/grc_proiel-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_KAZAKH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Kazakh-KTB/master"
cached_path(f"{web_path}/kk_ktb-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/kk_ktb-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_OLD_CHURCH_SLAVONIC(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Old_Church_Slavonic-PROIEL/master"
cached_path(f"{web_path}/cu_proiel-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/cu_proiel-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/cu_proiel-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ARMENIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Armenian-ArmTDP/master/"
cached_path(f"{web_path}/hy_armtdp-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/hy_armtdp-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/hy_armtdp-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ESTONIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Estonian-EDT/master"
cached_path(f"{web_path}/et_edt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/et_edt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/et_edt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_GERMAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_German-GSD/master"
cached_path(f"{ud_path}/de_gsd-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/de_gsd-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/de_gsd-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_GERMAN_HDT(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = False,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_German-HDT/dev"
cached_path(f"{ud_path}/de_hdt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/de_hdt-ud-test.conllu", Path("datasets") / dataset_name)
train_filenames = [
"de_hdt-ud-train-a-1.conllu",
"de_hdt-ud-train-a-2.conllu",
"de_hdt-ud-train-b-1.conllu",
"de_hdt-ud-train-b-2.conllu",
]
for train_file in train_filenames:
cached_path(f"{ud_path}/{train_file}", Path("datasets") / dataset_name / "original")
data_path = flair.cache_root / "datasets" / dataset_name
new_train_file: Path = data_path / "de_hdt-ud-train-all.conllu"
if not new_train_file.is_file():
with open(new_train_file, "w") as f_out:
for train_filename in train_filenames:
with open(data_path / "original" / train_filename) as f_in:
f_out.write(f_in.read())
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_DUTCH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Dutch-Alpino/master"
cached_path(f"{ud_path}/nl_alpino-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/nl_alpino-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/nl_alpino-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_FAROESE(UniversalDependenciesCorpus):
"""This treebank includes the Faroese treebank dataset.
The data is obtained from the following link:
https://github.com/UniversalDependencies/UD_Faroese-FarPaHC/tree/master
Faronese is a small Western Scandinavian language with 60.000-100.000, related to Icelandic and Old Norse.
"""
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Faroese-FarPaHC/master"
cached_path(f"{web_path}/fo_farpahc-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/fo_farpahc-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/fo_farpahc-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_FRENCH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_French-GSD/master"
cached_path(f"{ud_path}/fr_gsd-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/fr_gsd-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/fr_gsd-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ITALIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Italian-ISDT/master"
cached_path(f"{ud_path}/it_isdt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/it_isdt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/it_isdt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_LATIN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
web_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Latin-LLCT/master/"
cached_path(f"{web_path}/la_llct-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/la_llct-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{web_path}/la_llct-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_SPANISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Spanish-GSD/master"
cached_path(f"{ud_path}/es_gsd-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/es_gsd-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/es_gsd-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_PORTUGUESE(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Portuguese-Bosque/master"
cached_path(f"{ud_path}/pt_bosque-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/pt_bosque-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/pt_bosque-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ROMANIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Romanian-RRT/master"
cached_path(f"{ud_path}/ro_rrt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ro_rrt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ro_rrt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_CATALAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Catalan-AnCora/master"
cached_path(f"{ud_path}/ca_ancora-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ca_ancora-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ca_ancora-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_POLISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Polish-LFG/master"
cached_path(f"{ud_path}/pl_lfg-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/pl_lfg-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/pl_lfg-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_CZECH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = False,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Czech-PDT/master"
cached_path(f"{ud_path}/cs_pdt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/cs_pdt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(
f"{ud_path}/cs_pdt-ud-train-c.conllu",
Path("datasets") / dataset_name / "original",
)
cached_path(
f"{ud_path}/cs_pdt-ud-train-l.conllu",
Path("datasets") / dataset_name / "original",
)
cached_path(
f"{ud_path}/cs_pdt-ud-train-m.conllu",
Path("datasets") / dataset_name / "original",
)
cached_path(
f"{ud_path}/cs_pdt-ud-train-v.conllu",
Path("datasets") / dataset_name / "original",
)
data_path = flair.cache_root / "datasets" / dataset_name
train_filenames = [
"cs_pdt-ud-train-c.conllu",
"cs_pdt-ud-train-l.conllu",
"cs_pdt-ud-train-m.conllu",
"cs_pdt-ud-train-v.conllu",
]
new_train_file: Path = data_path / "cs_pdt-ud-train-all.conllu"
if not new_train_file.is_file():
with open(new_train_file, "w") as f_out:
for train_filename in train_filenames:
with open(data_path / "original" / train_filename) as f_in:
f_out.write(f_in.read())
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_SLOVAK(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Slovak-SNK/master"
cached_path(f"{ud_path}/sk_snk-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sk_snk-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sk_snk-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_SWEDISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Swedish-Talbanken/master"
cached_path(f"{ud_path}/sv_talbanken-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sv_talbanken-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sv_talbanken-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_DANISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Danish-DDT/master"
cached_path(f"{ud_path}/da_ddt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/da_ddt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/da_ddt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_NORWEGIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Norwegian-Bokmaal/master"
cached_path(f"{ud_path}/no_bokmaal-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/no_bokmaal-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/no_bokmaal-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_FINNISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Finnish-TDT/master"
cached_path(f"{ud_path}/fi_tdt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/fi_tdt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/fi_tdt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_SLOVENIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Slovenian-SSJ/master"
cached_path(f"{ud_path}/sl_ssj-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sl_ssj-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sl_ssj-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_CROATIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Croatian-SET/master"
cached_path(f"{ud_path}/hr_set-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/hr_set-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/hr_set-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_SERBIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Serbian-SET/master"
cached_path(f"{ud_path}/sr_set-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sr_set-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/sr_set-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_BULGARIAN(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Bulgarian-BTB/master"
cached_path(f"{ud_path}/bg_btb-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/bg_btb-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/bg_btb-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_ARABIC(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Arabic-PADT/master"
cached_path(f"{ud_path}/ar_padt-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ar_padt-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/ar_padt-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_HEBREW(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Hebrew-HTB/master"
cached_path(f"{ud_path}/he_htb-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/he_htb-ud-test.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/he_htb-ud-train.conllu", Path("datasets") / dataset_name)
super().__init__(data_folder, in_memory=in_memory, split_multiwords=split_multiwords)
class UD_TURKISH(UniversalDependenciesCorpus):
def __init__(
self,
base_path: Optional[Union[str, Path]] = None,
in_memory: bool = True,
split_multiwords: bool = True,
) -> None:
base_path = Path(flair.cache_root) / "datasets" if not base_path else Path(base_path)
# this dataset name
dataset_name = self.__class__.__name__.lower()
# default dataset folder is the cache root
if not base_path:
base_path = flair.cache_root / "datasets"
data_folder = base_path / dataset_name
# download data if necessary
ud_path = "https://raw.githubusercontent.com/UniversalDependencies/UD_Turkish-IMST/master"
cached_path(f"{ud_path}/tr_imst-ud-dev.conllu", Path("datasets") / dataset_name)
cached_path(f"{ud_path}/tr_imst-ud-test.conllu", Path("datasets") / dataset_name)