sobir-hf commited on
Commit
74f1447
·
1 Parent(s): 453bed4

Upload tajik-text-segmentation.py

Browse files
Files changed (1) hide show
  1. tajik-text-segmentation.py +92 -0
tajik-text-segmentation.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Custom Annotations Dataset."""
2
+
3
+ import os
4
+ from annotations_parser import load_yedda_annotations
5
+ import datasets
6
+
7
+ logger = datasets.logging.get_logger(__name__)
8
+
9
+
10
+
11
+ _CITATION = """
12
+ @misc{tajik-text-segmentation-2023,
13
+ title={Tajik sentence-wise text segmentation dataset},
14
+ author={Sobir Bobiev},
15
+ year={2023},
16
+ howpublished={\\url{https://huggingface.co/datasets/sobir-hf/tajik-text-segmentation}},
17
+ }
18
+ """
19
+
20
+ _DESCRIPTION = """
21
+ Tajik sentence-wise text segmentation dataset consisting of annotated text files.
22
+ """
23
+
24
+ class CustomAnnotationsConfig(datasets.BuilderConfig):
25
+ """BuilderConfig for Custom Annotations."""
26
+
27
+ def __init__(self, **kwargs):
28
+ """BuilderConfig for Custom Annotations.
29
+ Args:
30
+ **kwargs: keyword arguments forwarded to super.
31
+ """
32
+ super(CustomAnnotationsConfig, self).__init__(**kwargs)
33
+
34
+
35
+ class CustomAnnotations(datasets.GeneratorBasedBuilder):
36
+ """Custom Annotations: Dataset with annotated text files."""
37
+
38
+ BUILDER_CONFIGS = [
39
+ CustomAnnotationsConfig(
40
+ name="plain_text",
41
+ version=datasets.Version("1.0.0", ""),
42
+ description="Plain text",
43
+ ),
44
+ ]
45
+
46
+ def _info(self):
47
+ return datasets.DatasetInfo(
48
+ description=_DESCRIPTION,
49
+ features=datasets.Features(
50
+ {
51
+ "file": datasets.Value("string"),
52
+ "text": datasets.Value("string"),
53
+ "annotated_text": datasets.Value("string"),
54
+ "number_of_labels": datasets.Value("int32"),
55
+ }
56
+ ),
57
+ supervised_keys=None,
58
+ homepage="your dataset homepage",
59
+ citation=_CITATION,
60
+ )
61
+
62
+ def _split_generators(self, dl_manager):
63
+ directory_path = os.path.abspath('annotations')
64
+ return [
65
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"directory_path": directory_path}),
66
+ ]
67
+
68
+ def _generate_examples(self, directory_path):
69
+ """This function returns the examples in the raw (text) form."""
70
+ logger.info("generating examples from = %s", directory_path)
71
+
72
+ annotations = load_yedda_annotations(directory_path)
73
+
74
+ for idx, file_annotation in enumerate(annotations):
75
+ file = file_annotation['file']
76
+ text = file_annotation['text']
77
+ annotated_text = file_annotation['annotated_text']
78
+ number_of_labels = len(file_annotation['labels'])
79
+
80
+ yield idx, {
81
+ "file": file,
82
+ "text": text,
83
+ "annotated_text": annotated_text,
84
+ "number_of_labels": number_of_labels,
85
+ }
86
+
87
+ if __name__ == '__main__':
88
+ # You can test the data generation by running this script.
89
+ # It will create a dataset in a subdirectory `./datasets/`.
90
+ from datasets import load_dataset
91
+ dataset = load_dataset('./tajik-text-segmentation.py')
92
+ print(dataset)