-
Notifications
You must be signed in to change notification settings - Fork 1
/
html_generator.py
200 lines (178 loc) · 7.3 KB
/
html_generator.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from typing import Dict, List
class HTMLGenerator:
def __init__(self, ignore_insertions: bool = True):
"""Initialize HTML generator with insertion handling configuration."""
self.css = self._get_css()
self.ignore_insertions = ignore_insertions
def _get_css(self) -> str:
"""Define CSS styles for the HTML output."""
return '''
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 2em;
color: #333;
}
.stats-summary {
background-color: #f8f9fa;
padding: 20px;
border-radius: 5px;
margin-bottom: 30px;
border: 1px solid #dee2e6;
}
.stat-box {
display: inline-block;
padding: 15px 25px;
margin: 10px;
background-color: white;
border-radius: 5px;
border: 1px solid #dee2e6;
text-align: center;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #2c3e50;
margin: 5px 0;
}
.stat-label {
font-size: 14px;
color: #6c757d;
text-transform: uppercase;
letter-spacing: 1px;
}
.error-summary {
background-color: #f5f5f5;
padding: 20px;
border-radius: 5px;
margin-top: 30px;
}
.error-type { margin-bottom: 20px; }
.error-example {
display: inline-block;
margin: 5px;
padding: 5px 10px;
background-color: #fff;
border-radius: 3px;
border: 1px solid #ddd;
}
.explanation {
background-color: #e9f5ff;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.error-count {
color: #666;
font-size: 0.9em;
margin-left: 10px;
}
'''
def _generate_header(self) -> str:
"""Generate HTML header section."""
return f'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WER Analysis</title>
<style>{self.css}</style>
</head>
<body>'''
def _generate_stats_section(self, stats: Dict) -> str:
"""Generate statistics summary section."""
# Build stat boxes dynamically
stat_boxes = [
f'''<div class="stat-box">
<div class="stat-label">Word Error Rate</div>
<div class="stat-value">{stats['wer']:.1f}%</div>
</div>
<div class="stat-box">
<div class="stat-label">Accuracy</div>
<div class="stat-value">{stats['accuracy']:.1f}%</div>
</div>
<div class="stat-box">
<div class="stat-label">Total Words</div>
<div class="stat-value">{stats['total_words']}</div>
</div>
<div class="stat-box">
<div class="stat-label">Total Errors</div>
<div class="stat-value">{stats['total_errors']}</div>
{" (excluding insertions)" if self.ignore_insertions else ""}
</div>'''
]
return f'''
<div class="stats-summary">
<h2>Overall Performance</h2>
{"".join(stat_boxes)}
</div>'''
def _generate_explanation(self) -> str:
"""Generate explanation section."""
error_types = [
"<strong>Substitutions</strong> (yellow): Words that were replaced with different words",
"<strong>Deletions</strong> (red): Words that were in the reference but missing from the transcription"
]
if not self.ignore_insertions:
error_types.append("<strong>Insertions</strong> (green): Extra words that appeared in the transcription but weren't in the reference")
error_list = "\n".join(f"<li>{error_type}</li>" for error_type in error_types)
return f'''
<div class="explanation">
<h2>Understanding Word Error Rate (WER)</h2>
<p>WER measures how accurately text was transcribed by comparing it to a reference text.
There are {'three' if not self.ignore_insertions else 'two'} types of errors:</p>
<ul>
{error_list}
</ul>
</div>'''
def _generate_segments(self, aligned_htmls: List[str]) -> str:
"""Generate segments section."""
segments = []
for i, html in enumerate(aligned_htmls, 1):
segments.append(f'<h3>Comparison:</h3>\n<div style="margin-bottom: 20px;">{html}</div>')
return '\n'.join(segments)
def _generate_error_summary(self, error_examples: Dict, error_counts: Dict[str, int]) -> str:
"""Generate error summary section in chronological order."""
error_sections = [
('sub', 'Substitutions (Incorrect → Correct)'),
('del', 'Deletions (Missing Words)')
]
# Only include insertions section if not ignored
if not self.ignore_insertions:
error_sections.append(('ins', 'Insertions (Extra Words)'))
sections = ['<div class="error-summary">\n<h2>Error Summary</h2>']
for err_type, title in error_sections:
if error_examples[err_type]:
count = error_counts[err_type]
sections.append(f'<div class="error-type">')
sections.append(f'<h3>{title}<span class="error-count">({count} words)</span></h3>')
if err_type == 'sub':
for hyp, ref in error_examples[err_type]:
sections.append(f'<span class="error-example">{hyp} → {ref}</span>')
else:
for word in error_examples[err_type]:
sections.append(f'<span class="error-example">{word}</span>')
sections.append('</div>')
sections.append('</div>')
return '\n'.join(sections)
def save_html(self,
filename: str,
stats: Dict,
aligned_htmls: List[str],
error_examples: Dict,
error_counts: Dict[str, int]) -> None:
"""Generate and save the complete HTML visualization."""
try:
html_filename = f'{filename}_diagnosis.html'
print(f"\nGenerating HTML visualization: {html_filename}")
html_parts = [
self._generate_header(),
self._generate_stats_section(stats),
self._generate_explanation(),
self._generate_segments(aligned_htmls),
self._generate_error_summary(error_examples, error_counts),
'</body>\n</html>'
]
with open(html_filename, 'w', encoding='utf-8') as f:
f.write('\n'.join(html_parts))
print(f"HTML visualization saved successfully")
except Exception as e:
print(f'Failed to generate HTML visualization: {str(e)}')