nickel 0.0.3 → 0.0.4
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.
- data/License.txt +2 -2
- data/README.rdoc +24 -12
- data/Rakefile +22 -0
- data/lib/nickel/construct.rb +121 -0
- data/lib/nickel/construct_finder.rb +1145 -0
- data/lib/nickel/construct_interpreter.rb +345 -0
- data/lib/nickel/instance_from_hash.rb +13 -0
- data/lib/nickel/nlp.rb +73 -0
- data/lib/nickel/occurrence.rb +90 -0
- data/lib/nickel/query.rb +1143 -0
- data/lib/nickel/query_constants.rb +26 -0
- data/lib/nickel/ruby_ext/calling_method.rb +10 -0
- data/lib/nickel/ruby_ext/to_s2.rb +12 -0
- data/lib/nickel/zdate.rb +503 -0
- data/lib/nickel/ztime.rb +319 -0
- data/lib/nickel.rb +30 -34
- data/nickel.gemspec +30 -10
- data/{test → spec}/nickel_spec.rb +4 -4
- data/test/compare.rb +109 -0
- data/test/nlp_test.rb +813 -0
- data/test/nlp_tests_helper.rb +55 -0
- data/test/zdate_test.rb +43 -0
- data/test/ztime_test.rb +428 -0
- metadata +34 -22
data/License.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2009
|
1
|
+
Copyright (c) 2009-2011 Lou Zell, lzell11@gmail.com, http://hazelmade.com
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
THE SOFTWARE.
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
=== Nickel
|
1
|
+
=== Nickel extracts date, time, and message information from naturally worded text.
|
2
|
+
|
2
3
|
=== Install
|
3
4
|
|
4
5
|
gem install nickel
|
@@ -6,19 +7,18 @@
|
|
6
7
|
=== Usage
|
7
8
|
|
8
9
|
==== A single occurrence
|
9
|
-
n = Nickel.
|
10
|
-
n.message
|
11
|
-
n.occurrences
|
12
|
-
n.occurrences[0].start_date # => 20090323
|
10
|
+
n = Nickel.parse("use the force on july 1st at 9am", Time.now)
|
11
|
+
n.message #=> "use the force"
|
12
|
+
n.occurrences.first.start_date #=> "20110701"
|
13
13
|
|
14
14
|
==== A daily occurrence
|
15
|
-
n = Nickel.
|
15
|
+
n = Nickel.parse "wake up everyday at 11am"
|
16
16
|
n.message # => wake up
|
17
17
|
n.occurrences[0].type # => daily
|
18
18
|
n.occurrences[0].start_time # => 11:00:00
|
19
19
|
|
20
20
|
==== A weekly occurrence
|
21
|
-
n = Nickel.
|
21
|
+
n = Nickel.parse "guitar lessons every tuesday at 5pm"
|
22
22
|
n.message # => guitar lessons
|
23
23
|
n.occurrences[0].type # => weekly
|
24
24
|
n.occurrences[0].day_of_week # => tue
|
@@ -26,7 +26,7 @@
|
|
26
26
|
n.occurrences[0].start_time # => 17:00:00
|
27
27
|
|
28
28
|
==== A day monthly occurrence
|
29
|
-
n = Nickel.
|
29
|
+
n = Nickel.parse "drink specials on the second thursday of every month"
|
30
30
|
n.message # => drink specials
|
31
31
|
n.occurrences[0].type # => daymonthly
|
32
32
|
n.occurrences[0].day_of_week # => thu
|
@@ -34,14 +34,14 @@
|
|
34
34
|
n.occurrences[0].interval # => 1
|
35
35
|
|
36
36
|
==== A date monthly occurrence
|
37
|
-
n = Nickel.
|
37
|
+
n = Nickel.parse "pay credit card every month on the 22nd"
|
38
38
|
n.message # => pay credit card
|
39
39
|
n.occurrences[0].type # => datemonthly
|
40
40
|
n.occurrences[0].date_of_month # => 22
|
41
41
|
n.occurrences[0].interval # => 1
|
42
42
|
|
43
43
|
==== Multiple occurrences
|
44
|
-
n = Nickel.
|
44
|
+
n = Nickel.parse "band meeting every monday and wednesday at 2pm"
|
45
45
|
n.message # => band meeting
|
46
46
|
n.occurrences[0].type # => weekly
|
47
47
|
n.occurrences[0].day_of_week # => mon
|
@@ -52,6 +52,18 @@
|
|
52
52
|
|
53
53
|
==== Setting current time
|
54
54
|
|
55
|
-
n = Nickel.
|
55
|
+
n = Nickel.parse "lunch 3 days from now", DateTime.new(2010,3,31)
|
56
56
|
n.message # => lunch
|
57
|
-
n.occurrences[0].start_date # => 20100403
|
57
|
+
n.occurrences[0].start_date # => 20100403
|
58
|
+
|
59
|
+
|
60
|
+
Running Tests
|
61
|
+
=======
|
62
|
+
$ rake test
|
63
|
+
|
64
|
+
If you do not have RUBYOPT set:
|
65
|
+
$ RUBYOPT='rubygems' rake test
|
66
|
+
|
67
|
+
|
68
|
+
Copyright (c) 2008-2011 Lou Zell, released under the MIT license
|
69
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the nlp plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the nlp plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Nlp'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# Ruby Nickel Library
|
2
|
+
# Copyright (c) 2008-2011 Lou Zell, lzell11@gmail.com, http://hazelmade.com
|
3
|
+
# MIT License [http://www.opensource.org/licenses/mit-license.php]
|
4
|
+
|
5
|
+
module Nickel
|
6
|
+
|
7
|
+
class Construct
|
8
|
+
include InstanceFromHash
|
9
|
+
attr_accessor :comp_start, :comp_end, :found_in
|
10
|
+
end
|
11
|
+
|
12
|
+
class DateConstruct < Construct
|
13
|
+
attr_accessor :date
|
14
|
+
def interpret
|
15
|
+
{:date => @date}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class DateSpanConstruct < Construct
|
20
|
+
attr_accessor :start_date, :end_date
|
21
|
+
end
|
22
|
+
|
23
|
+
class TimeConstruct < Construct
|
24
|
+
attr_accessor :time
|
25
|
+
def interpret
|
26
|
+
{:time => @time}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class TimeSpanConstruct < Construct
|
31
|
+
attr_accessor :start_time, :end_time
|
32
|
+
end
|
33
|
+
|
34
|
+
class WrapperConstruct < Construct
|
35
|
+
attr_accessor :wrapper_type, :wrapper_length
|
36
|
+
end
|
37
|
+
|
38
|
+
class RecurrenceConstruct < Construct
|
39
|
+
attr_accessor :repeats, :repeats_on
|
40
|
+
|
41
|
+
def interpret
|
42
|
+
if variant_of?(:daily) then interpret_daily_variant
|
43
|
+
elsif variant_of?(:weekly) then interpret_weekly_variant
|
44
|
+
elsif variant_of?(:daymonthly) then interpret_daymonthly_variant
|
45
|
+
elsif variant_of?(:datemonthly) then interpret_datemonthly_variant
|
46
|
+
else
|
47
|
+
puts @repeats.inspect
|
48
|
+
raise StandardError.new("self is an invalid variant, check value of self.repeats or @repeats")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_interval
|
53
|
+
if has_interval_of?(1) then 1
|
54
|
+
elsif has_interval_of?(2) then 2
|
55
|
+
elsif has_interval_of?(3) then 3
|
56
|
+
else
|
57
|
+
raise StandardError.new("self.repeats is invalid!!")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def has_interval_of?(x)
|
63
|
+
case x
|
64
|
+
when 1 then [:daily, :weekly, :daymonthly, :datemonthly].include?(@repeats)
|
65
|
+
when 2 then [:altdaily, :altweekly, :altdaymonthly, :altdatemonthly].include?(@repeats)
|
66
|
+
when 3 then [:threedaily, :threeweekly, :threedaymonthly, :threedatemonthly].include?(@repeats)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def variant_of?(sym)
|
71
|
+
case sym
|
72
|
+
when :daily then [:daily, :altdaily, :threedaily].include?(@repeats)
|
73
|
+
when :weekly then [:weekly, :altweekly, :threeweekly].include?(@repeats)
|
74
|
+
when :daymonthly then [:daymonthly, :altdaymonthly, :threedaymonthly].include?(@repeats)
|
75
|
+
when :datemonthly then [:datemonthly, :altdatemonthly, :threedatemonthly].include?(@repeats)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def interpret_daily_variant
|
80
|
+
hash_for_occ_base = {:type => :daily, :interval => get_interval}
|
81
|
+
[hash_for_occ_base]
|
82
|
+
end
|
83
|
+
|
84
|
+
# @repeats_on is an array of day indices. For example,
|
85
|
+
# "every monday and wed" will produce @repeats_on == [0,2].
|
86
|
+
def interpret_weekly_variant
|
87
|
+
hash_for_occ_base = {:type => :weekly, :interval => get_interval}
|
88
|
+
array_of_occurrences = []
|
89
|
+
@repeats_on.each do |day_of_week|
|
90
|
+
array_of_occurrences << hash_for_occ_base.merge({:day_of_week => day_of_week})
|
91
|
+
end
|
92
|
+
array_of_occurrences
|
93
|
+
end
|
94
|
+
|
95
|
+
# @repeats_on is an array of arrays: Each sub array has the format
|
96
|
+
# [week_of_month, day_of_week]. For example,
|
97
|
+
# "the first and second sat of every month" will produce
|
98
|
+
# @repeats_on == [[1,5], [2,5]]
|
99
|
+
def interpret_daymonthly_variant
|
100
|
+
hash_for_occ_base = {:type => :daymonthly, :interval => get_interval}
|
101
|
+
array_of_occurrences = []
|
102
|
+
@repeats_on.each do |on|
|
103
|
+
h = {:week_of_month => on[0], :day_of_week => on[1]}
|
104
|
+
array_of_occurrences << hash_for_occ_base.merge(h)
|
105
|
+
end
|
106
|
+
array_of_occurrences
|
107
|
+
end
|
108
|
+
|
109
|
+
# @repeats_on is an array of datemonthly indices. For example,
|
110
|
+
# "the 21st and 22nd of every monthy" will produce @repeats_on == [21, 22]
|
111
|
+
def interpret_datemonthly_variant
|
112
|
+
hash_for_occ_base = {:type => :datemonthly, :interval => get_interval}
|
113
|
+
array_of_occurrences = []
|
114
|
+
@repeats_on.each do |date_of_month|
|
115
|
+
h = {:date_of_month => date_of_month}
|
116
|
+
array_of_occurrences << hash_for_occ_base.merge(h)
|
117
|
+
end
|
118
|
+
array_of_occurrences
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|