natural-date 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/natural-date/language_bundle.rb +55 -0
- data/lib/natural-date/natural_date_expression.rb +92 -0
- data/lib/natural-date/natural_date_expression_factory.rb +51 -0
- data/lib/natural-date/translator.rb +2 -0
- data/lib/natural-date/translator/cleaner.rb +16 -0
- data/lib/natural-date/translator/en.yml +63 -0
- data/lib/natural-date/translator/expander.rb +38 -0
- data/lib/natural-date/translator/expression_splitter.rb +12 -0
- data/lib/natural-date/translator/minifier.rb +10 -0
- data/lib/natural-date/translator/mod_applier.rb +30 -0
- data/lib/natural-date/translator/mounter.rb +18 -0
- data/lib/natural-date/translator/normalizer.rb +19 -0
- data/lib/natural-date/translator/pt-BR.yml +63 -0
- data/lib/natural-date/translator/splitter_literal_date.rb +19 -0
- data/lib/natural-date/translator/step.rb +7 -0
- data/lib/natural-date/translator/time_mounter.rb +55 -0
- data/lib/natural-date/translator/unknown_cleaner.rb +8 -0
- data/lib/natural-date/translator/year_finder.rb +25 -0
- data/natural-date.gemspec +42 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8415cba533cee28e1d874296600c2a958fd29ca2
|
4
|
+
data.tar.gz: 66cb8ab8941a4ebd019bd4909afd52120cd1b2df
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08458ad27757787be2e80fdbd5208948c460cae271398d2d7d9c208627b4c783a1679ae1b703f6bb480dbb138288bf2ab61ca56fc7411996fad32955507a0055'
|
7
|
+
data.tar.gz: 2880ff3803eea424fb3c452df836de85fbd0591a7745ae7f3be1c96e7573ce738f4b4dd25daa4fa2c4caaeadae9c820b4b3bdf982fb468d52dc2554ff7a9db2a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at andrewaguiar6@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Andrew S Aguiar
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Natural Date - Alpha
|
2
|
+
|
3
|
+
Natural language date/time parser in Ruby. `natural-date` can convert from plain text to date expressions that can
|
4
|
+
symbolize since single dates till recurrent dates.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'natural-date'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install natural-date
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'natural-date'
|
26
|
+
|
27
|
+
# First you need to create a NaturalDateExpressionFactory with a specific locale. only pt-BR and en is supported by now =(
|
28
|
+
factory = NaturalDateExpressionFactory.new(:en)
|
29
|
+
|
30
|
+
# then create a natural date expression passing a string expression and a reference date.
|
31
|
+
# In the example below as we set the reference date to 9th of Dezember, 2016 the natural_date is
|
32
|
+
# going to match only '2017-8-1'
|
33
|
+
natural_date = factory.create('1th of august', Date.parse('2016-12-9'))
|
34
|
+
|
35
|
+
# Test against a Date
|
36
|
+
puts natural_date.match(Date.new(2016, 8, 1)) # => {:match=>false, :first_matched_expression=>nil}
|
37
|
+
puts natural_date.match(Date.new(2017, 8, 1)) # => {:match=>true, :first_matched_expression=>{:day=>[1], :month=>[8], :year=>[2017]}}
|
38
|
+
```
|
39
|
+
|
40
|
+
TODO: Write other examples
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
|
+
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/natural-date. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
51
|
+
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
56
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'natural-date/natural_date_expression_factory'
|
5
|
+
require 'natural-date/natural_date_expression'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Translator
|
2
|
+
class LanguageBundle
|
3
|
+
BUNDLES ||= {}
|
4
|
+
|
5
|
+
attr_reader :types_range, :types_map, :values, :months, :values_map, :separator_regexp
|
6
|
+
|
7
|
+
def initialize lang
|
8
|
+
@lang = lang.freeze
|
9
|
+
|
10
|
+
@types_range = { week_day: 0..6, month: 1..12 }.freeze
|
11
|
+
@types_map = load_types_map.freeze
|
12
|
+
@values = load_values.freeze
|
13
|
+
@months = load_months.freeze
|
14
|
+
@values_map = @values.map { |key, value| value.split(' ').map { |v| { v => key } } }.flatten.reduce({}) { |e, map| map.merge(e) }.freeze
|
15
|
+
@separator_regexp = Regexp.new(@types_map.select { |_k, v| v == :separator }.keys.map { |k| " #{k} " }.join '|').freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.fetch lang
|
19
|
+
BUNDLES[lang] ||= LanguageBundle.new(lang)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def yaml
|
25
|
+
@yaml ||= begin
|
26
|
+
base_path = File.dirname(File.expand_path(__FILE__))
|
27
|
+
YAML::load_file(File.join(base_path, 'translator', "#{@lang}.yml"))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_types_map
|
32
|
+
yaml['keys']
|
33
|
+
.map{ |k, v| { k => v.values.join(' ') } }
|
34
|
+
.reduce({}){ |map, element| map.merge(element) }
|
35
|
+
.map { |k, v| v.split(' ').map { |v| { v => k.to_sym } } }
|
36
|
+
.flatten
|
37
|
+
.reduce({}) { |e, map| map.merge(e) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_values
|
41
|
+
yaml['keys']
|
42
|
+
.values
|
43
|
+
.reduce({}) { |map, element| map.merge(symbolize_keys(element)) }
|
44
|
+
.reject { |k, v| k == :value }
|
45
|
+
end
|
46
|
+
|
47
|
+
def symbolize_keys m
|
48
|
+
m.map{ |k, v| {k.to_sym => v} }.reduce({}) { |map, element| map.merge(element) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_months
|
52
|
+
yaml['keys']['month'].values.map { |m| m.split.first }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
class NaturalDateExpression
|
2
|
+
|
3
|
+
VERSION = "1.0"
|
4
|
+
|
5
|
+
def initialize data, reference_date, expression_string
|
6
|
+
@data = data
|
7
|
+
@reference_date = reference_date
|
8
|
+
@expression_string = expression_string
|
9
|
+
end
|
10
|
+
|
11
|
+
def match date
|
12
|
+
matches = @data.map do |expression_map|
|
13
|
+
match_week?(date, expression_map) &&
|
14
|
+
match_month?(date, expression_map) &&
|
15
|
+
match_literal?(date, expression_map) &&
|
16
|
+
match_day?(date, expression_map) &&
|
17
|
+
match_year?(date, expression_map)
|
18
|
+
end
|
19
|
+
|
20
|
+
first_matched_expression = if matches.any?
|
21
|
+
@data[matches.each_with_index.find { |exp, index| exp }.last]
|
22
|
+
else
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
|
26
|
+
{ match: matches.any?,
|
27
|
+
first_matched_expression: first_matched_expression }
|
28
|
+
end
|
29
|
+
|
30
|
+
def recurrent?
|
31
|
+
@data.map { |expression_map| !(expression_map[:day] && expression_map[:month]) }.any?
|
32
|
+
end
|
33
|
+
|
34
|
+
def data
|
35
|
+
@data.dup
|
36
|
+
end
|
37
|
+
|
38
|
+
def fetch_dates dates_range = nil
|
39
|
+
(dates_range || (Date.today..(Date.today + 365))).to_a.select do |date|
|
40
|
+
match(date).fetch(:match)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def match_year? date, expression_map
|
47
|
+
return expression_map[:year].include?(date.year) if expression_map[:year]
|
48
|
+
return true if expression_map[:week_day] || !expression_map[:day] || !expression_map[:month]
|
49
|
+
|
50
|
+
date.year == @reference_date.year
|
51
|
+
end
|
52
|
+
|
53
|
+
def match_day? date, expression_map
|
54
|
+
!expression_map[:day] || expression_map[:day].include?(date.day)
|
55
|
+
end
|
56
|
+
|
57
|
+
def match_literal? date, expression_map
|
58
|
+
!expression_map[:literal] || expression_map[:literal].any? { |token| match_literal_token?(token, args) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def match_week? date, expression_map
|
62
|
+
!expression_map[:week_day] || expression_map[:week_day].any? { |token| match_week_token?(token, date) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def match_month? date, expression_map
|
66
|
+
!expression_map[:month] || expression_map[:month].include?(date.month)
|
67
|
+
end
|
68
|
+
|
69
|
+
def match_week_token? token, date
|
70
|
+
return match_nth_week_token?(token, date) if token.to_s.include?('.')
|
71
|
+
token.to_i == date.wday
|
72
|
+
end
|
73
|
+
|
74
|
+
def match_nth_week_token? token, date
|
75
|
+
nth, token_wday = token.split('.')
|
76
|
+
|
77
|
+
wdays = (date.beginning_of_month..date.end_of_month).to_a.select do |day|
|
78
|
+
day.wday == token_wday.to_i
|
79
|
+
end
|
80
|
+
|
81
|
+
case nth
|
82
|
+
when 'first_1'
|
83
|
+
wdays.first == date
|
84
|
+
when 'last_999'
|
85
|
+
wdays.last == date
|
86
|
+
when 'prior_last_999'
|
87
|
+
wdays[wdays.size - 2] == date
|
88
|
+
else
|
89
|
+
wdays[nth.to_i - 1] == date
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class NaturalDateExpressionFactory
|
2
|
+
def initialize lang
|
3
|
+
@lang = lang
|
4
|
+
end
|
5
|
+
|
6
|
+
STEPS = {}
|
7
|
+
EXPRESSION_SPLITTER_STEP = {}
|
8
|
+
|
9
|
+
def create expression_string, reference_date
|
10
|
+
NaturalDateExpression.new(create_data_expression(expression_string, reference_date), reference_date, expression_string)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def create_data_expression expression_string, reference_date
|
16
|
+
expression_splitter.map(expression_string.to_s).map do |expression|
|
17
|
+
steps.reduce(expression) do |tokens, step|
|
18
|
+
step.map(tokens, reference_date)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def expression_splitter
|
24
|
+
EXPRESSION_SPLITTER_STEP[@lang] ||= begin
|
25
|
+
Translator::ExpressionSplitter.new.tap do |step|
|
26
|
+
step.language_bundle = Translator::LanguageBundle.fetch(@lang)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def steps
|
32
|
+
STEPS[@lang] ||= step_classes.map do |step_class|
|
33
|
+
step_class.new.tap do |step|
|
34
|
+
step.language_bundle = Translator::LanguageBundle.fetch(@lang)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def step_classes
|
40
|
+
[ Translator::Cleaner,
|
41
|
+
Translator::SplitterLiteralDate,
|
42
|
+
Translator::Mounter,
|
43
|
+
Translator::TimeMounter,
|
44
|
+
Translator::Normalizer,
|
45
|
+
Translator::Expander,
|
46
|
+
Translator::ModApplier,
|
47
|
+
Translator::Minifier,
|
48
|
+
Translator::YearFinder,
|
49
|
+
Translator::UnknownCleaner ].freeze
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Translator
|
2
|
+
class Cleaner < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.strip
|
6
|
+
.downcase
|
7
|
+
.gsub('ç', 'c')
|
8
|
+
.gsub('ã', 'a')
|
9
|
+
.gsub('á', 'a')
|
10
|
+
.gsub('ê', 'e')
|
11
|
+
.gsub('é', 'e')
|
12
|
+
.gsub(/,|\./, ' ')
|
13
|
+
.split(/\s+/)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
keys:
|
2
|
+
separator:
|
3
|
+
value: ";"
|
4
|
+
range:
|
5
|
+
value: 'till until'
|
6
|
+
modifier:
|
7
|
+
first_1: 'first'
|
8
|
+
prior_last_999: 'penultimate'
|
9
|
+
last_999: 'last'
|
10
|
+
of_month: 'of month'
|
11
|
+
week_day:
|
12
|
+
sun_0: 'sunday sun'
|
13
|
+
mon_1: 'monday mon'
|
14
|
+
tue_2: 'tuesday tue'
|
15
|
+
wed_3: 'wednesday wed'
|
16
|
+
thu_4: 'thursday thu'
|
17
|
+
fri_5: 'friday fri'
|
18
|
+
sat_6: 'saturday sat'
|
19
|
+
day:
|
20
|
+
day_1: '1 1th'
|
21
|
+
day_2: '2 2th'
|
22
|
+
day_3: '3 3th'
|
23
|
+
day_4: '4 4th'
|
24
|
+
day_5: '5 5th'
|
25
|
+
day_6: '6 6th'
|
26
|
+
day_7: '7 7th'
|
27
|
+
day_8: '8 8th'
|
28
|
+
day_9: '9 9th'
|
29
|
+
day_10: '10 10th'
|
30
|
+
day_11: '11 11th'
|
31
|
+
day_12: '12 12th'
|
32
|
+
day_13: '13 13th'
|
33
|
+
day_14: '14 14th'
|
34
|
+
day_15: '15 15th'
|
35
|
+
day_16: '16 16th'
|
36
|
+
day_17: '17 17th'
|
37
|
+
day_18: '18 18th'
|
38
|
+
day_19: '19 19th'
|
39
|
+
day_20: '20 20th'
|
40
|
+
day_21: '21 21th'
|
41
|
+
day_22: '22 22th'
|
42
|
+
day_23: '23 23th'
|
43
|
+
day_24: '24 24th'
|
44
|
+
day_25: '25 25th'
|
45
|
+
day_26: '26 26th'
|
46
|
+
day_27: '27 27th'
|
47
|
+
day_28: '28 28th'
|
48
|
+
day_29: '29 29th'
|
49
|
+
day_30: '30 30th'
|
50
|
+
day_31: '31 31th'
|
51
|
+
month:
|
52
|
+
jan_1: 'jan janeiro'
|
53
|
+
feb_2: 'feb february'
|
54
|
+
mar_3: 'mar march'
|
55
|
+
apr_4: 'apr april'
|
56
|
+
may_5: 'may'
|
57
|
+
jun_6: 'jun june'
|
58
|
+
jul_7: 'jul july'
|
59
|
+
aug_8: 'aug august'
|
60
|
+
sep_9: 'sep september'
|
61
|
+
oct_10: 'oct october'
|
62
|
+
nov_11: 'nov november'
|
63
|
+
dez_12: 'dez dezember'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Translator
|
2
|
+
class Expander < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.each_with_index
|
6
|
+
.map { |token, index| process_tokens(tokens, token, index) }
|
7
|
+
.flatten(1)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def process_tokens tokens, token, index
|
13
|
+
token.first == :range ? create_interval(tokens, index) : [token]
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_interval tokens, index
|
17
|
+
d1_key = tokens[index - 1].first
|
18
|
+
d1_value = tokens[index - 1].last.to_s.split('_').last.to_i
|
19
|
+
|
20
|
+
d2_key = tokens[index + 1].first
|
21
|
+
d2_value = tokens[index + 1].last.to_s.split('_').last.to_i
|
22
|
+
|
23
|
+
range = if d1_value == d2_value
|
24
|
+
language_bundle.types_range[d1_key]
|
25
|
+
|
26
|
+
elsif d1_value > d2_value
|
27
|
+
upper_range = (d1_value..(language_bundle.types_range[d1_key].to_a.max)).to_a
|
28
|
+
lower_range = (language_bundle.types_range[d1_key].to_a.min..d2_value).to_a
|
29
|
+
|
30
|
+
upper_range + lower_range
|
31
|
+
else
|
32
|
+
d1_value..d2_value
|
33
|
+
end
|
34
|
+
|
35
|
+
range.to_a.map { |number| [d1_key, number] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Translator
|
2
|
+
class ModApplier < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.each_with_index
|
6
|
+
.map { |token, index| apply(tokens, token, index) }
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def apply tokens, token, index
|
12
|
+
if token.first == :modifier
|
13
|
+
case token.last
|
14
|
+
when :of_month
|
15
|
+
if tokens[index + 1] && tokens[index + 1].first == :day
|
16
|
+
# transform day to month
|
17
|
+
tokens[index + 1] = [:month, tokens[index + 1].last]
|
18
|
+
end
|
19
|
+
else
|
20
|
+
if tokens[index + 1] && tokens[index + 1].first == :week_day
|
21
|
+
next_token = tokens[index + 1]
|
22
|
+
tokens[index + 1] = [next_token.first, "#{token.last}.#{next_token.last}"]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
token
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Translator
|
2
|
+
class Mounter < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.map { |expression_token| [extract_token_type(expression_token), extract_token_value(expression_token)] }
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def extract_token_type expression_token
|
11
|
+
language_bundle.types_map.fetch(expression_token, :unknown)
|
12
|
+
end
|
13
|
+
|
14
|
+
def extract_token_value expression_token
|
15
|
+
language_bundle.values_map.fetch(expression_token, expression_token)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Translator
|
2
|
+
class Normalizer < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.map { |token| normalize(token) }
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def normalize token
|
11
|
+
case token.first
|
12
|
+
when :day then [token.first, token.last.to_s.split('_').last.to_i]
|
13
|
+
when :month then [token.first, token.last.to_s.split('_').last.to_i]
|
14
|
+
when :week_day then [token.first, token.last.to_s.split('_').last.to_i]
|
15
|
+
else token
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
keys:
|
2
|
+
separator:
|
3
|
+
value: ";"
|
4
|
+
range:
|
5
|
+
value: 'a ate'
|
6
|
+
modifier:
|
7
|
+
first_1: 'primeiro primeira'
|
8
|
+
prior_last_999: 'penultimo penultima'
|
9
|
+
last_999: 'ultimo ultima'
|
10
|
+
of_month: 'do mes'
|
11
|
+
week_day:
|
12
|
+
sun_0: 'domingo domingos dom'
|
13
|
+
mon_1: 'segunda-feira segunda segundas seg'
|
14
|
+
tue_2: 'terca-feira terca tercas ter'
|
15
|
+
wed_3: 'quarta-feira quarta quartas qua'
|
16
|
+
thu_4: 'quinta-feira quinta quintas qui'
|
17
|
+
fri_5: 'sexta-feira sexta sextas sex'
|
18
|
+
sat_6: 'sabado sabados sab'
|
19
|
+
day:
|
20
|
+
day_1: '1'
|
21
|
+
day_2: '2'
|
22
|
+
day_3: '3'
|
23
|
+
day_4: '4'
|
24
|
+
day_5: '5'
|
25
|
+
day_6: '6'
|
26
|
+
day_7: '7'
|
27
|
+
day_8: '8'
|
28
|
+
day_9: '9'
|
29
|
+
day_10: '10'
|
30
|
+
day_11: '11'
|
31
|
+
day_12: '12'
|
32
|
+
day_13: '13'
|
33
|
+
day_14: '14'
|
34
|
+
day_15: '15'
|
35
|
+
day_16: '16'
|
36
|
+
day_17: '17'
|
37
|
+
day_18: '18'
|
38
|
+
day_19: '19'
|
39
|
+
day_20: '20'
|
40
|
+
day_21: '21'
|
41
|
+
day_22: '22'
|
42
|
+
day_23: '23'
|
43
|
+
day_24: '24'
|
44
|
+
day_25: '25'
|
45
|
+
day_26: '26'
|
46
|
+
day_27: '27'
|
47
|
+
day_28: '28'
|
48
|
+
day_29: '29'
|
49
|
+
day_30: '30'
|
50
|
+
day_31: '31'
|
51
|
+
month:
|
52
|
+
jan_1: 'jan janeiro'
|
53
|
+
feb_2: 'fev fevereiro'
|
54
|
+
mar_3: 'mar marco'
|
55
|
+
apr_4: 'abr abril'
|
56
|
+
may_5: 'mai maio'
|
57
|
+
jun_6: 'jun junho'
|
58
|
+
jul_7: 'jul julho'
|
59
|
+
aug_8: 'ago agosto'
|
60
|
+
sep_9: 'set setembro'
|
61
|
+
oct_10: 'out outubro'
|
62
|
+
nov_11: 'nov novembro'
|
63
|
+
dez_12: 'dez dezembro'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Translator
|
2
|
+
class SplitterLiteralDate < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens.map do |token|
|
5
|
+
if token.include? '/'
|
6
|
+
translate_day_and_month(token.split('/'))
|
7
|
+
else
|
8
|
+
[token]
|
9
|
+
end
|
10
|
+
end.flatten
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def translate_day_and_month parts
|
16
|
+
[ parts[0].to_i.to_s, language_bundle.months[parts[1].to_i - 1] ]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Translator
|
2
|
+
class TimeMounter < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
tokens
|
5
|
+
.map { |token| normalize(token) }
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def normalize token
|
11
|
+
case token.first
|
12
|
+
when :unknown
|
13
|
+
if time? token
|
14
|
+
[:time, create_token_value(token.last.upcase)]
|
15
|
+
else
|
16
|
+
token
|
17
|
+
end
|
18
|
+
else token
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def time? token
|
23
|
+
token_key = token.last.to_s.upcase
|
24
|
+
|
25
|
+
%w(H AM PM :).any? do |s|
|
26
|
+
token.last.to_s.upcase.include? s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_token_value raw_value
|
31
|
+
if raw_value.include? ':'
|
32
|
+
hour, minute = raw_value.split(':')
|
33
|
+
|
34
|
+
"#{adjust_hour(hour, raw_value)}:#{adjust_minute(minute)}"
|
35
|
+
else
|
36
|
+
#TODO
|
37
|
+
raw_value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def adjust_minute minute
|
42
|
+
minute.to_i
|
43
|
+
end
|
44
|
+
|
45
|
+
def adjust_hour hour, raw_value
|
46
|
+
if raw_value.include? 'PM'
|
47
|
+
Time.parse("#{hour}pm").hour
|
48
|
+
elsif raw_value.include? 'AM'
|
49
|
+
Time.parse("#{hour}am").hour
|
50
|
+
else
|
51
|
+
hour.to_i
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Translator
|
2
|
+
class YearFinder < Step
|
3
|
+
def map tokens, reference_date
|
4
|
+
if (years = tokens[:unknown].to_a.select { |t| t.size == 4 && t =~ /\d\d\d\d/ }).any?
|
5
|
+
tokens[:year] = years.map &:to_i
|
6
|
+
else
|
7
|
+
if !tokens[:week] && tokens[:day] && tokens[:month]
|
8
|
+
if tokens[:day].size == 1 && tokens[:month].size == 1
|
9
|
+
tokens[:year] = [find_year(tokens[:day].first, tokens[:month].first, reference_date)]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
tokens
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_year day, month, reference_date
|
18
|
+
if Date.new(reference_date.year, month, day) < reference_date
|
19
|
+
reference_date.year + 1
|
20
|
+
else
|
21
|
+
reference_date.year
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'natural-date/translator/step'
|
6
|
+
require 'natural-date/translator/cleaner'
|
7
|
+
require 'natural-date/translator/expander'
|
8
|
+
require 'natural-date/translator/expression_splitter'
|
9
|
+
require 'natural-date/translator/minifier'
|
10
|
+
require 'natural-date/translator/mod_applier'
|
11
|
+
require 'natural-date/translator/mounter'
|
12
|
+
require 'natural-date/translator/normalizer'
|
13
|
+
require 'natural-date/translator/splitter_literal_date'
|
14
|
+
require 'natural-date/translator/time_mounter'
|
15
|
+
require 'natural-date/translator/unknown_cleaner'
|
16
|
+
require 'natural-date/translator/year_finder'
|
17
|
+
require 'natural-date/language_bundle'
|
18
|
+
require 'natural-date/natural_date_expression_factory'
|
19
|
+
require 'natural-date/natural_date_expression'
|
20
|
+
|
21
|
+
Gem::Specification.new do |spec|
|
22
|
+
spec.name = "natural-date"
|
23
|
+
spec.version = NaturalDateExpression::VERSION
|
24
|
+
spec.authors = ["Andrew S Aguiar"]
|
25
|
+
spec.email = ["andrewaguiar6@gmail.com"]
|
26
|
+
spec.summary = 'Converts natural language in date expressions'
|
27
|
+
spec.description = 'Natural language date/time parser in Ruby. natural-date can convert from plain text to date expressions symbolizing since single dates till recurrent dates'
|
28
|
+
spec.homepage = 'https://github.com/andrewaguiar/natural-date'
|
29
|
+
spec.license = "MIT"
|
30
|
+
|
31
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
32
|
+
f.match(%r{^(test|spec|features)/})
|
33
|
+
end
|
34
|
+
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: natural-date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew S Aguiar
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Natural language date/time parser in Ruby. natural-date can convert from
|
56
|
+
plain text to date expressions symbolizing since single dates till recurrent dates
|
57
|
+
email:
|
58
|
+
- andrewaguiar6@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/natural-date/language_bundle.rb
|
74
|
+
- lib/natural-date/natural_date_expression.rb
|
75
|
+
- lib/natural-date/natural_date_expression_factory.rb
|
76
|
+
- lib/natural-date/translator.rb
|
77
|
+
- lib/natural-date/translator/cleaner.rb
|
78
|
+
- lib/natural-date/translator/en.yml
|
79
|
+
- lib/natural-date/translator/expander.rb
|
80
|
+
- lib/natural-date/translator/expression_splitter.rb
|
81
|
+
- lib/natural-date/translator/minifier.rb
|
82
|
+
- lib/natural-date/translator/mod_applier.rb
|
83
|
+
- lib/natural-date/translator/mounter.rb
|
84
|
+
- lib/natural-date/translator/normalizer.rb
|
85
|
+
- lib/natural-date/translator/pt-BR.yml
|
86
|
+
- lib/natural-date/translator/splitter_literal_date.rb
|
87
|
+
- lib/natural-date/translator/step.rb
|
88
|
+
- lib/natural-date/translator/time_mounter.rb
|
89
|
+
- lib/natural-date/translator/unknown_cleaner.rb
|
90
|
+
- lib/natural-date/translator/year_finder.rb
|
91
|
+
- natural-date.gemspec
|
92
|
+
homepage: https://github.com/andrewaguiar/natural-date
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Converts natural language in date expressions
|
116
|
+
test_files: []
|