time_range_extractor 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/time_range_extractor/match_result.rb +42 -40
- data/lib/time_range_extractor/parser.rb +48 -46
- data/lib/time_range_extractor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1191ee60eb66950fd5e8dceb2276c2938a78f9ee2bfa70b013a3e67a0e0a085a
|
4
|
+
data.tar.gz: 9dce17aa803cece9005de6cb45eb59ed5ec77a66936a0e4eb0aa6a9e566dc7f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cbd6549f7662daa73134c48862d30711416aaa2ef28d0a8e312b608b9e05f5f689bc8fed69aabdc10c34d05a7f120149e9d4e7289976bcf3aca990fff00ef90
|
7
|
+
data.tar.gz: 1b9f708e8551d23950d6f00e3f67525f9e5566aadad7e11861b33fbb57037b1141eb973ab1d1856512a763fe77c65c189c3efb5fb6f624dc14b26e762775e601
|
@@ -1,57 +1,59 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module TimeRangeExtractor
|
4
|
+
class MatchResult
|
5
|
+
def initialize(match_data)
|
6
|
+
@match_data = match_data
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def valid?
|
10
|
+
@match_data && (start_time.present? && end_time.present?)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def start_time
|
14
|
+
match_data[:start_time]
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def start_period
|
18
|
+
@start_period ||= match_data[:start_period].presence || begin
|
19
|
+
force_start_period_to_am? ? 'am' : end_period
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def end_time
|
24
|
+
match_data[:end_time]
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def end_period
|
28
|
+
match_data[:end_period]
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def time_zone
|
32
|
+
match_data[:time_zone]
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def start_time_string
|
36
|
+
[start_time, start_period, time_zone].compact.join(' ')
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def end_time_string
|
40
|
+
[end_time, end_period, time_zone].compact.join(' ')
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
+
private
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
def range?
|
46
|
+
start_time.present? && end_time.present?
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
def force_start_period_to_am?
|
50
|
+
start_t = start_time.to_i
|
51
|
+
end_t = end_time.to_i
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
end_period.casecmp('pm') == 0 &&
|
54
|
+
(start_t > end_t || (end_t == 12 && start_t < end_t))
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
+
attr_reader :match_data
|
58
|
+
end
|
57
59
|
end
|
@@ -2,59 +2,61 @@
|
|
2
2
|
|
3
3
|
# Parse out time values from a string of text. Uses the provided date as the
|
4
4
|
# basis for the DateTime generation.
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
(
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
5
|
+
module TimeRangeExtractor
|
6
|
+
class Parser
|
7
|
+
PATTERN = /
|
8
|
+
(\A|\s|\() # space or round bracket, to support: "Call Jim (8-9pm)"
|
9
|
+
(
|
10
|
+
(?<start_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)\s?
|
11
|
+
(?<start_period>am|pm)?\s?
|
12
|
+
(to|-|until|\s)\s?
|
13
|
+
)
|
14
|
+
(?<end_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)?\s?
|
15
|
+
(?<end_period>am|pm)\s?
|
16
|
+
(?<time_zone>(
|
17
|
+
[ABCDEFGHIJKLMNOPRSTUVWY]
|
18
|
+
[A-Z]
|
19
|
+
[ACDEGHKLMNORSTUVW]?
|
20
|
+
[CDNSTW]?
|
21
|
+
[T]?
|
22
|
+
))?\b
|
23
|
+
/xi.freeze
|
24
|
+
|
25
|
+
def initialize(text, date: Date.current)
|
26
|
+
@text = text
|
27
|
+
@date = date
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def call
|
31
|
+
match = PATTERN.match(@text)
|
32
|
+
result = MatchResult.new(match)
|
33
|
+
return nil unless result.valid?
|
33
34
|
|
34
|
-
|
35
|
-
|
35
|
+
time_range_from(result)
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
+
private
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def time_range_from(match_result)
|
41
|
+
start_time = time_from_string(match_result.start_time_string)
|
42
|
+
end_time = time_from_string(match_result.end_time_string)
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
if start_time <= end_time
|
45
|
+
start_time..end_time
|
46
|
+
elsif start_time > end_time
|
47
|
+
start_time..(end_time + 1.day)
|
48
|
+
end
|
49
|
+
rescue ArgumentError
|
50
|
+
nil
|
47
51
|
end
|
48
|
-
rescue ArgumentError
|
49
|
-
nil
|
50
|
-
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def time_from_string(string)
|
54
|
+
time_parser.parse(string, @date.to_time)
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
# :reek:UtilityFunction so that we can optionally include ActiveSupport
|
58
|
+
def time_parser
|
59
|
+
::Time.zone || ::Time
|
60
|
+
end
|
59
61
|
end
|
60
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_range_extractor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Baker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|