visualise: Begin breaking up main()

An effort to make it more readable and flexible
visualise: Ensure extract_week() is accurate across years
visualise: Avoid parsing database more than once

Previously required two passes over the database, one to generate
results grouped by month and the second by week. Modifying
group_period() so that it can generate multiple groups at once removes
this requirement, allowing stdin to be used to pump data into visualise
annotate: Remove unused global 'learn'
core: Begin centralising common code