A natural language date/time parser with pluggable rules

when godoc wercker status

when is a natural language date/time parser with pluggable rules and merge strategies

Examples

  • tonight at 11:10 pm
  • at Friday afternoon
  • the deadline is next tuesday 14:00
  • drop me a line next wednesday at 2:25 p.m
  • it could be done at 11 am past tuesday

Check EN, RU and BR rules and tests for them, for more examples.

Needed rule not found? Open an issue with the case and it will be added asap.

How it works

Usually, there are several rules added to the parser's instance for checking. Each rule has its own borders - length and offset in provided string. Meanwhile, each rule yields only the first match over the string. So, the library checks all the rules and extracts a cluster of matched rules which have distance between each other less or equal to options.Distance, which is 5 by default. For example:

on next wednesday at 2:25 p.m.
   └──────┬─────┘    └───┬───┘
       weekday      hour + minute

So, we have a cluster of matched rules - "next wednesday at 2:25 p.m." in the string representation.

After that, each rule is applied to the context. In order of definition or in match order, if options.MatchByOrder is set to true(which it is by default). Each rule could be applied with given merge strategy. By default, it's an Override strategy. The other strategies are not implemented yet in the rules. Pull requests are welcome.

Usage

w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)

text := "drop me a line in next wednesday at 2:25 p.m"
r, err := w.Parse(text, time.Now())
if err != nil {
	// an error has occurred
}
if  r == nil {
 	// no matches found
}

fmt.Println(
	"the time",
	r.Time.String(),
	"mentioned in",
	text[r.Index:r.Index+len(r.Text)],
)

Distance Option

w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)

text := "February 23, 2019 | 1:46pm"

// With default distance (5):
// February 23, 2019 | 1:46pm
//            └───┬───┘
//           distance: 9 (1:46pm will be ignored)

r, _ := w.Parse(text, time.Now())
fmt.Printf(r.Time.String())
// "2019-02-23 09:21:21.835182427 -0300 -03"
// 2019-02-23 (correct)
//   09:21:21 ("wrong")

// With custom distance (10):
w.SetOptions(&rules.Options{
	Distance:     10,
	MatchByOrder: true})

r, _ = w.Parse(text, time.Now())
fmt.Printf(r.Time.String())
// "2019-02-23 13:46:21.559521554 -0300 -03"
// 2019-02-23 (correct)
//   13:46:21 (correct)

State of the project

The project is in a more-or-less complete state. It's used for one project already. Bugs will be fixed as soon as they will be found.

TODO

  • readme: describe all the existing rules
  • implement missed rules for these examples
  • add cli and simple rest api server(#2)

LICENSE

http://www.apache.org/licenses/LICENSE-2.0

Owner
Oleg Lebedev
Don’t let the Canva bugs bite
Oleg Lebedev
Comments
  • Ability to use a different timezone?

    Ability to use a different timezone?

    I couldn't see to find any timezone parsing code. So, for example, if I am saying "10 PM EST" while the machine's local time is in PST then I expect 7 PM as the outcome in local time or 10 PM with the timezone information extracted.

  • Truncate

    Truncate "10pm" to "10:00:00" as opposed to "10:00:12" (current second)

    Thanks for the great library. I have no illusions of this getting merged (since the lib seems unmaintained by now), but this annoyed me enough to create a PR :-)

    Currently "10pm" is expanded to "10:00:x" with x being the current second. So if it's 8:23:12pm right now and you say "10pm", it'll be "10:00:12", which is not really what the user expects.

    I forked it and made keep this small change in a tag. You can use it like this:

    replace github.com/olebedev/when => github.com/binwiederhier/when v0.0.1-binwiederhier2
    
  • Added simple parse exact month date for ru langugage

    Added simple parse exact month date for ru langugage

    First of all, thanks for the package, i have my own solution, but it more hardcoded:)

    I do not pretend to be the best solution, but it seemed to me necessary to add the simplest definition of the number along with the month

    And i think it needs ignore idea folder and go fmt code :)

    BTW, what about naming? Why you use SNAKE_CASE instead CamelCase? What about rename them all?

  • Add past time rules: T ago

    Add past time rules: T ago

    This adds english rules for writing "T ago". The commit means that the following examples should work:

    5 minutes ago 2 hours ago a month ago half a month ago etc.

    It does not cover more complex rules such as "1 year ago tomorrow".

    Let me know if there is anything that needs updating.

    Thanks!

  • bias ExactMonthDate towards future dates

    bias ExactMonthDate towards future dates

    Hi! I picked up when in a quick project to file tasks into notion, and it's been great so far! I'm glad I don't have to rewrite all of this natural time parsing myself. Being the end of the year, I've been trying to schedule tasks for next January when I go back to work, and was surprised when they were parsed as January of 2021 instead of 2022.

    I was wondering:

    • Is it intentional that ExactMonthDate parses, e.g. January 4th as 2021-01-04 instead of 2022-01-04?
    • If not, would it be possible to bias ExactMonthDate the next occurrence of the date? This is a breaking change to the library, so I understand if it's not acceptable to upstream.

    I do have a working approach (that isn't particularly well thought out :grin:) which I could turn over to a pull request if y'all are interested.

    Thanks!

  • Is it possible to have 'deadlines' extended to work without 'in/within'

    Is it possible to have 'deadlines' extended to work without 'in/within'

    It would be nice to be able to parse "2 hours" or "4 days" instead of requiring the in/within, maybe it's a bit "noisy" but if it were possible via an option or some sort of "aggressive" version of deadline i think it could be nice - sometimes you just want to parse a time no matter what, and it won't always have the in/within at the start

  • Dependent dates

    Dependent dates

    when cannot parse dates with relative delta from some date:

    gore> r, err := w.Parse("завтра в 12:00 поезд, напомни за час", time.Now())
    &when.Result{
      Index:  0,
      Text:   "завтра в 12:00",
      Source: "завтра в 12:00 поезд, напомни за час",
      Time:   2019-02-28 12:00:28 Local,
    }
    nil
    

    BTW, I have old rutimeparser lib for Python. Maybe, some test cases will be useful for you.

Selected Machine Learning algorithms for natural language processing and semantic analysis in Golang

Natural Language Processing Implementations of selected machine learning algorithms for natural language processing in golang. The primary focus for t

Dec 25, 2022
Self-contained Machine Learning and Natural Language Processing library in Go

If you like the project, please ★ star this repository to show your support! ?? A Machine Learning library written in pure Go designed to support rele

Dec 30, 2022
Natural language detection library for Go
Natural language detection library for Go

Whatlanggo Natural language detection for Go. Features Supports 84 languages 100% written in Go No external dependencies Fast Recognizes not only a la

Dec 28, 2022
Stemmer packages for Go programming language. Includes English, German and Dutch stemmers.

Stemmer package for Go Stemmer package provides an interface for stemmers and includes English, German and Dutch stemmers as sub-packages: porter2 sub

Dec 14, 2022
Gopher-translator - A HTTP API that accepts english word or sentences and translates them to Gopher language

Gopher Translator Service An interview assignment project. To see the full assig

Jan 25, 2022
Complete Translation - translate a document to another language
 Complete Translation - translate a document to another language

Complete Translation This project is to translate a document to another language. The initial target is English to Korean. Consider this project is no

Feb 25, 2022
A natural language date/time parser with pluggable rules

when when is a natural language date/time parser with pluggable rules and merge strategies Examples tonight at 11:10 pm at Friday afternoon the deadli

Dec 26, 2022
Natural-deploy - A natural and simple way to deploy workloads or anything on other machines.

Natural Deploy Its Go way of doing Ansibles: Motivation: Have you ever felt when using ansible or any declarative type of program that is used for dep

Jan 3, 2022
A fast ISO8601 date parser for Go

A fast ISO8601 date parser for Go go get github.com/relvacode/iso8601 The built-in RFC3333 time layout in Go is too restrictive to support any ISO860

Jan 7, 2023
A parser generator where rules defined as go structs and code generation is optional

A parser generator where rules defined as go structs and code generation is optional. The concepts are introduced in the simple example below.

Jul 1, 2022
Quick and simple parser for PFSense XML configuration files, good for auditing firewall rules

pfcfg-parser version 0.0.1 : 13 January 2022 A quick and simple parser for PFSense XML configuration files to generate a plain text file of the main c

Jan 13, 2022
Selected Machine Learning algorithms for natural language processing and semantic analysis in Golang

Natural Language Processing Implementations of selected machine learning algorithms for natural language processing in golang. The primary focus for t

Dec 25, 2022
Natural language detection package in pure Go

getlang getlang provides fast natural language detection in Go. Features Offline -- no internet connection required Supports 29 languages Provides ISO

Dec 26, 2022
Selected Machine Learning algorithms for natural language processing and semantic analysis in Golang

Natural Language Processing Implementations of selected machine learning algorithms for natural language processing in golang. The primary focus for t

Dec 25, 2022
Self-contained Machine Learning and Natural Language Processing library in Go

If you like the project, please ★ star this repository to show your support! ?? A Machine Learning library written in pure Go designed to support rele

Dec 30, 2022
Natural language detection library for Go
Natural language detection library for Go

Whatlanggo Natural language detection for Go. Features Supports 84 languages 100% written in Go No external dependencies Fast Recognizes not only a la

Dec 28, 2022
Guess the natural language of a text in Go

guesslanguage This is a Go version of python guess-language. guesslanguage provides a simple way to detect the natural language of unicode string and

Dec 26, 2022
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike

?? The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike

Dec 25, 2022
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike

Its task is simple: It tells you which language some provided textual data is written in. This is very useful as a preprocessing step for linguistic data in natural language processing applications such as text classification and spell checking. Other use cases, for instance, might include routing e-mails to the right geographically located customer service department, based on the e-mails' languages.

Dec 29, 2022
Self-contained Machine Learning and Natural Language Processing library in Go
Self-contained Machine Learning and Natural Language Processing library in Go

Self-contained Machine Learning and Natural Language Processing library in Go

Jan 8, 2023