pick_ja_date 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/lib/pick_ja_date.rb +174 -0
- data/lib/pick_ja_date/version.rb +3 -0
- data/pick_ja_date.gemspec +25 -0
- data/spec/pick_ja_date_spec.rb +58 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 813d4f19172202cf554e58d0be41050dfecd293d
|
4
|
+
data.tar.gz: 04658bf1797a781376f8db976b88512e867a954e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8515a5350934c66ebebdbdcfcf43b5ab9e2f72e5bd9b5908bc82a6cb7e92accaddb07c384be31e3b1837c5ecbe7a5558378dc792caf337d79d2319e62848b0e
|
7
|
+
data.tar.gz: 5be65134be159031a3bcbb974d82bab644310c7085a9427d1eb52145f48ef08a73a4f633191289018d33fda9b970688beaa1a7b1ec8efa320f41b0b960e94574
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 hiroshi kobayashi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# PickJaDate
|
2
|
+
|
3
|
+
日本語の文字列から日付を取得する
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pick_ja_date'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pick_ja_date
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
現在時刻が '2011/10/10 10:10:00' の時に
|
22
|
+
|
23
|
+
```
|
24
|
+
time,text = PickJaDate.extract('三ヶ月後の3日の13:00に地下鉄にのる')
|
25
|
+
p time # 2011-11-03 13:00:00 +0900
|
26
|
+
p text # 地下鉄にのる
|
27
|
+
```
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/pick_ja_date.rb
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
#encoding=utf-8
|
2
|
+
require "pick_ja_date/version"
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler'
|
6
|
+
Bundler.setup
|
7
|
+
require 'active_support/time'
|
8
|
+
|
9
|
+
class PickJaDate
|
10
|
+
attr_accessor :input, :str, :time
|
11
|
+
|
12
|
+
KANJI_NUMBER = '一二三四五六七八九十'
|
13
|
+
DAYS_OF_WEEK = %w{日 月 火 水 木 金 土}
|
14
|
+
TODAY = ['今日']
|
15
|
+
TOMORROW = ['明日', 'あした']
|
16
|
+
DAT = ['明後日', 'あさって'] # day after tomorrow
|
17
|
+
|
18
|
+
def self.extract(str)
|
19
|
+
p = self.new(str)
|
20
|
+
p.parse
|
21
|
+
[p.time, p.str]
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.date(str)
|
25
|
+
p = self.new(str)
|
26
|
+
p.parse
|
27
|
+
p.time
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(input)
|
31
|
+
@input = input
|
32
|
+
@now = Time.now
|
33
|
+
@time = Time.now
|
34
|
+
@str = input
|
35
|
+
end
|
36
|
+
|
37
|
+
def parse
|
38
|
+
set_day
|
39
|
+
set_time
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_day
|
43
|
+
num2 = '([0-9]{1,2})'
|
44
|
+
|
45
|
+
match?(/再来月/){|x|
|
46
|
+
remove_time_word x[0]
|
47
|
+
@time = (@time + 2.month)
|
48
|
+
}
|
49
|
+
|
50
|
+
match?(/来月/){|x|
|
51
|
+
remove_time_word x[0]
|
52
|
+
@time = (@time + 1.month)
|
53
|
+
}
|
54
|
+
|
55
|
+
match?(%r{(([0-9]{2,4})[年/-])?#{num2}[月/-]#{num2}日?})do |x|
|
56
|
+
all, y_with_suffix, y, m, d = x
|
57
|
+
remove_time_word all
|
58
|
+
y = @now.year unless y
|
59
|
+
@time = @time.change(year: y.to_i, month: m.to_i, day: d.to_i)
|
60
|
+
@time += 1.year if @time < @now
|
61
|
+
return
|
62
|
+
end
|
63
|
+
|
64
|
+
match?(/月末/){|x|
|
65
|
+
remove_time_word x[0]
|
66
|
+
@time = @time.change(day: @now.end_of_month.day)
|
67
|
+
return
|
68
|
+
}
|
69
|
+
|
70
|
+
match?(/月初|[1一]日/){|x|
|
71
|
+
remove_time_word x[0]
|
72
|
+
@time = (@time + 1.month).change(day: 1)
|
73
|
+
return
|
74
|
+
}
|
75
|
+
|
76
|
+
match?(%r{(次の)?(#{DAYS_OF_WEEK.join("|")})曜}) do |x|
|
77
|
+
remove, dummy, dow_str = x
|
78
|
+
remove_time_word remove
|
79
|
+
@time += next_dow_days(dow_str).day
|
80
|
+
return
|
81
|
+
end
|
82
|
+
|
83
|
+
match?(/#{(TODAY + TOMORROW + DAT).join('|')}/) do |x|
|
84
|
+
case x.first
|
85
|
+
when *TODAY; add = 0
|
86
|
+
when *TOMORROW; add = 1
|
87
|
+
when *DAT; add = 2
|
88
|
+
end
|
89
|
+
remove_time_word x.first
|
90
|
+
@time += add.day
|
91
|
+
return
|
92
|
+
end
|
93
|
+
|
94
|
+
# N 日後
|
95
|
+
units = '(分|時間|日|週間?|ヶ?月)'
|
96
|
+
[
|
97
|
+
/([0-9#{KANJI_NUMBER}]{1,2})#{units}後/,
|
98
|
+
/あと([0-9]{1,2})#{units}/,
|
99
|
+
].each do |regex|
|
100
|
+
match?(regex)do |x|
|
101
|
+
str, num, unit = x
|
102
|
+
add_unit_time kanji_to_number(num), unit
|
103
|
+
remove_time_word str
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
match?(%r{#{num2}日}){|x|
|
108
|
+
remove_time_word x.first
|
109
|
+
@time = @time.change(day: x[1])
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
def set_time
|
114
|
+
num = '([0-9]{1,2})'
|
115
|
+
space = '[ ]?'
|
116
|
+
match?(/#{num}([::])#{num}/){|x| set_time_result x[0], x[1], x[2]; return}
|
117
|
+
match?(/#{num+space}時半/){|x| set_time_result x[0], x[1], 30; return}
|
118
|
+
match?(/#{num+space}時/){|x| set_time_result x[0], x[1], 0}
|
119
|
+
match?(/#{num+space}分/){|x| set_time_result x[0], @time.hour, x[1]}
|
120
|
+
end
|
121
|
+
|
122
|
+
def match?(regex)
|
123
|
+
if @str =~ regex
|
124
|
+
yield $~.to_a
|
125
|
+
else
|
126
|
+
nil
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def format(time)
|
131
|
+
time.strftime '%Y/%m/%d %H:%M'
|
132
|
+
end
|
133
|
+
|
134
|
+
def set_time_result(remove, hour, min)
|
135
|
+
remove_time_word remove
|
136
|
+
hour = hour.to_i
|
137
|
+
if hour > 24
|
138
|
+
@time += (hour.div(24)).day
|
139
|
+
hour = hour % 24
|
140
|
+
end
|
141
|
+
@time = @time.change(hour: hour, min: min)
|
142
|
+
end
|
143
|
+
|
144
|
+
def remove_time_word(key)
|
145
|
+
@str.sub!(%r{#{key}[のにで]?}, '')
|
146
|
+
@str.sub!(%r{^[ ]|[ ]$}, '')
|
147
|
+
end
|
148
|
+
|
149
|
+
def next_dow_days(str)
|
150
|
+
now = @now.wday
|
151
|
+
target = DAYS_OF_WEEK.index(str).to_i
|
152
|
+
target += 7 if target <= now
|
153
|
+
(target - now)
|
154
|
+
end
|
155
|
+
|
156
|
+
def add_unit_time(num, unit)
|
157
|
+
num = num.to_i
|
158
|
+
case unit
|
159
|
+
when '分'; @time += num.minute
|
160
|
+
when '時間'; @time += num.hour
|
161
|
+
when '日'; @time += num.day
|
162
|
+
when '週間', '週'; @time += num.week
|
163
|
+
when 'ヶ月', '月'; @time += num.month
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def kanji_to_number(kanji)
|
168
|
+
if i = KANJI_NUMBER.chars.index(kanji)
|
169
|
+
i + 1
|
170
|
+
else
|
171
|
+
kanji
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pick_ja_date/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pick_ja_date"
|
8
|
+
spec.version = PickJaDate::VERSION
|
9
|
+
spec.authors = ["hiroshi kobayashi"]
|
10
|
+
spec.email = ["koba.shipbuilding@gmail.com"]
|
11
|
+
spec.description = %q{日本語のテキストから日付を取得する}
|
12
|
+
spec.summary = %q{日本語のテキストから日付を取得する}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "active_support"
|
24
|
+
spec.add_development_dependency "i18n"
|
25
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#encoding=utf-8
|
2
|
+
require './lib/pick_ja_date.rb'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
describe PickJaDate do
|
6
|
+
def format_time(time)
|
7
|
+
time.strftime '%Y/%m/%d %H:%M'
|
8
|
+
end
|
9
|
+
|
10
|
+
before do
|
11
|
+
Time.stub(:now).and_return(Time.parse('2011/10/10 10:10:00')) # monday
|
12
|
+
@now = Time.now
|
13
|
+
end
|
14
|
+
|
15
|
+
yml = YAML.load <<-YML
|
16
|
+
土曜の15 時半に地下鉄にのる: 2011/10/15 15:30
|
17
|
+
土曜 15時半 地下鉄にのる: 2011/10/15 15:30
|
18
|
+
地下鉄にのる土曜の15時半に: 2011/10/15 15:30
|
19
|
+
次の土曜の15時半に地下鉄にのる: 2011/10/15 15:30
|
20
|
+
|
21
|
+
明日地下鉄にのる: 2011/10/11 10:10
|
22
|
+
あしたの15:00に地下鉄にのる: 2011/10/11 15:00
|
23
|
+
明後日の15:00に地下鉄にのる: 2011/10/12 15:00
|
24
|
+
|
25
|
+
3時間後に地下鉄にのる: 2011/10/10 13:10
|
26
|
+
3日後に地下鉄にのる: 2011/10/13 10:10
|
27
|
+
3週間後に地下鉄にのる: 2011/10/31 10:10
|
28
|
+
1月後に地下鉄にのる: 2011/11/10 10:10
|
29
|
+
1ヶ月後に地下鉄にのる: 2011/11/10 10:10
|
30
|
+
|
31
|
+
12/10 10:00に地下鉄にのる: 2011/12/10 10:00
|
32
|
+
2011/12/10 10:00に地下鉄にのる: 2011/12/10 10:00
|
33
|
+
2012/12/10 10:00に地下鉄にのる: 2012/12/10 10:00
|
34
|
+
9/10 18:00に地下鉄にのる: 2012/09/10 18:00
|
35
|
+
|
36
|
+
3日後の1時に地下鉄にのる: 2011/10/13 01:00
|
37
|
+
3日後の25時に地下鉄にのる: 2011/10/14 01:00
|
38
|
+
あと30分で地下鉄にのる: 2011/10/10 10:40
|
39
|
+
|
40
|
+
月末の13:00に地下鉄にのる: 2011/10/31 13:00
|
41
|
+
1日の13:00に地下鉄にのる: 2011/11/01 13:00
|
42
|
+
月初の13:00に地下鉄にのる: 2011/11/01 13:00
|
43
|
+
来月の3日の13:00に地下鉄にのる: 2011/11/03 13:00
|
44
|
+
再来月の3日の13:00に地下鉄にのる: 2011/12/03 13:00
|
45
|
+
三ヶ月後の3日の13:00に地下鉄にのる: 2012/01/03 13:00
|
46
|
+
YML
|
47
|
+
|
48
|
+
describe "日付を解析できる" do
|
49
|
+
yml.each do|text,time|
|
50
|
+
it "#{text}" do
|
51
|
+
r_time,r_text = PickJaDate.extract(text.dup)
|
52
|
+
expect(format_time(r_time)).to eq(time)
|
53
|
+
expect(r_text).to eq('地下鉄にのる')
|
54
|
+
expect(format_time(PickJaDate.date(text.dup))).to eq(time)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pick_ja_date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hiroshi kobayashi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: active_support
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: i18n
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 日本語のテキストから日付を取得する
|
70
|
+
email:
|
71
|
+
- koba.shipbuilding@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .ruby-version
|
79
|
+
- Gemfile
|
80
|
+
- Guardfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/pick_ja_date.rb
|
85
|
+
- lib/pick_ja_date/version.rb
|
86
|
+
- pick_ja_date.gemspec
|
87
|
+
- spec/pick_ja_date_spec.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.0.3
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: 日本語のテキストから日付を取得する
|
112
|
+
test_files:
|
113
|
+
- spec/pick_ja_date_spec.rb
|