Lua Task 9 - Date formats (advanced)
editPrerequisite: 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
editOn 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:
Text | Format | Date |
---|---|---|
31 december 2019 | 31 December 2019 | |
31 December 2019 | mdy | December 31, 2019 |
December 31, 2019 | iso | 2019-12-31 |
31 December 2019 (uncertain) | year | circa 2019 |
31 December 2019 | iso | 2019-12-31 |
29 February 2004 (uncertain) | mdy | circa February 29, 2004 |
29 February 2005 (uncertain) | mdy | Invalid entry |
31/12/2019 | 2019-12-31 | |
2019-12-31 | mdy | December 31, 2019 |
2019 (uncertain) | circa 2019 | |
31 | 31 | |
31 December | 31 December | |
31 2019 | 2019 | |
sometime around 27th December 2019 | circa 27 December 2019 | |
sometime around 3rd December 2019 | circa 3 December 2019 | |
on the 16th of December in the year of our Lord 1770 | 16 December 1770 | |
99 red balloons | 99 | |
20/06/2019 | mdy | June 20, 2019 |
31 August 103 AD | 31 August 103 AD | |
31 August 2019 BC | 31 August 2019 BC | |
31 August 2019 BCE | 31 August 2019 BCE | |
31 August 103 CE | 31 August 103 CE | |
2019-08-31 | 2019-08-31 | |
31 August 213 | 31 August 213 | |
213 | 213 | |
31 August 13 | 31 August 13 | |
31 August 13 BC | 31 August 13 BC | |
30 BCE | 30 BCE | |
3 may 2017 | 3 May 2017 | |
3 Jan 2017 | 3 January 2017 | |
3 jan 9 AD | 3 January 9 AD | |
31 February 2013 | mdy | Invalid entry |
the quick brown fox | Invalid entry | |
4 and 20 blackbirds ... | Invalid entry |
Notes:
- The first column has examples of text that may contain dates.
- The second column contains the requested formats (from a parameter like
|format=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
editThis 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
edit- 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.