-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_html.py
More file actions
104 lines (87 loc) · 2.97 KB
/
parse_html.py
File metadata and controls
104 lines (87 loc) · 2.97 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from bs4 import BeautifulSoup
from pprint import pprint
import json
import re
FILE_PATH = "transcript_html/0.html"
SYS_NAME = "Dr. Ali Marsh"
USER_NAME = "Julia"
def get_soup(file_path):
with open(file_path, "r") as file:
soup = BeautifulSoup(file, "html.parser")
return soup
def parse_html_sliding_window(file_path, sys_name, user_name, window_size=8, max_overlap=4):
data = []
curr = 0
end = curr + window_size
soup = get_soup(file_path)
tags = soup.select("p.transp")
conversations = []
while curr < len(tags):
# Get speaker
speaker = tags[curr].find("span", class_="speaker")
if speaker == None:
curr += 1
end += 1
continue
speaker = speaker.text.strip()
# Ensure conversation initiated by human
if curr == end - window_size and speaker == sys_name:
curr += 1
end += 1
continue
# Get text
text = tags[curr].find_all("span", class_="transspan")
text = "".join([re.sub('(\u00a0|\n)\s*', ' ', t.text) for t in text[1:]]).strip()
actor = "gpt" if speaker == sys_name else "human"
# Append Conversation
if conversations != [] and actor == conversations[-1]["from"]:
conversations[-1]["value"] += " " + text
end += 1
else:
conversations.append({
"from": actor,
"value": text
})
curr += 1
# Append to data if window is reached
if curr == end:
data.append({
"id": len(data),
"conversations": conversations
})
print(len(data))
conversations = []
curr = end - max_overlap
end = curr + window_size
return data
def parse_html_sequential(file_path, sys_name, user_name, window_size=8, max_overlap=4):
curr = 0
soup = get_soup(file_path)
tags = soup.select("p.transp")
conversations = []
while curr < len(tags):
# Get speaker
speaker = tags[curr].find("span", class_="speaker")
if speaker == None:
curr += 1
continue
speaker = speaker.text.strip()
# Get text
text = tags[curr].find_all("span", class_="transspan")
text = "".join([re.sub('(\u00a0|\n)\s*', ' ', t.text) for t in text[1:]]).strip()
actor = "gpt" if speaker == sys_name else "human"
# Append Conversation
if conversations != [] and actor == conversations[-1]["from"]:
conversations[-1]["value"] += " " + text
else:
conversations.append({
"from": actor,
"value": text
})
curr += 1
return conversations
if __name__ == "__main__":
dataset = parse_html_sequential(FILE_PATH, SYS_NAME, USER_NAME)
pprint(dataset)
with open("transcript_json/sequential.json", "w") as file:
json.dump(dataset, file, indent=4)