voorhs commited on
Commit
3eca468
·
verified ·
1 Parent(s): 8d467db

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +59 -36
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
- from datasets import load_dataset
77
  from autointent import Dataset
78
  from autointent.context.data_handler import split_dataset
79
 
80
- # load original data
81
- dstc3 = load_dataset("marcel-gohsen/dstc3")
82
-
83
- # extract intent names
84
- dstc3["test"] = dstc3["test"].filter(lambda example: example["transcript"] != "")
85
- intent_names = sorted(
86
- set(name for intents in dstc3["test"]["intent"] for name in intents)
87
- )
88
- intent_names.remove("reqmore")
89
- dstc3["test"].filter(lambda example: "reqmore" in example["intent"])
90
- name_to_id = {name: i for i, name in enumerate(intent_names)}
91
-
92
- # parse complicated dstc format
93
- def transform(example: dict):
94
- return {
95
- "utterance": example["transcript"],
96
- "label": [int(name in example["intent"]) for name in intent_names],
97
- }
98
- dstc_converted = dstc3["test"].map(
99
- transform, remove_columns=dstc3["test"].features.keys()
100
- )
101
-
102
- # format to autointent.Dataset
103
- intents = [{"id": i, "name": name} for i, name in enumerate(intent_names)]
104
- utterances = []
105
- for rec in dstc_converted.to_list():
106
- if sum(rec["label"]) == 0:
107
- rec.pop("label")
108
- else:
109
- utterances.append(rec)
110
-
111
- dstc_converted = Dataset.from_dict({"intents": intents, "train": utterances})
112
- dstc_converted["train"], dstc_converted["test"] = split_dataset(
113
- dstc_converted, split="train", test_size=0.2, random_seed=42
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
  ```