datte 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47ea673d705622d1bdc0a948f5176642ced4efb1
4
- data.tar.gz: 23d0c88f4952ab62f3e6930c923f38bbde6b7d5b
3
+ metadata.gz: bc322b1b841443f6126d408c18bf35663c5f0553
4
+ data.tar.gz: e94da6f51a9d11c12d7f4a6cd4816fe37cb055b9
5
5
  SHA512:
6
- metadata.gz: 67e32414d8019a6ce6dad8b9c20f73069b12a3567d7485d3a2fb1234ad64077a0149a7ac3d93df920981f52a1cf9130155602516ea155e9b86198dc9977b5dd1
7
- data.tar.gz: 4f4020ee371670fb1671f1795f62182c52062fc74cbbfd733ab61f2b1c119c52883b1f445e3a58146b236f2fef1f4f55c2b1a12303cef3f691adc00e0c8a804d
6
+ metadata.gz: cae6a971d24209a0ee4a0f272d75fe209159e03f7e722e2d5360a3c286520df1fe7dca21e232afe08ec5a80ff3df8c26c4a758dd9b8aaa235398467660d2fdfc
7
+ data.tar.gz: 80f80edb5e229985829ba43e7d09bf44ad99c86d688ac84cdc035c8843d845ff447c06ac76264a619427704484136b594e82e312cc52c397065779249c5ef52e
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Datte
1
+ ![Datte](https://raw.githubusercontent.com/hyde2able/datte/master/datte.png)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/datte`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A Fast and Easy DateTime Extractor in Japanese build with love by pokohide & sasaken.
6
4
 
7
5
  ## Installation
8
6
 
@@ -14,7 +12,9 @@ gem 'datte'
14
12
 
15
13
  And then execute:
16
14
 
17
- $ bundle
15
+ ```
16
+ $ bundle
17
+ ```
18
18
 
19
19
  Or install it yourself as:
20
20
 
@@ -22,27 +22,33 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
26
-
27
-
28
-
29
-
30
- datte = Datte::Parser.new()
31
-
32
- datte.parse_date(body) do |date|
33
- # dateには抽出した日付
34
- # 日付が抽出出来た場合のみ実行される
25
+ ```
26
+ datte = Datte::Parser.new
27
+ body = '明日の10時にxxに集合でよろしくー!'
28
+ datte.parse_date(body) do |datetime|
29
+ # if body contains datetime, this block is excuted.
30
+ p datetime
31
+ # <DateTime: 2017-01-22T10:00:00+00:00 ((2457776j,36000s,0n),+0s,2299161j)>
35
32
  end
33
+ ```
36
34
 
37
- date = datte.parse_date(body)
35
+ ```
36
+ datte = Datte::Parser.new
37
+ body = '3/2まで回答お願いしますー!'
38
+ datetime = datte.parse_date(body)
38
39
  if datte.nil?
39
- # 抽出できなかった場合
40
+ puts "body doesn't contain datetime"
40
41
  else
41
- # 抽出できた場合
42
+ p datetime
43
+ # <DateTime: 2017-03-02T18:00:00+00:00 ((2457815j,64800s,0n),+0s,2299161j)>
42
44
  end
45
+ ```
43
46
 
47
+ ## Test
44
48
 
45
-
49
+ ```
50
+ rspec spec/*
51
+ ```
46
52
 
47
53
  ## Development
48
54
 
data/datte.png ADDED
Binary file
@@ -37,6 +37,20 @@ module Datte
37
37
  end
38
38
  end
39
39
 
40
+ WEEKS.each do |matcher_s, week|
41
+ matcher = Regexp.new(matcher_s.to_s)
42
+ if @body.match(matcher)
43
+ WDAYS.each do |wday_matcher_s, wday|
44
+ wday_matcher = Regexp.new(wday_matcher_s.to_s)
45
+ if @body.match(wday_matcher)
46
+ now_wday = DateTime.now.wday
47
+ day = 7 * (week || 1) + wday - now_wday
48
+ @date.after({day: day})
49
+ end
50
+ end
51
+ end
52
+ end
53
+
40
54
  return @date.to_datetime
41
55
  end
42
56
 
@@ -57,6 +57,23 @@ module Datte
57
57
  '(?<min>\d{1,2})分後'
58
58
  ].map { |pattern| Regexp.compile(pattern) }.freeze
59
59
 
60
+ WEEKS = {
61
+ '来週|らいしゅう': 1,
62
+ '再来週|さらいしゅう': 2,
63
+ '次の': 1,
64
+ '翌週': 1
65
+ }
66
+
67
+ WDAYS = {
68
+ '日曜日|にちようび': 0,
69
+ '月曜日|げつようび': 1,
70
+ '火曜日|かようび': 2,
71
+ '水曜日|すいようび': 3,
72
+ '木曜日|もくようび': 4,
73
+ '金曜日|きんようび': 5,
74
+ '土曜日|どようび': 6
75
+ }
76
+
60
77
  class DatetimeTable
61
78
  def initialize
62
79
  end
@@ -39,13 +39,17 @@ module Datte
39
39
  @year, @month, @day = now[:year] + md[:year].to_i, now[:month], now[:day]
40
40
  end
41
41
  if md.matched?(:month)
42
- @year, @month, @day = now[:year], now[:month] + md[:month].to_i, now[:day]
42
+ carry = (now[:month] + md[:month].to_i) / 12
43
+ @year, @month, @day = now[:year] + carry, now[:month] + md[:month].to_i , now[:day]
43
44
  end
44
45
  if md.matched?(:day)
45
- @year, @month, @day = now[:year], now[:month], now[:day] + md[:day].to_i
46
+ days = [31,28,31,30,31,30,31,31,30,31,30,31][now[:month] - 1]
47
+ carry = (now[:day] + md[:day].to_i) / days
48
+ @year, @month, @day = now[:year], now[:month] + carry, now[:day] + md[:day].to_i - carry * days
46
49
  end
47
50
  if md.matched?(:hour)
48
- @hour, @min = now[:hour] + md[:hour].to_i, now[:min]
51
+ carry = (now[:hour] + md[:hour].to_i) / 24
52
+ @day, @hour = now[:day] + carry, now[:hour] + md[:hour].to_i - carry * 24, now[:min]
49
53
  end
50
54
  # @date >> (md[:year].to_i * 12) if md.matched?(:year) # 何年後
51
55
  # @date >> md[:month].to_i if md.matched?(:month) # 何ヶ月後
@@ -63,8 +67,8 @@ module Datte
63
67
  def mi; @min || 0 end
64
68
 
65
69
  def now
66
- d = DateTime.now
67
- { year: d.year, month: d.month, day: d.day, hour: d.hour, min: d.min }
70
+ @d ||= DateTime.now
71
+ { year: @d.year, month: @d.month, day: @d.day, hour: @d.hour, min: @d.min }
68
72
  end
69
73
 
70
74
  def year!(md)
@@ -14,7 +14,6 @@ module Datte
14
14
  num = kan.scan(/([^千百十]*)([千百十]?)/).inject(-1) do |num, (_1_9, unit)|
15
15
  num + KANNUM_1_9[_1_9] * KANNUM_10[unit]
16
16
  end
17
- p kan, num
18
17
  body.gsub!(kan, num.to_s)
19
18
  end
20
19
  body
data/lib/datte/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Datte
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
data/spec/datte_spec.rb CHANGED
@@ -1,17 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Datte do
4
+
4
5
  it "has a version number" do
5
6
  expect(Datte::VERSION).not_to be nil
6
7
  end
7
-
8
- describe "::Parser" do
9
- datte = Datte::Parser.new
10
-
11
- it "2016/11/1日に遊ぼー" do
12
- body = "2016/11/1日に遊ぼー"
13
- d = DateTime.now
14
- expect(datte.parse_date(body)).to eq(DateTime.new(2016, 11, 1, d.hour, 0, 0))
15
- end
16
- end
17
8
  end
data/spec/parser.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Datte::Parser do
4
+ parser = Datte::Parser.new
5
+ d = DateTime.now
6
+
7
+ it "日時情報を含まなければnilを返す" do
8
+ body = "ChatBotHack楽しい"
9
+ expect(parser.parse_date(body)).to eq(nil)
10
+
11
+ body = "お腹すいたなぁ"
12
+ expect(parser.parse_date(body)).to eq(nil)
13
+ end
14
+
15
+
16
+ it "y/m/d 形式での認識" do
17
+ body = "2016/11/1日に遊ぼー"
18
+ expect(parser.parse_date(body)).to eq(DateTime.new(2016, 11, 1, d.hour, 0, 0))
19
+
20
+ body = "8/10日に飲みに行かない?"
21
+ expect(parser.parse_date(body)).to eq(DateTime.new(d.year, 8, 10, d.hour, 0, 0))
22
+ end
23
+
24
+ it "何時何分のような時間の認識" do
25
+ body = "明日の朝8時集合ね!"
26
+ expect(parser.parse_date(body).day).to eq((d+1).day)
27
+ expect(parser.parse_date(body).hour).to eq(8)
28
+
29
+ body = "20日の16時集合ね!"
30
+ expect(parser.parse_date(body).day).to eq(20)
31
+ expect(parser.parse_date(body).hour).to eq(16)
32
+ end
33
+
34
+ it "明日や明後日といった相対的な文字の認識" do
35
+ body = "明日テストだ。。。"
36
+ expect(parser.parse_date(body).day).to eq((d+1).day)
37
+
38
+ body = "でも明後日はお肉食べに行く"
39
+ expect(parser.parse_date(body).day).to eq((d+2).day)
40
+ end
41
+
42
+ # TODO この部分のテスト通らないので本体を修正
43
+ it "3日後のような相対的な数値" do
44
+ body = "もう3日後にはセンター試験"
45
+ expect(parser.parse_date(body).day).to eq((d+3).day)
46
+
47
+ body = "今日の一時間後に卓球しない?"
48
+ expect(parser.parse_date(body).hour).to eq((d+Rational(1, 24)).hour)
49
+ end
50
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datte::TextConverter do
4
+ tc = Datte::TextConverter.new()
5
+
6
+ it '漢数字を数字に変換' do
7
+ expect(tc.kan_num('一月二日')).to eq '1月2日'
8
+ expect(tc.kan_num('十二月二十日に遊びに行こうね!')).to eq '12月20日に遊びに行こうね!'
9
+ expect(tc.kan_num('今日の一時間後くらいに飲みに行かない?')).to eq '今日の1時間後くらいに飲みに行かない?'
10
+ end
11
+
12
+ it '漢数字がなければ何もしない' do
13
+ expect(tc.kan_num('おなかすいた')).to eq 'おなかすいた'
14
+ expect(tc.kan_num('今日は夕ご飯食べるの?')).to eq '今日は夕ご飯食べるの?'
15
+ expect(tc.kan_num('就活で消耗してる。。。')).to eq '就活で消耗してる。。。'
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pokohide
@@ -72,6 +72,7 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - datte.gemspec
75
+ - datte.png
75
76
  - lib/datte.rb
76
77
  - lib/datte/data.txt
77
78
  - lib/datte/date_parser.rb
@@ -82,7 +83,9 @@ files:
82
83
  - lib/datte/train.rb
83
84
  - lib/datte/version.rb
84
85
  - spec/datte_spec.rb
86
+ - spec/parser.rb
85
87
  - spec/spec_helper.rb
88
+ - spec/text_converter.rb
86
89
  homepage: https://github.com/hyde2able/datte.git
87
90
  licenses:
88
91
  - MIT
@@ -109,4 +112,6 @@ specification_version: 4
109
112
  summary: A convenient way to parse string in ruby
110
113
  test_files:
111
114
  - spec/datte_spec.rb
115
+ - spec/parser.rb
112
116
  - spec/spec_helper.rb
117
+ - spec/text_converter.rb