-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
50 lines (36 loc) · 1.55 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re
class ParseCSV:
def __call__(self, df):
df["ip_stance"] = df.generated_text.map(
lambda x: self.get_category(x, "israel-palestine stance:.+\n"))
df["ur_stance"] = df.generated_text.map(
lambda x: self.get_category(x, "russia-ukraine stance:.+\n"))
df["ip_score"] = df.generated_text.map(
lambda x: self.get_score(x, 0))
df["ru_score"] = df.generated_text.map(
lambda x: self.get_score(x, 1))
df["ip_reason"] = df.generated_text.map(
lambda x: self.get_reason(x, 0))
df["ru_reason"] = df.generated_text.map(
lambda x: self.get_reason(x, 1))
return df
def get_reason(self, text, id):
pattern = "(?<=reason:).+"
matches = [match for match in re.finditer(pattern, text)]
if matches is None:
return None
return text[matches[id].start() : matches[id].end()].strip()
def get_category(self, text, pattern):
match = re.search(pattern, text)
if match is None:
return None
return text[match.start():match.end()].split()[-1].lower()
def get_score(self, text, id):
pattern = "score:.+\n"
matches = [match for match in re.finditer(pattern, text)]
if matches is None:
return None
try:
return float(text[matches[id].start() : matches[id].end()].split()[-1])
except Exception:
return None