Exploring various methods is key, ranging from easy to more complex.
A straightforward approach
One simple variation involves always having three parts in the time string, keeping the format consistent:
"<quantity token> <time span token> ago"
The concept is quite basic:
- Divide the time string into individual components
- Transform each component into a meaningful format (a numerical value for the
<quantity token>
or milliseconds for the <time span token>
)
- Calculate the total milliseconds by multiplying the values obtained. Repeat for all input time strings to create an array of millisecond values representing time passed
- Ensure the millisecond values are sorted in ascending order
Additional tokens have been included to illustrate the extensibility of this method beyond the provided input.
const times = ["a day ago", "10 days ago", "3 months ago", "6 months ago"];
/tests?/.test("a day ago");
const termsOfQuanity = {
a: 1,
an: 1,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
};
const milliSecondsPerTimeSpan = {
seconds: 1000,
minutes: 60000,
hours: 3600000,
days: 86400000,
weeks: 604800000,
months: 2629800000,
years: 31557600000,
};
const timeSpans = {
...
...
Overall insights on the approach:
- Steps 3 and 4 are fundamental and prevalent in most solutions to this issue.
- Steps 1 (considering varying token quantities) and 2 pose more complex challenges, allowing room for intricate coding solutions
Potential enhancements/ alternative methods
- Based on the number of tokens, specific processing steps can be applied
- For instance, if only one token is present: check for terms like
today
, yesterday
, etc.
- With multiple tokens, trial-and-error parsing can determine quantitative or timespan tokens, accommodating potential errors
- Utilizing advanced RegEx patterns can replace object lookups, offering flexibility for time span token recognition
- Implementing AI models for millisecond extraction can handle unanticipated time strings, ensuring comprehensive parsing capabilities
- Combining approaches, beginning with a simple version and transitioning to AI models when necessary, can optimize resource utilization
While natural language processing may not guarantee absolute accuracy, AI technologies can approximate perfection. Typically, a simple solution suffices, assuming machine-generated time strings adhere to predictable patterns. Ongoing monitoring is recommended to identify and adapt to pattern shifts promptly.
The ambiguity in precise solutions
Some unquantified terms like few, a couple in phrases such as a few minutes ago, a couple hours ago introduce a challenge in comparison. Which term holds greater significance - a couple or a few?