Datasets:

ArXiv:
Elron commited on
Commit
a87f691
1 Parent(s): 78c575e

Upload processors.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. processors.py +37 -0
processors.py CHANGED
@@ -1,4 +1,6 @@
 
1
  import re
 
2
 
3
  from .operator import BaseFieldOperator
4
 
@@ -14,10 +16,45 @@ class RegexParser(BaseFieldOperator):
14
  """
15
 
16
  regex: str
 
17
 
18
  def process(self, text):
 
 
19
  matches = re.findall(self.regex, text)
20
  return matches
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # add_to_catalog(ToString('prediction'), 'processors', 'to_string')
 
1
+ import json
2
  import re
3
+ from typing import Any
4
 
5
  from .operator import BaseFieldOperator
6
 
 
16
  """
17
 
18
  regex: str
19
+ termination_regex: str = None
20
 
21
  def process(self, text):
22
+ if self.termination_regex is not None and re.fullmatch(self.termination_regex, text):
23
+ return []
24
  matches = re.findall(self.regex, text)
25
  return matches
26
 
27
 
28
+ class LoadJson(BaseFieldOperator):
29
+ def process(self, text):
30
+ try:
31
+ return json.loads(text)
32
+ except json.JSONDecodeError:
33
+ return []
34
+
35
+
36
+ class ListToEmptyEntitiesTuples(BaseFieldOperator):
37
+ def process(self, lst):
38
+ try:
39
+ return [(str(item), "") for item in lst]
40
+ except json.JSONDecodeError:
41
+ return []
42
+
43
+
44
+ class DictOfListsToPairs(BaseFieldOperator):
45
+ position_key_before_value: bool = True
46
+
47
+ def process(self, obj):
48
+ try:
49
+ result = []
50
+ for key, values in obj.items():
51
+ for value in values:
52
+ assert isinstance(value, str)
53
+ pair = (key, value) if self.position_key_before_value else (value, key)
54
+ result.append(pair)
55
+ return result
56
+ except:
57
+ return []
58
+
59
+
60
  # add_to_catalog(ToString('prediction'), 'processors', 'to_string')