File size: 4,427 Bytes
aae5ab2
 
abc8eba
aae5ab2
 
 
 
 
 
 
 
 
05ac2cb
 
 
c29004b
 
 
 
 
abc8eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aae5ab2
 
 
 
 
05ac2cb
 
c29004b
 
abc8eba
 
 
 
aae5ab2
5b25b42
 
 
 
 
 
 
 
 
 
 
 
 
fdc5203
5b25b42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
dataset_info:
- config_name: default
  features:
  - name: utterance
    dtype: string
  - name: label
    dtype: int64
  splits:
  - name: train
    num_bytes: 857605
    num_examples: 15200
  - name: validation
    num_bytes: 160686
    num_examples: 3100
  - name: test
    num_bytes: 287654
    num_examples: 5500
  download_size: 542584
  dataset_size: 1305945
- config_name: intents
  features:
  - name: id
    dtype: int64
  - name: name
    dtype: string
  - name: tags
    sequence: 'null'
  - name: regexp_full_match
    sequence: 'null'
  - name: regexp_partial_match
    sequence: 'null'
  - name: description
    dtype: 'null'
  splits:
  - name: intents
    num_bytes: 5368
    num_examples: 150
  download_size: 5519
  dataset_size: 5368
configs:
- config_name: default
  data_files:
  - split: train
    path: data/train-*
  - split: validation
    path: data/validation-*
  - split: test
    path: data/test-*
- config_name: intents
  data_files:
  - split: intents
    path: intents/intents-*
---

# clinc150

This is a text classification dataset. It is intended for machine learning research and experimentation.

This dataset is obtained via formatting another publicly available data to be compatible with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html).

## Usage

It is intended to be used with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):

```python
from autointent import Dataset
banking77 = Dataset.from_hub("AutoIntent/clinc150")
```

## Source

This dataset is taken from `cmaldona/All-Generalization-OOD-CLINC150` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):

```python
# define util
"""Convert clincq50 dataset to autointent internal format and scheme."""

from datasets import Dataset as HFDataset
from datasets import load_dataset

from autointent import Dataset
from autointent.schemas import Intent, Sample


def extract_intents_data(
    clinc150_split: HFDataset, oos_intent_name: str = "ood"
) -> tuple[list[Intent], dict[str, int]]:
    """Extract intent names and assign ids to them."""
    intent_names = sorted(clinc150_split.unique("labels"))
    oos_intent_id = intent_names.index(oos_intent_name)
    intent_names.pop(oos_intent_id)

    n_classes = len(intent_names)
    assert n_classes == 150  # noqa: PLR2004, S101

    name_to_id = dict(zip(intent_names, range(n_classes), strict=False))
    intents_data = [Intent(id=i, name=name) for name, i in name_to_id.items()]
    return intents_data, name_to_id


def convert_clinc150(
    clinc150_split: HFDataset,
    name_to_id: dict[str, int],
    shots_per_intent: int | None = None,
    oos_intent_name: str = "ood",
) -> list[Sample]:
    """Convert one split into desired format."""
    oos_samples = []
    classwise_samples = [[] for _ in range(len(name_to_id))]
    n_unrecognized_labels = 0

    for batch in clinc150_split.iter(batch_size=16, drop_last_batch=False):
        for txt, name in zip(batch["data"], batch["labels"], strict=False):
            if name == oos_intent_name:
                oos_samples.append(Sample(utterance=txt))
                continue
            intent_id = name_to_id.get(name, None)
            if intent_id is None:
                n_unrecognized_labels += 1
                continue
            target_list = classwise_samples[intent_id]
            if shots_per_intent is not None and len(target_list) >= shots_per_intent:
                continue
            target_list.append(Sample(utterance=txt, label=intent_id))

    in_domain_samples = [sample for samples_from_single_class in classwise_samples for sample in samples_from_single_class]
    
    print(f"{len(in_domain_samples)=}")
    print(f"{len(oos_samples)=}")
    print(f"{n_unrecognized_labels=}\n")
    
    return in_domain_samples + oos_samples


if __name__ == "__main__":
    clinc150 = load_dataset("cmaldona/All-Generalization-OOD-CLINC150")

    intents_data, name_to_id = extract_intents_data(clinc150["train"])

    train_samples = convert_clinc150(clinc150["train"], name_to_id)
    validation_samples = convert_clinc150(clinc150["validation"], name_to_id)
    test_samples = convert_clinc150(clinc150["test"], name_to_id)

    clinc150_converted = Dataset.from_dict(
        {"train": train_samples, "validation": validation_samples, "test": test_samples, "intents": intents_data}
    )
```