facebook / duckling

Language, engine, and tooling for expressing, testing, and evaluating composable language rules on input strings.
Other
4.08k stars 724 forks source link

[Time] Support for Fiscal Calendar #170

Open tbaptista opened 6 years ago

tbaptista commented 6 years ago

It seems that all of the date calculations are assuming the ISO 8601 calendar (at least for the EN language). It would be interesting to be able to use other calendars, such as the fiscal calendar used by companies in the US (see https://en.wikipedia.org/wiki/Fiscal_year and https://en.wikipedia.org/wiki/4–4–5_calendar).

I am using duckling in a project where such an issue has arisen, and the best solution would be to support it in duckling.

As I understand, the support for this may need first #29 to be complete.

patapizza commented 6 years ago

Hi @tbaptista, thanks for reaching out.

This is interesting. Can you tell a bit more about this? What are typical sentences you'd like to parse and what would be the resolution?

tbaptista commented 6 years ago

Hi @patapizza, thanks for the quick reply.

Here are some examples. Let's assume that we configure our fiscal calendar with a 4-4-5 pattern, starting in September, and ending the week on a Saturday (this is one example from https://en.wikipedia.org/wiki/4–4–5_calendar).

I am thinking of the system returning always the value from the ISO calendar, but assuming the fiscal calendar in the natural language text.

A good reference for this type of calendar is this implementation for java.time: http://www.threeten.org/threeten-extra/.

patapizza commented 6 years ago

Hi @tbaptista, thanks for providing the examples. At a quick glance, it seems like this could be implemented on top of assumptions (#29).

chessai commented 3 years ago

I disagree that this needs something as powerful as assumptions. When I hear "the last quarter of 2020", I immediately know the context (financial quarter). So I think we could definitely do something like

ruleFinancialQuarter :: Rule
ruleFinancialQuarter = Rule
  { name = "financial quarter"
  , pattern =
    [ regex "(last|previous|prior|current|next|upcoming) (financial)? quarter"
    ]
  , prod = \case
      (Token RegexMatch (GroupMatch (relative:_)):_) -> do
        ...case on relative...
      _ -> Nothing
  }

ruleFinancialQuarterOf :: Rule
ruleFinancialQuarterOf = Rule
  { name = "financial quarter of"
  , pattern =
    [ regex "(last|previous|prior|current|next|upcoming) (financial)? quarter (of|in)"
    , isGrainOfTime Year
    ]
  , prod = \case
      ...case on relative and provide the year...
  }