-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_article.py
More file actions
388 lines (328 loc) · 14.4 KB
/
generate_article.py
File metadata and controls
388 lines (328 loc) · 14.4 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
"""
The D-AI-LY Article Generator
Generates Statistics Canada "The Daily"-style articles from CANSIM data.
Uses The Daily voice: neutral, clinical, inverted pyramid structure.
"""
import json
import re
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Any
def format_month_year(ref_date: str) -> str:
"""Convert '2025-11' to 'November 2025'."""
try:
date = datetime.strptime(ref_date, "%Y-%m")
return date.strftime("%B %Y")
except ValueError:
return ref_date
def format_change(value: float, is_percentage: bool = False) -> str:
"""Format a change value with appropriate direction language."""
if value > 0:
direction = "increased"
elif value < 0:
direction = "decreased"
value = abs(value)
else:
direction = "remained unchanged"
return direction
if is_percentage:
return f"{direction} {value:.1f}%"
else:
return f"{direction} {value:.1f}"
def generate_headline(data: Dict[str, Any]) -> str:
"""Generate a headline (max 15 words) with key number first."""
latest = data["latest"]
metadata = data["metadata"]
period = format_month_year(latest["ref_date"])
series_name = metadata.get("series_name", "")
mom_change = latest.get("mom_pct_change", 0)
yoy_change = latest.get("yoy_pct_change", 0)
if "Consumer Price Index" in metadata["table_title"] or series_name == "Consumer Price Index":
if yoy_change > 0:
return f"Consumer prices up {yoy_change:.1f}% year over year in {period}"
elif yoy_change < 0:
return f"Consumer prices down {abs(yoy_change):.1f}% year over year in {period}"
else:
return f"Consumer prices unchanged in {period}"
elif "Retail" in metadata["table_title"] or series_name == "Retail Sales":
# Retail uses month-over-month as the lead
if mom_change > 0:
return f"Retail sales up {mom_change:.1f}% in {period}"
elif mom_change < 0:
return f"Retail sales down {abs(mom_change):.1f}% in {period}"
else:
return f"Retail sales unchanged in {period}"
elif "Labour" in metadata["table_title"] or "Employment" in metadata["table_title"]:
return f"Employment update for {period}"
else:
# Generic headline
title_short = metadata["table_title"].split(",")[0]
if mom_change != 0:
direction = "up" if mom_change > 0 else "down"
return f"{title_short} {direction} {abs(mom_change):.1f}% in {period}"
return f"{title_short}: {period}"
def format_value(value: float, series_name: str) -> str:
"""Format a value based on the series type."""
if series_name == "Retail Sales":
# Value is in millions, convert to billions
return f"${value/1000:.1f} billion"
elif series_name == "Consumer Price Index":
return f"{value:.1f}"
else:
if value >= 1000000:
return f"{value/1000000:.1f} million"
elif value >= 1000:
return f"{value/1000:.1f} thousand"
else:
return f"{value:.1f}"
def generate_highlights(data: Dict[str, Any]) -> List[str]:
"""Generate 3-5 highlight bullets."""
latest = data["latest"]
comparison = data["comparison"]
time_series = data["time_series"]
metadata = data["metadata"]
series_name = metadata.get("series_name", "")
highlights = []
# Main finding
period = format_month_year(latest["ref_date"])
value = latest["value"]
yoy = latest.get("yoy_pct_change", 0)
mom = latest.get("mom_pct_change", 0)
if series_name == "Retail Sales":
# Retail sales highlights
value_str = format_value(value, series_name)
if mom is not None and mom != 0:
direction = "increased" if mom > 0 else "decreased"
highlights.append(
f"Retail sales {direction} {abs(mom):.1f}% to {value_str} in {period}."
)
else:
highlights.append(
f"Retail sales were {value_str} in {period}."
)
if yoy is not None and yoy != 0:
direction = "up" if yoy > 0 else "down"
highlights.append(
f"Sales were {direction} {abs(yoy):.1f}% compared with {period.split()[0]} of last year."
)
else:
# CPI / default highlights
if yoy is not None:
if yoy > 0:
highlights.append(
f"The Consumer Price Index rose {yoy:.1f}% on a year-over-year basis in {period}."
)
elif yoy < 0:
highlights.append(
f"The Consumer Price Index fell {abs(yoy):.1f}% on a year-over-year basis in {period}."
)
else:
highlights.append(
f"The Consumer Price Index was unchanged on a year-over-year basis in {period}."
)
# Month-over-month comparison
if mom is not None:
prev_period = format_month_year(comparison["previous_period"]["ref_date"])
if mom > 0:
highlights.append(
f"On a monthly basis, the CPI increased {mom:.2f}% from {prev_period}."
)
elif mom < 0:
highlights.append(
f"On a monthly basis, the CPI decreased {abs(mom):.2f}% from {prev_period}."
)
else:
highlights.append(
f"The index was unchanged from {prev_period}."
)
# Trend context (compare to 3 months ago)
if len(time_series) >= 4:
three_months_ago = time_series[-4]
three_month_yoy = three_months_ago.get("yoy_pct_change")
if three_month_yoy is not None and yoy is not None:
diff = yoy - three_month_yoy
period_3m = format_month_year(three_months_ago["ref_date"])
if abs(diff) >= 0.1:
direction = "higher" if diff > 0 else "lower"
highlights.append(
f"The year-over-year rate was {abs(diff):.1f} percentage points {direction} than in {period_3m}."
)
return highlights[:5] # Max 5 highlights
def generate_sections(data: Dict[str, Any]) -> List[str]:
"""Generate article body sections."""
latest = data["latest"]
comparison = data["comparison"]
metadata = data["metadata"]
time_series = data["time_series"]
series_name = metadata.get("series_name", "")
sections = []
# Lede paragraph
period = format_month_year(latest["ref_date"])
value = latest["value"]
yoy = latest.get("yoy_pct_change", 0)
mom = latest.get("mom_pct_change", 0)
if series_name == "Retail Sales":
value_str = format_value(value, series_name)
lede = f"Retail sales totalled {value_str} in {period}"
if mom is not None and mom != 0:
direction = "up" if mom > 0 else "down"
lede += f", {direction} {abs(mom):.1f}% from the previous month"
lede += "."
if yoy is not None and yoy != 0:
direction = "higher" if yoy > 0 else "lower"
lede += f" Sales were {abs(yoy):.1f}% {direction} than in the same month last year."
sections.append(lede)
# Month-over-month detail
if comparison.get("previous_period"):
prev_value = comparison["previous_period"]["value"]
prev_value_str = format_value(prev_value, series_name)
prev_period = format_month_year(comparison["previous_period"]["ref_date"])
if mom != 0:
direction = "increased" if mom > 0 else "decreased"
mom_section = (
f"On a month-over-month basis, retail sales {direction} from "
f"{prev_value_str} in {prev_period}."
)
else:
mom_section = f"Sales were unchanged from {prev_period}."
sections.append(mom_section)
# Historical context for retail
if len(time_series) >= 12:
yoy_rates = [d["yoy_pct_change"] for d in time_series if d.get("yoy_pct_change") is not None]
if yoy_rates:
max_yoy = max(yoy_rates)
min_yoy = min(yoy_rates)
if max_yoy != min_yoy:
context = (
f"Over the past two years, year-over-year retail sales growth has ranged "
f"from {min_yoy:.1f}% to {max_yoy:.1f}%."
)
sections.append(context)
else:
# CPI / default sections
lede = (
f"The Consumer Price Index (CPI) stood at {value:.1f} in {period}, "
)
if yoy is not None and yoy != 0:
direction = "up" if yoy > 0 else "down"
lede += f"{direction} {abs(yoy):.1f}% compared with the same month a year earlier. "
if comparison.get("year_ago"):
year_ago_value = comparison["year_ago"]["value"]
lede += f"A year ago, the index was {year_ago_value:.1f}."
sections.append(lede)
# Month-over-month context
if mom is not None:
prev_value = comparison["previous_period"]["value"]
prev_period = format_month_year(comparison["previous_period"]["ref_date"])
if mom != 0:
direction = "increased" if mom > 0 else "decreased"
mom_section = (
f"On a month-over-month basis, the index {direction} {abs(mom):.2f}% "
f"from {prev_value:.1f} in {prev_period} to {value:.1f} in {period}."
)
else:
mom_section = (
f"The index was unchanged from {prev_period}, remaining at {value:.1f}."
)
sections.append(mom_section)
# Historical context
if len(time_series) >= 12:
yoy_rates = [d["yoy_pct_change"] for d in time_series if d.get("yoy_pct_change") is not None]
if yoy_rates:
max_yoy = max(yoy_rates)
min_yoy = min(yoy_rates)
if max_yoy != min_yoy:
context = (
f"Over the past two years, the year-over-year inflation rate has ranged "
f"from {min_yoy:.1f}% to {max_yoy:.1f}%."
)
sections.append(context)
return sections
def generate_note_to_readers(data: Dict[str, Any]) -> str:
"""Generate the methodology/notes section."""
metadata = data["metadata"]
series_name = metadata.get("series_name", "")
if series_name == "Retail Sales":
return (
"Retail trade sales represent the value of all sales made through retail channels, "
"including both in-store and online transactions. Data are seasonally adjusted to "
"account for regular seasonal patterns. Values are expressed in current dollars. "
"For more information, consult Statistics Canada's retail trade portal."
)
else:
return (
"The Consumer Price Index (CPI) measures the rate of price change experienced by "
"Canadian consumers. It is calculated by comparing the cost of a fixed basket of "
"goods and services purchased by consumers over time. The CPI is not seasonally adjusted. "
"For more information, consult Statistics Canada's Consumer Price Index portal."
)
def generate_article(data_path: str, output_path: str) -> str:
"""Generate a complete article from data JSON."""
# Load data
with open(data_path, "r") as f:
data = json.load(f)
# Generate content
headline = generate_headline(data)
highlights = generate_highlights(data)
sections = generate_sections(data)
note = generate_note_to_readers(data)
series_name = data["metadata"].get("series_name", "")
# Chart title and y-axis based on series type
if series_name == "Retail Sales":
chart_title = f"Retail Sales, {format_month_year(data['time_series'][0]['ref_date'])} to {format_month_year(data['latest']['ref_date'])}"
chart_y_label = "Sales ($ millions)"
else:
chart_title = f"Consumer Price Index, {format_month_year(data['time_series'][0]['ref_date'])} to {format_month_year(data['latest']['ref_date'])}"
chart_y_label = "Index (2002=100)"
# Load template
template_path = Path(__file__).parent / "templates" / "article.html"
with open(template_path, "r") as f:
template = f.read()
# Replace placeholders
# Convert time series to JSON for embedding
time_series_json = json.dumps(data["time_series"])
# Simple template substitution
html = template
html = html.replace("{{headline}}", headline)
html = html.replace("{{release_date}}", datetime.now().strftime("%B %d, %Y"))
html = html.replace("{{chart_title}}", chart_title)
html = html.replace("{{chart_y_label}}", chart_y_label)
html = html.replace("{{note_to_readers}}", note)
html = html.replace("{{table_number}}", data["metadata"]["table_number"])
html = html.replace("{{reference_period}}", format_month_year(data["latest"]["ref_date"]))
html = html.replace("{{time_series_json}}", time_series_json)
# Handle highlights (mustache-style list)
highlights_html = "\n".join(f" <li>{h}</li>" for h in highlights)
html = re.sub(
r'\{\{#highlights\}\}.*?\{\{/highlights\}\}',
highlights_html,
html,
flags=re.DOTALL
)
# Handle sections
sections_html = "\n".join(f" <p>{s}</p>" for s in sections)
html = re.sub(
r'\{\{#sections\}\}.*?\{\{/sections\}\}',
sections_html,
html,
flags=re.DOTALL
)
# Write output
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w") as f:
f.write(html)
print(f"Article generated: {output_path}")
print(f"Headline: {headline}")
print(f"Highlights: {len(highlights)}")
print(f"Sections: {len(sections)}")
return output_path
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python generate_article.py <data.json> [output.html]")
print("Example: python generate_article.py output/data_18_10_0004.json output/article.html")
sys.exit(1)
data_path = sys.argv[1]
output_path = sys.argv[2] if len(sys.argv) > 2 else "output/article.html"
generate_article(data_path, output_path)