chronic 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +27 -0
- data/Manifest.txt +16 -5
- data/README.md +14 -8
- data/Rakefile +2 -8
- data/chronic.gemspec +8 -11
- data/lib/chronic.rb +21 -14
- data/lib/chronic/chronic.rb +38 -130
- data/lib/chronic/grabber.rb +11 -15
- data/lib/chronic/handlers.rb +63 -40
- data/lib/chronic/mini_date.rb +27 -0
- data/lib/chronic/numerizer.rb +120 -0
- data/lib/chronic/ordinal.rb +5 -10
- data/lib/chronic/pointer.rb +8 -10
- data/lib/chronic/repeater.rb +106 -109
- data/lib/chronic/repeaters/repeater_day.rb +43 -41
- data/lib/chronic/repeaters/repeater_day_name.rb +38 -36
- data/lib/chronic/repeaters/repeater_day_portion.rb +74 -73
- data/lib/chronic/repeaters/repeater_fortnight.rb +57 -55
- data/lib/chronic/repeaters/repeater_hour.rb +46 -44
- data/lib/chronic/repeaters/repeater_minute.rb +46 -44
- data/lib/chronic/repeaters/repeater_month.rb +52 -50
- data/lib/chronic/repeaters/repeater_month_name.rb +84 -80
- data/lib/chronic/repeaters/repeater_season.rb +97 -119
- data/lib/chronic/repeaters/repeater_season_name.rb +39 -39
- data/lib/chronic/repeaters/repeater_second.rb +32 -30
- data/lib/chronic/repeaters/repeater_time.rb +106 -101
- data/lib/chronic/repeaters/repeater_week.rb +60 -58
- data/lib/chronic/repeaters/repeater_weekday.rb +67 -58
- data/lib/chronic/repeaters/repeater_weekend.rb +54 -52
- data/lib/chronic/repeaters/repeater_year.rb +50 -48
- data/lib/chronic/scalar.rb +24 -16
- data/lib/chronic/separator.rb +15 -33
- data/lib/chronic/span.rb +31 -0
- data/lib/chronic/tag.rb +26 -0
- data/lib/chronic/time_zone.rb +7 -9
- data/lib/chronic/token.rb +35 -0
- data/test/helper.rb +5 -6
- data/test/test_Chronic.rb +5 -0
- data/test/test_Numerizer.rb +60 -39
- data/test/test_RepeaterHour.rb +4 -0
- data/test/test_parsing.rb +104 -13
- metadata +14 -20
- data/lib/chronic/numerizer/numerizer.rb +0 -97
metadata
CHANGED
@@ -1,26 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chronic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
- 0
|
10
|
-
version: 0.3.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.4.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Tom Preston-Werner
|
9
|
+
- Lee Jarvis
|
14
10
|
autorequire:
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
13
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
14
|
+
date: 2011-06-04 00:00:00 Z
|
20
15
|
dependencies: []
|
21
16
|
|
22
17
|
description: Chronic is a natural language date/time parser written in pure Ruby.
|
23
|
-
email:
|
18
|
+
email:
|
19
|
+
- tom@mojombo.com
|
20
|
+
- lee@jarvis.co
|
24
21
|
executables: []
|
25
22
|
|
26
23
|
extensions: []
|
@@ -41,7 +38,8 @@ files:
|
|
41
38
|
- lib/chronic/chronic.rb
|
42
39
|
- lib/chronic/grabber.rb
|
43
40
|
- lib/chronic/handlers.rb
|
44
|
-
- lib/chronic/
|
41
|
+
- lib/chronic/mini_date.rb
|
42
|
+
- lib/chronic/numerizer.rb
|
45
43
|
- lib/chronic/ordinal.rb
|
46
44
|
- lib/chronic/pointer.rb
|
47
45
|
- lib/chronic/repeater.rb
|
@@ -63,7 +61,10 @@ files:
|
|
63
61
|
- lib/chronic/repeaters/repeater_year.rb
|
64
62
|
- lib/chronic/scalar.rb
|
65
63
|
- lib/chronic/separator.rb
|
64
|
+
- lib/chronic/span.rb
|
65
|
+
- lib/chronic/tag.rb
|
66
66
|
- lib/chronic/time_zone.rb
|
67
|
+
- lib/chronic/token.rb
|
67
68
|
- test/helper.rb
|
68
69
|
- test/test_Chronic.rb
|
69
70
|
- test/test_DaylightSavings.rb
|
@@ -84,7 +85,6 @@ files:
|
|
84
85
|
- test/test_Time.rb
|
85
86
|
- test/test_Token.rb
|
86
87
|
- test/test_parsing.rb
|
87
|
-
has_rdoc: true
|
88
88
|
homepage: http://github.com/mojombo/chronic
|
89
89
|
licenses: []
|
90
90
|
|
@@ -98,25 +98,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
98
|
requirements:
|
99
99
|
- - ">="
|
100
100
|
- !ruby/object:Gem::Version
|
101
|
-
hash: 3
|
102
|
-
segments:
|
103
|
-
- 0
|
104
101
|
version: "0"
|
105
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
103
|
none: false
|
107
104
|
requirements:
|
108
105
|
- - ">="
|
109
106
|
- !ruby/object:Gem::Version
|
110
|
-
hash: 3
|
111
|
-
segments:
|
112
|
-
- 0
|
113
107
|
version: "0"
|
114
108
|
requirements: []
|
115
109
|
|
116
110
|
rubyforge_project: chronic
|
117
|
-
rubygems_version: 1.
|
111
|
+
rubygems_version: 1.8.2
|
118
112
|
signing_key:
|
119
|
-
specification_version:
|
113
|
+
specification_version: 3
|
120
114
|
summary: Natural language date/time parsing.
|
121
115
|
test_files:
|
122
116
|
- test/test_Chronic.rb
|
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'strscan'
|
2
|
-
|
3
|
-
class Numerizer
|
4
|
-
|
5
|
-
DIRECT_NUMS = [
|
6
|
-
['eleven', '11'],
|
7
|
-
['twelve', '12'],
|
8
|
-
['thirteen', '13'],
|
9
|
-
['fourteen', '14'],
|
10
|
-
['fifteen', '15'],
|
11
|
-
['sixteen', '16'],
|
12
|
-
['seventeen', '17'],
|
13
|
-
['eighteen', '18'],
|
14
|
-
['nineteen', '19'],
|
15
|
-
['ninteen', '19'], # Common mis-spelling
|
16
|
-
['zero', '0'],
|
17
|
-
['one', '1'],
|
18
|
-
['two', '2'],
|
19
|
-
['three', '3'],
|
20
|
-
['four(\W|$)', '4\1'], # The weird regex is so that it matches four but not fourty
|
21
|
-
['five', '5'],
|
22
|
-
['six(\W|$)', '6\1'],
|
23
|
-
['seven(\W|$)', '7\1'],
|
24
|
-
['eight(\W|$)', '8\1'],
|
25
|
-
['nine(\W|$)', '9\1'],
|
26
|
-
['ten', '10'],
|
27
|
-
['\ba[\b^$]', '1'] # doesn't make sense for an 'a' at the end to be a 1
|
28
|
-
]
|
29
|
-
|
30
|
-
TEN_PREFIXES = [ ['twenty', 20],
|
31
|
-
['thirty', 30],
|
32
|
-
['fourty', 40],
|
33
|
-
['fifty', 50],
|
34
|
-
['sixty', 60],
|
35
|
-
['seventy', 70],
|
36
|
-
['eighty', 80],
|
37
|
-
['ninety', 90]
|
38
|
-
]
|
39
|
-
|
40
|
-
BIG_PREFIXES = [ ['hundred', 100],
|
41
|
-
['thousand', 1000],
|
42
|
-
['million', 1_000_000],
|
43
|
-
['billion', 1_000_000_000],
|
44
|
-
['trillion', 1_000_000_000_000],
|
45
|
-
]
|
46
|
-
|
47
|
-
def self.numerize(string)
|
48
|
-
string = string.dup
|
49
|
-
|
50
|
-
# preprocess
|
51
|
-
string.gsub!(/ +|([^\d])-([^\d])/, '\1 \2') # will mutilate hyphenated-words but shouldn't matter for date extraction
|
52
|
-
string.gsub!(/a half/, 'haAlf') # take the 'a' out so it doesn't turn into a 1, save the half for the end
|
53
|
-
|
54
|
-
# easy/direct replacements
|
55
|
-
|
56
|
-
DIRECT_NUMS.each do |dn|
|
57
|
-
string.gsub!(/#{dn[0]}/i, '<num>' + dn[1])
|
58
|
-
end
|
59
|
-
|
60
|
-
# ten, twenty, etc.
|
61
|
-
|
62
|
-
TEN_PREFIXES.each do |tp|
|
63
|
-
string.gsub!(/(?:#{tp[0]}) *<num>(\d(?=[^\d]|$))*/i) { '<num>' + (tp[1] + $1.to_i).to_s }
|
64
|
-
end
|
65
|
-
|
66
|
-
TEN_PREFIXES.each do |tp|
|
67
|
-
string.gsub!(/#{tp[0]}/i) { '<num>' + tp[1].to_s }
|
68
|
-
end
|
69
|
-
|
70
|
-
# hundreds, thousands, millions, etc.
|
71
|
-
|
72
|
-
BIG_PREFIXES.each do |bp|
|
73
|
-
string.gsub!(/(?:<num>)?(\d*) *#{bp[0]}/i) { '<num>' + (bp[1] * $1.to_i).to_s}
|
74
|
-
andition(string)
|
75
|
-
end
|
76
|
-
|
77
|
-
# fractional addition
|
78
|
-
# I'm not combining this with the previous block as using float addition complicates the strings
|
79
|
-
# (with extraneous .0's and such )
|
80
|
-
string.gsub!(/(\d+)(?: | and |-)*haAlf/i) { ($1.to_f + 0.5).to_s }
|
81
|
-
|
82
|
-
string.gsub(/<num>/, '')
|
83
|
-
end
|
84
|
-
|
85
|
-
private
|
86
|
-
|
87
|
-
def self.andition(string)
|
88
|
-
sc = StringScanner.new(string)
|
89
|
-
while(sc.scan_until(/<num>(\d+)( | and )<num>(\d+)(?=[^\w]|$)/i))
|
90
|
-
if sc[2] =~ /and/ || sc[1].size > sc[3].size
|
91
|
-
string[(sc.pos - sc.matched_size)..(sc.pos-1)] = '<num>' + (sc[1].to_i + sc[3].to_i).to_s
|
92
|
-
sc.reset
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|