Update README.md
Browse files
README.md
CHANGED
@@ -73,40 +73,56 @@ reuters = Dataset.from_hub("AutoIntent/reuters")
|
|
73 |
This dataset is taken from `ucirvine/reuters21578` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
74 |
|
75 |
```python
|
76 |
-
from collections import defaultdict
|
77 |
from autointent import Dataset
|
78 |
import datasets
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
intent_names
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
return [sample for sample in ds if sum(sample["label"]) != 0]
|
105 |
|
106 |
-
train = transform(reuters["train"])
|
107 |
-
test = transform(reuters["test"])
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
```
|
|
|
73 |
This dataset is taken from `ucirvine/reuters21578` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
74 |
|
75 |
```python
|
|
|
76 |
from autointent import Dataset
|
77 |
import datasets
|
78 |
|
79 |
+
|
80 |
+
|
81 |
+
def get_intents_info(ds: datasets.DatasetDict) -> list[str]:
|
82 |
+
return sorted(set(name for intents in ds["train"]["topics"] for name in intents))
|
83 |
+
|
84 |
+
def parse(ds: datasets.Dataset, intent_names: list[str]) -> list[dict]:
|
85 |
+
return [{
|
86 |
+
"utterance": example["text"],
|
87 |
+
"label": [int(name in example["topics"]) for name in intent_names]
|
88 |
+
} for example in ds]
|
89 |
+
|
90 |
+
|
91 |
+
def get_low_resource_classes_mask(ds: list[dict], intent_names: list[str], fraction_thresh: float = 0.01) -> list[bool]:
|
92 |
+
res = [0] * len(intent_names)
|
93 |
+
for sample in ds:
|
94 |
+
for i, indicator in enumerate(sample["label"]):
|
95 |
+
res[i] += indicator
|
96 |
+
for i in range(len(intent_names)):
|
97 |
+
res[i] /= len(ds)
|
98 |
+
return [(frac < fraction_thresh) for frac in res]
|
99 |
+
|
100 |
+
def remove_low_resource_classes(ds: datasets.Dataset, mask: list[bool]) -> list[dict]:
|
101 |
+
res = []
|
102 |
+
for sample in ds:
|
103 |
+
if sum(sample["label"]) == 1 and mask[sample["label"].index(1)]:
|
104 |
+
continue
|
105 |
+
sample["label"] = [
|
106 |
+
indicator for indicator, low_resource in
|
107 |
+
zip(sample["label"], mask, strict=True) if not low_resource
|
108 |
+
]
|
109 |
+
res.append(sample)
|
110 |
+
return res
|
111 |
+
|
112 |
+
def remove_oos(ds: list[dict]):
|
113 |
return [sample for sample in ds if sum(sample["label"]) != 0]
|
114 |
|
|
|
|
|
115 |
|
116 |
+
if __name__ == "__main__":
|
117 |
+
reuters = datasets.load_dataset("ucirvine/reuters21578", "ModHayes", trust_remote_code=True)
|
118 |
+
intent_names = get_intents_info(reuters)
|
119 |
+
train_parsed = parse(reuters["train"], intent_names)
|
120 |
+
test_parsed = parse(reuters["test"], intent_names)
|
121 |
+
mask = get_low_resource_classes_mask(train_parsed, intent_names)
|
122 |
+
intent_names = [name for i, name in enumerate(intent_names) if not mask[i]]
|
123 |
+
train_filtered = remove_oos(remove_low_resource_classes(train_parsed, mask))
|
124 |
+
test_filtered = remove_oos(remove_low_resource_classes(test_parsed, mask))
|
125 |
+
|
126 |
+
intents = [{"id": i, "name": name} for i, name in enumerate(intent_names)]
|
127 |
+
reuters_converted = Dataset.from_dict({"intents": intents, "train": train_filtered, "test": test_filtered})
|
128 |
```
|