Download normalizers.py from unitxt/data: direct link, hf CLI and curl.
- Browser
- Download file 987 Bytes
-
https://huggingface.co/datasets/unitxt/data/resolve/85300cd814f4f9b94c36ef30b4c9cc1ffc8bb4eb/normalizers.py
- Command line
-
hf download hf://datasets/unitxt/data@85300cd814f4f9b94c36ef30b4c9cc1ffc8bb4eb/normalizers.py
-
curl -L -o normalizers.py https://huggingface.co/datasets/unitxt/data/resolve/85300cd814f4f9b94c36ef30b4c9cc1ffc8bb4eb/normalizers.py
987 Bytes
| from typing import Any, Dict, List, Optional | |
| from .operator import InstanceOperator | |
| class NormalizeListFields(InstanceOperator): | |
| fields: List[str] | |
| key_prefix: str = "" | |
| empty_value: str = "" | |
| separator: str = ", " | |
| def process( | |
| self, instance: Dict[str, Any], stream_name: Optional[str] = None | |
| ) -> Dict[str, Any]: | |
| for field in self.fields: | |
| assert field in instance, f"Field {field} not found in instance {instance}" | |
| assert isinstance( | |
| instance[field], list | |
| ), f"Field {field} should be a list, got {type(instance[field])}" | |
| target_key = self.key_prefix + field | |
| if len(instance[field]) == 0: | |
| instance[target_key] = self.empty_value | |
| elif len(instance[field]) == 1: | |
| instance[target_key] = instance[field][0] | |
| else: | |
| instance[target_key] = self.separator.join(instance[field]) | |
| return instance | |