User:RexxS/GCI-2019-Task09

Lua Task 9 - Date formats (advanced)
Prerequisite: Lua Task 7 - Wikibase client. This task requires a lot of research and independent learning and is considerably more difficult than the introductory seven tasks. You should have successfully and comfortably completed all of the introductory tasks before attempting any of the advanced ones. It is not suitable for beginners to programming, although students new to Lua with previous experience in other programming languages should be able to produce acceptable solutions. Read through the entire task before starting work on it.

Background
On the English Wikipedia, we find 5 types of allowed date formats:
 * "dmy" – e.g. 31 August 2013
 * "mdy" – e.g. August 31, 2013
 * "iso"-style – e.g. 2013-08-27
 * year – e.g. 2013
 * month and year – e.g. August 2013

Wikipedia always gives month names in full (i.e. December, not Dec) and only uses all-numeric dates in special cases. Text that is imported from outside of Wikipedia may be in any of large number of formats, or sometimes malformed but understandable. There is often a need to take a piece of text and extract a date from it, displaying it in the required format. This is a common sort of task in natural language processing.

Examine the table below: Notes:
 * The first column has examples of text that may contain dates.
 * The second column contains the requested formats (from a parameter like mdy). If there is no format parameter given, then the date output attempts to match the format of the text supplied.
 * The third column shows the expected output. Certain words, e.g. "around", "uncertain" indicate that the date is approximate; we normally add "circa" before approximate dates.

Requirements
This task requires you to create your own function which can take text such as may be found in the first column and an optional format parameter. It will output a date either in the requested format or in a format matching that of the text supplied. You should test your function against all of the text shown in the table above, at least. Copy the table into your sandbox and change the entries in the last column to make calls to your function by supplying the value in the first column and the format in the second. See how close you can get to reproducing the table above.

To complete this task you will need to make use of the techniques you learned in the first six tasks, as well as doing further research on string-handling functions and patterns, and possibly making use of other libraries.

You must work in a fresh module sandbox and user sandbox. If I were doing the task, I would use Module:Sandbox/RexxS/Dates and User:RexxS/Sandbox/Dates.

Meeting the requirement to add "circa" may prove to be difficult, so get a function working without it to start with, and consider adding it in later if time permits. If you can get a function working without it, that is the minimum needed to successfully complete the task if you have no more time, but please try to accomplish the entire task if time permits.

Hints and tips
A. Plan your work in two parts: (i) extracting day, month, year and format from the text; and (ii) validating and re-assembling those four values into the required format.

B. You already know how to extract a simple dmy or mdy date from Task 5. Work out how to match an ISO-style date. Try these first, and only look at more complicated routines if they don't return a date.

C. One technique is to have a list of month names and to look for those and their three-letter abbreviations.

D. You may assume that if you can match three numbers or two numbers and a month name in the text, then there is probably a date. Numbers more than 31 can only be years; numbers greater than 12 could be month or year; smaller numbers could be any of day, month or year.

E. You may wish to use the os.date function to validate dates or you may prefer to write your own routines.