-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_xml.py
More file actions
114 lines (96 loc) · 3.29 KB
/
Copy pathparse_xml.py
File metadata and controls
114 lines (96 loc) · 3.29 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
105
106
107
108
109
110
111
112
113
114
import xml.etree.ElementTree as ET
import csv
import re
# Parse XML
try:
tree = ET.parse('datasets.xml')
root = tree.getroot()
except Exception as e:
print(f"Error parsing XML: {e}")
exit(1)
# Finding mxCell elements.
cells = root.findall('.//mxCell')
nodes = {}
edges_list = []
def clean_label(label):
if not label:
return ""
# Remove HTML tags
label = re.sub(r'<[^>]+>', '', label)
# Unescape common entities
label = label.replace('&', '&').replace(' ', ' ').replace('<', '<').replace('>', '>')
return label.strip()
# Identify Nodes first
for cell in cells:
cid = cell.get('id')
value = cell.get('value')
style = cell.get('style', '')
if value:
clean_name = clean_label(value)
if clean_name:
node_type = "Unknown"
if "fillColor=#dae8fc" in style:
node_type = "Corpus"
elif "fillColor=#fff2cc" in style:
node_type = "Dataset"
nodes[cid] = {
'name': clean_name,
'type': node_type
}
# Identify Edges
for cell in cells:
source_id = cell.get('source')
target_id = cell.get('target')
style = cell.get('style', '')
if source_id and target_id:
if source_id in nodes and target_id in nodes:
src_node = nodes[source_id]
tgt_node = nodes[target_id]
# Determine attributes from style
is_dashed = False
if "dashed=1" in style:
is_dashed = True
# Stroke color logic
match = re.search(r'strokeColor=([^;]+)', style)
stroke_color = match.group(1) if match else "#000000"
stroke_color = stroke_color.lower()
speech_type = "Unknown Speech"
if "6c8ebf" in stroke_color:
speech_type = "Real Speech"
elif "d79b00" in stroke_color:
speech_type = "DF Speech"
elif "82b366" in stroke_color:
speech_type = "Real & DF Speech"
lang_content = "Non-English" if is_dashed else "English"
rel_type = f"{src_node['type']}->{tgt_node['type']}"
edges_list.append([
src_node['name'],
tgt_node['name'],
rel_type,
lang_content,
speech_type
])
# Verify and fix "In-the-Wild"
# Check if "In-the-Wild" is present
found = False
for edge in edges_list:
if edge[1] == "In-the-Wild":
found = True
break
if not found:
print("Fixing In-the-Wild edges...")
# Based on diagram:
# social media, internet (dashed blue) -> In-the-Wild
edges_list.append([
"social media, internet",
"In-the-Wild",
"Corpus->Dataset", # Assuming src is Corpus-like (blue)
"Non-English", # Dashed
"Real Speech" # Blue stroke
])
# Write to CSV
with open('relationships_all.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['Source', 'Target', 'Relationship Type', 'Language Content', 'Speech Type'])
writer.writerows(sorted(edges_list, key=lambda x: (x[0], x[1])))
print(f"Total relationships extracted: {len(edges_list)}")