Upload processors.py with huggingface_hub
Browse files- processors.py +53 -52
processors.py
CHANGED
@@ -1,31 +1,32 @@
|
|
1 |
import json
|
2 |
import re
|
|
|
3 |
|
4 |
-
from .
|
5 |
|
6 |
|
7 |
-
class ToString(
|
8 |
-
def
|
9 |
-
return str(
|
10 |
|
11 |
|
12 |
-
class ToStringStripped(
|
13 |
-
def
|
14 |
-
return str(
|
15 |
|
16 |
|
17 |
-
class ToListByComma(
|
18 |
-
def
|
19 |
-
return [x.strip() for x in
|
20 |
|
21 |
|
22 |
-
class RegexParser(
|
23 |
"""A processor that uses regex in order to parse a string."""
|
24 |
|
25 |
regex: str
|
26 |
termination_regex: str = None
|
27 |
|
28 |
-
def
|
29 |
if self.termination_regex is not None and re.fullmatch(
|
30 |
self.termination_regex, text
|
31 |
):
|
@@ -33,26 +34,26 @@ class RegexParser(BaseFieldOperator):
|
|
33 |
return re.findall(self.regex, text)
|
34 |
|
35 |
|
36 |
-
class LoadJson(
|
37 |
-
def
|
38 |
try:
|
39 |
return json.loads(text)
|
40 |
except json.JSONDecodeError:
|
41 |
return []
|
42 |
|
43 |
|
44 |
-
class ListToEmptyEntitiesTuples(
|
45 |
-
def
|
46 |
try:
|
47 |
return [(str(item), "") for item in lst]
|
48 |
except json.JSONDecodeError:
|
49 |
return []
|
50 |
|
51 |
|
52 |
-
class DictOfListsToPairs(
|
53 |
position_key_before_value: bool = True
|
54 |
|
55 |
-
def
|
56 |
try:
|
57 |
result = []
|
58 |
for key, values in obj.items():
|
@@ -67,17 +68,17 @@ class DictOfListsToPairs(BaseFieldOperator):
|
|
67 |
return []
|
68 |
|
69 |
|
70 |
-
class TakeFirstNonEmptyLine(
|
71 |
-
def
|
72 |
-
splitted = str(
|
73 |
if len(splitted) == 0:
|
74 |
return ""
|
75 |
return splitted[0].strip()
|
76 |
|
77 |
|
78 |
-
class ConvertToBoolean(
|
79 |
-
def
|
80 |
-
clean_instance = str(
|
81 |
if any(w in clean_instance for w in ["no", "not", "wrong", "false"]):
|
82 |
return "FALSE"
|
83 |
if any(w in clean_instance for w in ["yes", "right", "correct", "true"]):
|
@@ -85,9 +86,9 @@ class ConvertToBoolean(BaseFieldOperator):
|
|
85 |
return "OTHER"
|
86 |
|
87 |
|
88 |
-
class LowerCaseTillPunc(
|
89 |
-
def
|
90 |
-
non_empty_line =
|
91 |
match = re.search(r"[.,!?;]", non_empty_line)
|
92 |
if match:
|
93 |
# Extract text up to the first punctuation
|
@@ -95,56 +96,56 @@ class LowerCaseTillPunc(BaseFieldOperator):
|
|
95 |
return non_empty_line
|
96 |
|
97 |
|
98 |
-
class LowerCase(
|
99 |
-
def
|
100 |
-
return
|
101 |
|
102 |
|
103 |
-
class FirstCharacter(
|
104 |
-
def
|
105 |
-
match = re.search(r"\s*(\w)",
|
106 |
if match:
|
107 |
return match.groups(0)[0]
|
108 |
return ""
|
109 |
|
110 |
|
111 |
-
class TakeFirstWord(
|
112 |
-
def
|
113 |
-
match = re.search(r"[\w]+",
|
114 |
if match:
|
115 |
-
return
|
116 |
return ""
|
117 |
|
118 |
|
119 |
-
class YesNoToInt(
|
120 |
-
def
|
121 |
-
if
|
122 |
return "1"
|
123 |
return "0"
|
124 |
|
125 |
|
126 |
-
class ToYesOrNone(
|
127 |
-
def
|
128 |
-
if
|
129 |
return "yes"
|
130 |
return "none"
|
131 |
|
132 |
|
133 |
-
class StanceToProCon(
|
134 |
-
def
|
135 |
-
if
|
136 |
return "PRO"
|
137 |
-
if
|
138 |
return "CON"
|
139 |
return "none"
|
140 |
|
141 |
|
142 |
-
class StringOrNotString(
|
143 |
string: str
|
144 |
|
145 |
-
def
|
146 |
-
if "not " + self.string.lower() in
|
147 |
return "not " + self.string.lower()
|
148 |
-
if self.string.lower() in
|
149 |
return self.string.lower()
|
150 |
-
return
|
|
|
1 |
import json
|
2 |
import re
|
3 |
+
from typing import Any
|
4 |
|
5 |
+
from .operators import FieldOperator
|
6 |
|
7 |
|
8 |
+
class ToString(FieldOperator):
|
9 |
+
def process_value(self, text: Any) -> Any:
|
10 |
+
return str(text)
|
11 |
|
12 |
|
13 |
+
class ToStringStripped(FieldOperator):
|
14 |
+
def process_value(self, text: Any) -> Any:
|
15 |
+
return str(text).strip()
|
16 |
|
17 |
|
18 |
+
class ToListByComma(FieldOperator):
|
19 |
+
def process_value(self, text: Any) -> Any:
|
20 |
+
return [x.strip() for x in text.split(",")]
|
21 |
|
22 |
|
23 |
+
class RegexParser(FieldOperator):
|
24 |
"""A processor that uses regex in order to parse a string."""
|
25 |
|
26 |
regex: str
|
27 |
termination_regex: str = None
|
28 |
|
29 |
+
def process_value(self, text: Any) -> Any:
|
30 |
if self.termination_regex is not None and re.fullmatch(
|
31 |
self.termination_regex, text
|
32 |
):
|
|
|
34 |
return re.findall(self.regex, text)
|
35 |
|
36 |
|
37 |
+
class LoadJson(FieldOperator):
|
38 |
+
def process_value(self, text: Any) -> Any:
|
39 |
try:
|
40 |
return json.loads(text)
|
41 |
except json.JSONDecodeError:
|
42 |
return []
|
43 |
|
44 |
|
45 |
+
class ListToEmptyEntitiesTuples(FieldOperator):
|
46 |
+
def process_value(self, lst: Any) -> Any:
|
47 |
try:
|
48 |
return [(str(item), "") for item in lst]
|
49 |
except json.JSONDecodeError:
|
50 |
return []
|
51 |
|
52 |
|
53 |
+
class DictOfListsToPairs(FieldOperator):
|
54 |
position_key_before_value: bool = True
|
55 |
|
56 |
+
def process_value(self, obj: Any) -> Any:
|
57 |
try:
|
58 |
result = []
|
59 |
for key, values in obj.items():
|
|
|
68 |
return []
|
69 |
|
70 |
|
71 |
+
class TakeFirstNonEmptyLine(FieldOperator):
|
72 |
+
def process_value(self, text: Any) -> Any:
|
73 |
+
splitted = str(text).strip().split("\n")
|
74 |
if len(splitted) == 0:
|
75 |
return ""
|
76 |
return splitted[0].strip()
|
77 |
|
78 |
|
79 |
+
class ConvertToBoolean(FieldOperator):
|
80 |
+
def process_value(self, text: Any) -> Any:
|
81 |
+
clean_instance = str(text).strip().lower()
|
82 |
if any(w in clean_instance for w in ["no", "not", "wrong", "false"]):
|
83 |
return "FALSE"
|
84 |
if any(w in clean_instance for w in ["yes", "right", "correct", "true"]):
|
|
|
86 |
return "OTHER"
|
87 |
|
88 |
|
89 |
+
class LowerCaseTillPunc(FieldOperator):
|
90 |
+
def process_value(self, text: Any) -> Any:
|
91 |
+
non_empty_line = text.lower()
|
92 |
match = re.search(r"[.,!?;]", non_empty_line)
|
93 |
if match:
|
94 |
# Extract text up to the first punctuation
|
|
|
96 |
return non_empty_line
|
97 |
|
98 |
|
99 |
+
class LowerCase(FieldOperator):
|
100 |
+
def process_value(self, text: Any) -> Any:
|
101 |
+
return text.lower()
|
102 |
|
103 |
|
104 |
+
class FirstCharacter(FieldOperator):
|
105 |
+
def process_value(self, text: Any) -> Any:
|
106 |
+
match = re.search(r"\s*(\w)", text)
|
107 |
if match:
|
108 |
return match.groups(0)[0]
|
109 |
return ""
|
110 |
|
111 |
|
112 |
+
class TakeFirstWord(FieldOperator):
|
113 |
+
def process_value(self, text: Any) -> Any:
|
114 |
+
match = re.search(r"[\w]+", text)
|
115 |
if match:
|
116 |
+
return text[match.start() : match.end()]
|
117 |
return ""
|
118 |
|
119 |
|
120 |
+
class YesNoToInt(FieldOperator):
|
121 |
+
def process_value(self, text: Any) -> Any:
|
122 |
+
if text == "yes":
|
123 |
return "1"
|
124 |
return "0"
|
125 |
|
126 |
|
127 |
+
class ToYesOrNone(FieldOperator):
|
128 |
+
def process_value(self, text: Any) -> Any:
|
129 |
+
if text == "yes":
|
130 |
return "yes"
|
131 |
return "none"
|
132 |
|
133 |
|
134 |
+
class StanceToProCon(FieldOperator):
|
135 |
+
def process_value(self, text: Any) -> Any:
|
136 |
+
if text == "positive":
|
137 |
return "PRO"
|
138 |
+
if text in ["negative", "suggestion"]:
|
139 |
return "CON"
|
140 |
return "none"
|
141 |
|
142 |
|
143 |
+
class StringOrNotString(FieldOperator):
|
144 |
string: str
|
145 |
|
146 |
+
def process_value(self, text: Any) -> Any:
|
147 |
+
if "not " + self.string.lower() in text.lower():
|
148 |
return "not " + self.string.lower()
|
149 |
+
if self.string.lower() in text.lower():
|
150 |
return self.string.lower()
|
151 |
+
return text
|