Elron commited on
Commit
30ec791
1 Parent(s): 71c3f75

Upload normalizers.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. normalizers.py +26 -0
normalizers.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .operator import StreamInstanceOperator
2
+
3
+ from typing import List, Dict, Any
4
+
5
+
6
+ class NormalizeListFields(StreamInstanceOperator):
7
+ fields: List[str]
8
+ key_prefix: str = ""
9
+ empty_value: str = ""
10
+ separator: str = ", "
11
+
12
+ def process(self, instance: Dict[str, Any], stream_name: str = None) -> Dict[str, Any]:
13
+ for field in self.fields:
14
+ assert field in instance, f"Field {field} not found in instance {instance}"
15
+ assert isinstance(instance[field], list), f"Field {field} should be a list, got {type(instance[field])}"
16
+
17
+ target_key = self.key_prefix + field
18
+
19
+ if len(instance[field]) == 0:
20
+ instance[target_key] = self.empty_value
21
+ elif len(instance[field]) == 1:
22
+ instance[target_key] = instance[field][0]
23
+ else:
24
+ instance[target_key] = self.separator.join(instance[field])
25
+
26
+ return instance