time_range_extractor 0.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 +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/time_range_extractor.rb +15 -0
- data/lib/time_range_extractor/match_result.rb +56 -0
- data/lib/time_range_extractor/parser.rb +33 -0
- data/lib/time_range_extractor/version.rb +3 -0
- data/time_range_extractor.gemspec +33 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e17089d12415100a06670d2ba9ceb8b47427a5e62130dc5eb10731a2848ffa8
|
4
|
+
data.tar.gz: b042513dea3c4f1e90b160455fb3952e716815d473aa39e40fabb6ed749d95b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 81a5b69e52bd8ee93dd5f5e16e5646bb5b209cf1da30b88723e9b0bdf41ed56d28b63f5e77535bfe35f44543563041ac161a42e4ae9db588c5a99eceae4e8af1
|
7
|
+
data.tar.gz: 71bae72527430763653b3f8d6f32b3caea965bb403db4c574c75b2c4af07506678e11138da4106e0c86c4b4dd99d127afc06ecab372b1589583f3256780ebab8
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
time_range_extractor (0.1.0)
|
5
|
+
activesupport (> 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.3)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
concurrent-ruby (1.1.5)
|
16
|
+
i18n (1.6.0)
|
17
|
+
concurrent-ruby (~> 1.0)
|
18
|
+
minitest (5.11.3)
|
19
|
+
rake (10.5.0)
|
20
|
+
thread_safe (0.3.6)
|
21
|
+
tzinfo (1.2.5)
|
22
|
+
thread_safe (~> 0.1)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 2.0)
|
29
|
+
minitest (~> 5.0)
|
30
|
+
rake (~> 10.0)
|
31
|
+
time_range_extractor!
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Jeremy Baker
|
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,76 @@
|
|
1
|
+
# Time Range Extractor
|
2
|
+
|
3
|
+
This gem makes it easy to extract a time range from some text. The goal is to be able to pull out start and end times from a body of text so that you can give more context to the information.
|
4
|
+
|
5
|
+
For example:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
result = TimeRangeExtractor.call("Meet with Jessie from 4-5pm")
|
9
|
+
result.begin
|
10
|
+
#=> 2019-06-20 16:00:00 -0700
|
11
|
+
result.end
|
12
|
+
#=> 2019-06-20 17:00:00 -0700
|
13
|
+
```
|
14
|
+
|
15
|
+
A few caveats:
|
16
|
+
|
17
|
+
- Only reads out the first time it can find
|
18
|
+
- If only one time is found, that becomes the start time
|
19
|
+
- Only the last time zone is taken into account
|
20
|
+
- Doesn't support 24 hour clocks yet
|
21
|
+
- Likely no support for international times
|
22
|
+
- Check the test suite for supported cases
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'time_range_extractor'
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install time_range_extractor
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
result = TimeRangeExtractor.call("Meet with Jessie from 4-5pm")
|
44
|
+
result.begin
|
45
|
+
#=> 2019-06-24 16:00:00 -0700
|
46
|
+
result.end
|
47
|
+
#=> 2019-06-24 17:00:00 -0700
|
48
|
+
|
49
|
+
result = TimeRangeExtractor.call("Meet with Jessie at 4pm EDT", date: 4.days.ago.to_date)
|
50
|
+
result.begin
|
51
|
+
#=> 2019-06-20 16:00:00 -0400
|
52
|
+
result.end
|
53
|
+
#=> 2019-06-20 16:00:00 -0400
|
54
|
+
|
55
|
+
Time.zone = 'America/Vancouver'
|
56
|
+
|
57
|
+
result = TimeRangeExtractor.call("Meet with Jessie at 4pm EDT", date: 4.days.ago.to_date)
|
58
|
+
result.begin
|
59
|
+
#=> Thu, 20 Jun 2019 13:00:00 PDT -07:00
|
60
|
+
result.end
|
61
|
+
#=> Thu, 20 Jun 2019 13:00:00 PDT -07:00
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
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).
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/retailzipline/time_range_extractor.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "time_range_extractor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "time"
|
2
|
+
require "date"
|
3
|
+
require "active_support/time"
|
4
|
+
|
5
|
+
require "time_range_extractor/match_result"
|
6
|
+
require "time_range_extractor/parser"
|
7
|
+
require "time_range_extractor/version"
|
8
|
+
|
9
|
+
module TimeRangeExtractor
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
def self.call(text, date: Date.current)
|
13
|
+
Parser.new(text, date: date).call
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class MatchResult
|
2
|
+
def initialize(match_data)
|
3
|
+
@match_data = match_data
|
4
|
+
end
|
5
|
+
|
6
|
+
def start_time
|
7
|
+
match_data[:start_time]
|
8
|
+
end
|
9
|
+
|
10
|
+
def start_period
|
11
|
+
@start_period ||= match_data[:start_period].presence || begin
|
12
|
+
start_t = start_time.to_i
|
13
|
+
end_t = end_time.to_i
|
14
|
+
|
15
|
+
if end_period.casecmp("pm") == 0 && (start_t > end_t || (end_t == 12 && start_t < end_t))
|
16
|
+
"am"
|
17
|
+
else
|
18
|
+
"pm"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def end_time
|
24
|
+
match_data[:end_time]
|
25
|
+
end
|
26
|
+
|
27
|
+
def end_period
|
28
|
+
match_data[:end_period]
|
29
|
+
end
|
30
|
+
|
31
|
+
def time_zone
|
32
|
+
match_data[:time_zone]
|
33
|
+
end
|
34
|
+
|
35
|
+
def start_time_string
|
36
|
+
if range?
|
37
|
+
[start_time, start_period, time_zone].compact.join(' ')
|
38
|
+
else
|
39
|
+
end_time && [end_time, end_period, time_zone].compact.join(' ')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def end_time_string
|
44
|
+
return start_time_string unless range?
|
45
|
+
|
46
|
+
[end_time, end_period, time_zone].compact.join(' ')
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def range?
|
52
|
+
start_time.present? && end_time.present?
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :match_data
|
56
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Parser
|
2
|
+
PATTERN = Regexp.new(
|
3
|
+
'(?i)((?<start_time>[0-9]{1,2}:?[0-9]{0,2}?)\s?(?<start_period>am|pm)?\s?(-|until)\s?)?(?<end_time>[0-9]{1,2}:?[0-9]{0,2})?\s?(?<end_period>am|pm)\s?(?<time_zone>[a-z][sd]t)?\b',
|
4
|
+
Regexp::IGNORECASE
|
5
|
+
).freeze
|
6
|
+
|
7
|
+
def initialize(text, date: Date.current)
|
8
|
+
@text = text
|
9
|
+
@date = date
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
result = PATTERN.match(@text)
|
14
|
+
return nil unless result
|
15
|
+
|
16
|
+
match_result = MatchResult.new(result)
|
17
|
+
|
18
|
+
start_time = time_from_string(match_result.start_time_string)
|
19
|
+
end_time = time_from_string(match_result.end_time_string)
|
20
|
+
|
21
|
+
start_time..end_time
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def time_from_string(string)
|
27
|
+
time_parser.parse("#{@date.to_s(:db)} #{string}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def time_parser
|
31
|
+
::Time.zone ? ::Time.zone : ::Time
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "time_range_extractor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "time_range_extractor"
|
7
|
+
spec.version = TimeRangeExtractor::VERSION
|
8
|
+
spec.authors = ["Jeremy Baker"]
|
9
|
+
spec.email = ["jhubert@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Extract a time range from a block of text.}
|
12
|
+
spec.homepage = "https://github.com/retailzipline/time_range_extractor"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/retailzipline/time_range_extractor"
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/retailzipline/time_range_extractor/CHANGELOG"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "activesupport", "> 2.0"
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_range_extractor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Baker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jhubert@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/time_range_extractor.rb
|
86
|
+
- lib/time_range_extractor/match_result.rb
|
87
|
+
- lib/time_range_extractor/parser.rb
|
88
|
+
- lib/time_range_extractor/version.rb
|
89
|
+
- time_range_extractor.gemspec
|
90
|
+
homepage: https://github.com/retailzipline/time_range_extractor
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
homepage_uri: https://github.com/retailzipline/time_range_extractor
|
95
|
+
source_code_uri: https://github.com/retailzipline/time_range_extractor
|
96
|
+
changelog_uri: https://github.com/retailzipline/time_range_extractor/CHANGELOG
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.7.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Extract a time range from a block of text.
|
117
|
+
test_files: []
|