voorhs commited on
Commit
3175e80
·
verified ·
1 Parent(s): 0e61b58

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +30 -23
README.md CHANGED
@@ -84,54 +84,61 @@ names_to_remove = [
84
  "department establishment",
85
  ]
86
 
87
- def extract_intents_data(events_dataset: HFDataset) -> tuple[list[Intent], dict[str, int]]:
88
  """Extract intent names and assign ids to them."""
89
  intent_names = sorted({name for intents in events_dataset["train"]["all_labels"] for name in intents})
90
  for n in names_to_remove:
91
  intent_names.remove(n)
92
- name_to_id = {name: i for i, name in enumerate(intent_names)}
93
- intents_data = [Intent(id=i,name=name) for i, name in enumerate(intent_names)]
94
- return intents_data, name_to_id
95
 
96
 
97
- def converting_mapping(example: dict, name_to_id: dict[str, int]) -> dict[str, str | list[int]]:
98
- """Extract utterance and label and drop the rest."""
99
- return {
100
  "utterance": example["content"],
101
  "label": [
102
- name_to_id[intent_name] for intent_name in example["all_labels"] if intent_name not in names_to_remove
103
- ],
104
  }
 
 
 
105
 
106
 
107
- def convert_events(events_split: HFDataset, name_to_id: dict[str, int]) -> list[Sample]:
108
  """Convert one split into desired format."""
109
  events_split = events_split.map(
110
  converting_mapping, remove_columns=events_split.features.keys(),
111
- fn_kwargs={"name_to_id": name_to_id}
112
  )
113
 
114
- in_domain_samples = []
115
- oos_samples = [] # actually this dataset doesn't contain oos_samples so this will stay empty
116
  for sample in events_split.to_list():
117
  if sample["utterance"] is None:
118
  continue
119
- if len(sample["label"]) == 0:
120
- sample.pop("label")
121
- oos_samples.append(sample)
122
- else:
123
- in_domain_samples.append(sample)
124
 
125
- return [Sample(**sample) for sample in in_domain_samples + oos_samples]
 
 
 
 
 
 
 
 
 
 
126
 
127
  if __name__ == "__main__":
128
- # FYI: https://github.com/huggingface/datasets/issues/7248
 
129
  events_dataset = load_dataset("knowledgator/events_classification_biotech", trust_remote_code=True)
130
 
131
- intents_data, name_to_id = extract_intents_data(events_dataset)
132
 
133
- train_samples = convert_events(events_dataset["train"], name_to_id)
134
- test_samples = convert_events(events_dataset["test"], name_to_id)
135
 
136
  events_converted = Dataset.from_dict(
137
  {"train": train_samples, "test": test_samples, "intents": intents_data}
 
84
  "department establishment",
85
  ]
86
 
87
+ def extract_intents_data(events_dataset: HFDataset) -> list[Intent]:
88
  """Extract intent names and assign ids to them."""
89
  intent_names = sorted({name for intents in events_dataset["train"]["all_labels"] for name in intents})
90
  for n in names_to_remove:
91
  intent_names.remove(n)
92
+ return [Intent(id=i,name=name) for i, name in enumerate(intent_names)]
 
 
93
 
94
 
95
+ def converting_mapping(example: dict, intents_data: list[Intent]) -> dict[str, str | list[int] | None]:
96
+ """Extract utterance and OHE label and drop the rest."""
97
+ res = {
98
  "utterance": example["content"],
99
  "label": [
100
+ int(intent.name in example["all_labels"]) for intent in intents_data
101
+ ]
102
  }
103
+ if sum(res["label"]) == 0:
104
+ res["label"] = None
105
+ return res
106
 
107
 
108
+ def convert_events(events_split: HFDataset, intents_data: dict[str, int]) -> list[Sample]:
109
  """Convert one split into desired format."""
110
  events_split = events_split.map(
111
  converting_mapping, remove_columns=events_split.features.keys(),
112
+ fn_kwargs={"intents_data": intents_data}
113
  )
114
 
115
+ samples = []
 
116
  for sample in events_split.to_list():
117
  if sample["utterance"] is None:
118
  continue
119
+ samples.append(sample)
 
 
 
 
120
 
121
+ mask = [sample["label"] is None for sample in samples]
122
+ n_oos_samples = sum(mask)
123
+ n_in_domain_samples = len(samples) - n_oos_samples
124
+
125
+ print(f"{n_oos_samples=}")
126
+ print(f"{n_in_domain_samples=}\n")
127
+
128
+ # actually there are too few oos samples to include them, so filter out
129
+ samples = list(filter(lambda sample: sample["label"] is not None, samples))
130
+
131
+ return [Sample(**sample) for sample in samples]
132
 
133
  if __name__ == "__main__":
134
+ # `load_dataset` might not work
135
+ # fix is here: https://github.com/huggingface/datasets/issues/7248
136
  events_dataset = load_dataset("knowledgator/events_classification_biotech", trust_remote_code=True)
137
 
138
+ intents_data = extract_intents_data(events_dataset)
139
 
140
+ train_samples = convert_events(events_dataset["train"], intents_data)
141
+ test_samples = convert_events(events_dataset["test"], intents_data)
142
 
143
  events_converted = Dataset.from_dict(
144
  {"train": train_samples, "test": test_samples, "intents": intents_data}