Datasets:
Update README.md
Browse files
README.md
CHANGED
@@ -73,43 +73,66 @@ dstc3 = Dataset.from_hub("AutoIntent/dstc3")
|
|
73 |
This dataset is taken from `marcel-gohsen/dstc3` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
74 |
|
75 |
```python
|
76 |
-
|
77 |
from autointent import Dataset
|
78 |
from autointent.context.data_handler import split_dataset
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
)
|
88 |
-
intent_names
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
)
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
for
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
```
|
|
|
73 |
This dataset is taken from `marcel-gohsen/dstc3` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
74 |
|
75 |
```python
|
76 |
+
import datasets
|
77 |
from autointent import Dataset
|
78 |
from autointent.context.data_handler import split_dataset
|
79 |
|
80 |
+
|
81 |
+
def extract_intent_info(ds: datasets.Dataset) -> list[str]:
|
82 |
+
ds = ds.filter(lambda example: example["transcript"] != "")
|
83 |
+
intent_names = sorted(
|
84 |
+
set(name for intents in ds["intent"] for name in intents)
|
85 |
+
)
|
86 |
+
intent_names.remove("reqmore")
|
87 |
+
ds.filter(lambda example: "reqmore" in example["intent"])
|
88 |
+
return intent_names
|
89 |
+
|
90 |
+
def parse(ds: datasets.Dataset, intent_names: list[str]):
|
91 |
+
def transform(example: dict):
|
92 |
+
return {
|
93 |
+
"utterance": example["transcript"],
|
94 |
+
"label": [int(name in example["intent"]) for name in intent_names],
|
95 |
+
}
|
96 |
+
return ds.map(
|
97 |
+
transform, remove_columns=ds.features.keys()
|
98 |
+
)
|
99 |
+
|
100 |
+
def calc_fractions(ds: datasets.Dataset, intent_names: list[str]) -> list[float]:
|
101 |
+
res = [0] * len(intent_names)
|
102 |
+
for sample in ds:
|
103 |
+
for i, indicator in enumerate(sample["label"]):
|
104 |
+
res[i] += indicator
|
105 |
+
for i in range(len(intent_names)):
|
106 |
+
res[i] /= len(ds)
|
107 |
+
return res
|
108 |
+
|
109 |
+
def remove_low_resource_classes(ds: datasets.Dataset, intent_names: list[str], fraction_thresh: float = 0.01) -> tuple[list[dict], list[str]]:
|
110 |
+
remove_or_not = [(frac < fraction_thresh) for frac in calc_fractions(ds, intent_names)]
|
111 |
+
intent_names = [name for i, name in enumerate(intent_names) if not remove_or_not[i]]
|
112 |
+
res = []
|
113 |
+
for sample in ds:
|
114 |
+
if sum(sample["label"]) == 1 and remove_or_not[sample["label"].index(1)]:
|
115 |
+
continue
|
116 |
+
sample["label"] = [
|
117 |
+
indicator for indicator, low_resource in
|
118 |
+
zip(sample["label"], remove_or_not, strict=True) if not low_resource
|
119 |
+
]
|
120 |
+
res.append(sample)
|
121 |
+
return res, intent_names
|
122 |
+
|
123 |
+
def remove_oos(ds: datasets.Dataset):
|
124 |
+
return ds.filter(lambda sample: sum(sample["label"]) != 0)
|
125 |
+
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
dstc3 = datasets.load_dataset("marcel-gohsen/dstc3")
|
129 |
+
|
130 |
+
intent_names = extract_intent_info(dstc3["test"])
|
131 |
+
parsed = parse(dstc3["test"], intent_names)
|
132 |
+
filtered, intent_names = remove_low_resource_classes(remove_oos(parsed), intent_names)
|
133 |
+
intents = [{"id": i, "name": name} for i, name in enumerate(intent_names)]
|
134 |
+
dstc_final = Dataset.from_dict({"intents": intents, "train": filtered})
|
135 |
+
dstc_final["train"], dstc_final["test"] = split_dataset(
|
136 |
+
dstc_final, split="train", test_size=0.2, random_seed=42
|
137 |
+
)
|
138 |
```
|