Datasets:
Update README.md
Browse files
README.md
CHANGED
|
@@ -78,22 +78,64 @@ eurlex = Dataset.from_hub("AutoIntent/eurlex")
|
|
| 78 |
This dataset is taken from `coastalcph/multi_eurlex` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
| 79 |
|
| 80 |
```python
|
| 81 |
-
|
| 82 |
from autointent import Dataset
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
```
|
|
|
|
| 78 |
This dataset is taken from `coastalcph/multi_eurlex` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
| 79 |
|
| 80 |
```python
|
| 81 |
+
import datasets
|
| 82 |
from autointent import Dataset
|
| 83 |
|
| 84 |
+
|
| 85 |
+
def get_number_of_classes(ds: datasets.Dataset) -> int:
|
| 86 |
+
return len(set(i for example in ds for labels in example for i in labels))
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def parse(ds: datasets.Dataset, n_classes: int) -> datasets.Dataset:
|
| 90 |
+
def transform(example: dict):
|
| 91 |
+
return {"utterance": example["text"], "label": [int(i in example["labels"]) for i in range(n_classes)]}
|
| 92 |
+
return ds.map(transform, remove_columns=ds.features.keys())
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def get_low_resource_classes_mask(ds: datasets.Dataset, n_classes: int, fraction_thresh: float = 0.01) -> list[bool]:
|
| 96 |
+
res = [0] * n_classes
|
| 97 |
+
for sample in ds:
|
| 98 |
+
for i, indicator in enumerate(sample["label"]):
|
| 99 |
+
res[i] += indicator
|
| 100 |
+
for i in range(n_classes):
|
| 101 |
+
res[i] /= len(ds)
|
| 102 |
+
return [(frac < fraction_thresh) for frac in res]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def remove_low_resource_classes(ds: datasets.Dataset, mask: list[bool]) -> list[dict]:
|
| 106 |
+
res = []
|
| 107 |
+
for sample in ds:
|
| 108 |
+
if sum(sample["label"]) == 1 and mask[sample["label"].index(1)]:
|
| 109 |
+
continue
|
| 110 |
+
sample["label"] = [
|
| 111 |
+
indicator for indicator, low_resource in
|
| 112 |
+
zip(sample["label"], mask, strict=True) if not low_resource
|
| 113 |
+
]
|
| 114 |
+
res.append(sample)
|
| 115 |
+
return res
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def remove_oos(ds: list[dict]):
|
| 119 |
+
return [sample for sample in ds if sum(sample["label"]) != 0]
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
eurlex = datasets.load_dataset("coastalcph/multi_eurlex", "en", trust_remote_code=True)
|
| 124 |
+
|
| 125 |
+
n_classes = get_number_of_classes(eurlex["train"])
|
| 126 |
+
|
| 127 |
+
train = parse(eurlex["train"], n_classes)
|
| 128 |
+
test = parse(eurlex["test"], n_classes)
|
| 129 |
+
validation = parse(eurlex["validation"], n_classes)
|
| 130 |
+
|
| 131 |
+
mask = get_low_resource_classes_mask(train, n_classes)
|
| 132 |
+
train = remove_oos(remove_low_resource_classes(train, mask))
|
| 133 |
+
test = remove_oos(remove_low_resource_classes(test, mask))
|
| 134 |
+
validation = remove_oos(remove_low_resource_classes(validation, mask))
|
| 135 |
+
|
| 136 |
+
eurlex_converted = Dataset.from_dict({
|
| 137 |
+
"train": train,
|
| 138 |
+
"test": test,
|
| 139 |
+
"validation": validation,
|
| 140 |
+
})
|
| 141 |
```
|