Skip to content

Commit

Permalink
Merge pull request #833 from spencermountain/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
spencermountain authored Apr 17, 2021
2 parents 2ce0f78 + b93b522 commit 4579627
Show file tree
Hide file tree
Showing 51 changed files with 941 additions and 616 deletions.
716 changes: 447 additions & 269 deletions plugins/dates/builds/compromise-dates.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/dates/builds/compromise-dates.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/dates/builds/compromise-dates.mjs

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion plugins/dates/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<!-- #### [Unreleased]
- **[breaking]** - return array in .json().dates
-->

### 2.2.0 [April 2021]

- **[new]** - start parsing or/and date combos

### 2.1.0 [April 2021]

- **[change]** - null timezone is now the same as undefined timezone
Expand Down
2 changes: 1 addition & 1 deletion plugins/dates/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "compromise-dates",
"description": "plugin for nlp-compromise",
"version": "2.0.3",
"version": "2.2.0",
"author": "Spencer Kelly <[email protected]> (http://spencermounta.in)",
"main": "./builds/compromise-dates.js",
"unpkg": "./builds/compromise-dates.min.js",
Expand Down
17 changes: 12 additions & 5 deletions plugins/dates/scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@ const spacetime = require('spacetime')
nlp.extend(require('../../plugins/numbers/src'))
nlp.extend(require('../../plugins/dates/src'))
// nlp.verbose(true)
nlp.verbose('date')
// nlp.verbose('date')

const fmt = (iso) => (iso ? spacetime(iso).format('{day-short} {nice} {year}') : '-')

const context = {
// today: '2021-04-17', //saturday
// timezone: null, //'Asia/Shanghai',
today: '2021-04-01', //friday
// today: '2020-01-21',
// today: [2016, 10, 23], //wed nov 23rd
// today: '2018-01-19T10:05:00',
// dayStart: '8:00am',
// dayEnd: '8:00pm',
}

// let doc = nlp('22-23 Feb 2016 at 7pm')
// let doc = nlp('ten to 4 in the morning')
// let doc = nlp('february 5th or 7th')
let doc = nlp('2016-feb-01')
let dates = doc.dates(context).debug()
// let doc = nlp('february 5th, 6th or 7th') //.debug()
let doc = nlp('during march or september').debug()
// let doc = nlp('march 1')
// let doc = nlp('next friday, this monday').debug()
// let doc = nlp('wednesdays at 3') //.debug()
let dates = doc.dates(context) //.debug()
// console.log(dates.json(0))
dates = dates.get()
// console.log(dates)
dates.forEach((date) => {
console.log('start: ', fmt(date.start))
console.log(' end: ', fmt(date.end))
Expand Down
19 changes: 18 additions & 1 deletion plugins/dates/src/01-tagger/00-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const tagYearSafe = (m, reason) => {
}

const tagDates = function (doc) {
// doc.debug()
// in the evening
doc.match('in the (night|evening|morning|afternoon|day|daytime)').tag('Time', 'in-the-night')
// 8 pm
Expand All @@ -45,6 +46,14 @@ const tagDates = function (doc) {
// misc weekday words
doc.match('(tue|thu)').tag('WeekDay', 'misc-weekday')

doc
.match('(march|april|may) (and|to|or|through|until)? (march|april|may)')
.tag('Date')
.match('(march|april|may)')
.tag('Month', 'march|april|may')
// april should almost-always be a date
// doc.match('[april] !#LastName?', 0).tag('Month', 'april')

//months:
let month = doc.if('#Month')
if (month.found === true) {
Expand All @@ -55,9 +64,15 @@ const tagDates = function (doc) {
//5 March
month.match('#Cardinal #Month').tag('Date', 'cardinal-month')
//march 5 to 7
month.match('#Month #Value to #Value').tag('Date', 'value-to-value')
month.match('#Month #Value (and|or|to)? #Value+').tag('Date', 'value-to-value')
//march the 12th
month.match('#Month the #Value').tag('Date', 'month-the-value')
// march to april
month.match('(march|may) to? #Date').tag('Date').match('^.').tag('Month', 'march-to')
// 'march'
month.match('^(march|may)$').tag('Month', 'single-march')
//March or June
month.match('#Month or #Month').tag('Date', 'month-or-month')
}

//months:
Expand Down Expand Up @@ -187,6 +202,8 @@ const tagDates = function (doc) {
doc.match('(from|starting|until|by) now').tag('Date', 'for-now')
// every night
doc.match('(each|every) night').tag('Date', 'for-now')

// doc.debug()
return doc
}
module.exports = tagDates
2 changes: 2 additions & 0 deletions plugins/dates/src/01-tagger/01-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const values = function (doc) {
doc.match('#Value and a half (years|months|weeks|days)').tag('Date', here)
//on the fifth
doc.match('on the #Ordinal').tag('Date', here)
// 'jan 5 or 8'
doc.match('#Month #Value+ (and|or) #Value').tag('Date', 'date-or-date')
}
return doc
}
Expand Down
20 changes: 10 additions & 10 deletions plugins/dates/src/01-tagger/08-fixup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,34 @@ const fixUp = function (doc) {
// d.match(`(this|last|next) #Date ${knownDate}$`).unTag('Date').lastTerm().tag('Date', 'this month yesterday')
}
//tomorrow on 5
d.match(`on #Cardinal$`).unTag('Date', here)
d.match(`on #Cardinal$`).unTag('Date', 'on 5')
//this tomorrow
d.match(`this tomorrow`).terms(0).unTag('Date', 'this-tomorrow')
//q2 2019
d.match(`(q1|q2|q3|q4) #Year`).tag('Date', here)
//5 tuesday
// d.match(`^#Value #WeekDay`).terms(0).unTag('Date');
//5 next week
d.match(`^#Value (this|next|last)`).terms(0).unTag('Date', here)
d.match(`^#Value (this|next|last)`).terms(0).unTag('Date', '4 next')

if (d.has('(last|this|next)')) {
//this month 7
d.match(`(last|this|next) #Duration #Value`).terms(2).unTag('Date', here)
d.match(`(last|this|next) #Duration #Value`).terms(2).unTag('Date', 'this month 7')
//7 this month
d.match(`!#Month #Value (last|this|next) #Date`).terms(0).unTag('Date', here)
d.match(`!#Month #Value (last|this|next) #Date`).terms(0).unTag('Date', '7 this month')
}
//january 5 5
if (d.has('(#Year|#Time|#TextValue|#NumberRange)') === false) {
d.match('(#Month|#WeekDay) #Value #Value').terms(2).unTag('Date', here)
}
// if (d.has('(#Year|#Time|#TextValue|#NumberRange)') === false) {
// d.match('(#Month|#WeekDay) #Value #Value !(or|and)?').terms(2).unTag('Date', 'jan 5 5')
// }
//between june
if (d.has('^between') && !d.has('and .')) {
d.unTag('Date', here)
}
//june june
if (d.has('#Month #Month') && !d.has('@hasHyphen') && !d.has('@hasComma')) {
d.match('#Month').lastTerm().unTag('Date', 'month-month')
}
// if (d.has('#Month #Month') && !d.has('@hasHyphen') && !d.has('@hasComma')) {
// d.match('#Month').lastTerm().unTag('Date', 'month-month')
// }
// over the years
d.match('(in|over) the #Duration #Date+?').unTag('Date', 'over-the-duration')
// log the hours
Expand Down
49 changes: 49 additions & 0 deletions plugins/dates/src/02-find/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const split = require('./split')

const findDate = function (doc) {
if (doc.world.isVerbose() === 'date') {
doc.debug()
console.log(' ---')
}
let dates = doc.match('#Date+')
// ignore only-durations like '20 minutes'
dates = dates.filter((m) => {
let isDuration = m.has('^#Duration+$') || m.has('^#Value #Duration+$')
// allow 'q4', etc
if (isDuration === true && m.has('(#FinancialQuarter|quarter)')) {
return true
}
return isDuration === false
})
// 30 minutes on tuesday
let m = dates.match('[#Cardinal #Duration (in|on|this|next|during|for)] #Date', 0)
if (m.found) {
dates = dates.not(m)
}
// 30 minutes tuesday
m = dates.match('[#Cardinal #Duration] #WeekDay', 0)
if (m.found) {
dates = dates.not(m)
}
// tuesday for 30 mins
m = dates.match('#Date [for #Value #Duration]$', 0)
if (m.found) {
dates = dates.not(m)
}
// '20 minutes june 5th'
m = dates.match('[#Cardinal #Duration] #Date', 0) //but allow '20 minutes ago'
if (m.found && !dates.has('#Cardinal #Duration] (ago|from|before|after|back)')) {
dates = dates.not(m)
}
// for 20 minutes
m = dates.match('for #Cardinal #Duration')
if (m.found) {
dates = dates.not(m)
}
// 'one saturday'
dates = dates.notIf('^one (#WeekDay|#Month)$')
// tokenize the dates
dates = split(dates)
return dates
}
module.exports = findDate
74 changes: 74 additions & 0 deletions plugins/dates/src/02-find/split.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// chop things up into bite-size pieces
const split = function (dates) {
let m = null
// don't split anything if it looks like a range
if (dates.has('^(between|within) #Date')) {
return dates
}

if (dates.has('#Month')) {
// 'june 5, june 10'
m = dates.match('[#Month #Value] and? #Month', 0).ifNo('@hasDash$')
if (m.found) {
dates = dates.splitAfter(m)
}

// '5 june, 10 june'
m = dates.match('[#Value #Month] and? #Value #Month', 0)
if (m.found) {
dates = dates.splitAfter(m)
}

// 'june, august'
m = dates.match('^[#Month] and? #Month #Ordinal?$', 0)
if (m.found) {
dates = dates.splitAfter(m)
}

// 'june 5th, june 10th'
m = dates.match('[#Month #Value] #Month', 0).ifNo('@hasDash$')
if (m.found) {
dates = dates.splitAfter(m)
}
}

if (dates.has('#WeekDay')) {
// 'tuesday, wednesday'
m = dates.match('^[#WeekDay] and? #WeekDay$', 0).ifNo('@hasDash$')
if (m.found) {
dates = dates.splitAfter(m)
}

// 'tuesday, wednesday, and friday'
m = dates.match('#WeekDay #WeekDay and? #WeekDay')
if (m.found) {
dates = dates.splitOn('#WeekDay')
}

// monday, wednesday
m = dates.match('[#WeekDay] (and|or|this|next)? #WeekDay', 0).ifNo('@hasDash$')
if (m.found) {
dates = dates.splitAfter('#WeekDay')
}
}

// next week tomorrow
m = dates.match('(this|next) #Duration [(today|tomorrow|yesterday)]', 0)
if (m.found) {
dates = dates.splitBefore(m)
}
// tomorrow 15 march
m = dates.match('[(today|tomorrow|yesterday)] #Value #Month', 0)
if (m.found) {
dates = dates.splitAfter(m)
}
// tomorrow yesterday
m = dates.match('[(today|tomorrow|yesterday)] (today|tomorrow|yesterday|#WeekDay)', 0).ifNo('@hasDash$')
if (m.found) {
dates = dates.splitAfter(m)
}
// cleanup any splits
dates = dates.not('^and')
return dates
}
module.exports = split
58 changes: 0 additions & 58 deletions plugins/dates/src/02-ranges/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const parseDate = require('../03-parse')
const parseDate = require('../04-parse')
const reverseMaybe = require('./_reverse')

const moveToPM = function (obj) {
Expand All @@ -23,7 +23,7 @@ module.exports = [
let end = parseDate(to, context)
if (end) {
let start = end.clone()
start.applyTime(from.text())
start.applyTime(from.text('reduced'))
if (start) {
let obj = {
start: start,
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = [
from = parseDate(from, context)
if (from) {
let end = from.clone()
end.applyTime(to.text())
end.applyTime(to.text('reduced'))
if (end) {
let obj = {
start: from,
Expand Down
Loading

0 comments on commit 4579627

Please sign in to comment.