ikku 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4916cf8fe3220837e015717ad3360e206115a738
4
- data.tar.gz: ef486b20ffa6bd0f791702fa57b1ebe00a1f31e7
3
+ metadata.gz: 53a3541a871522f9f4f7fb8a7c93a7f8facd4ba2
4
+ data.tar.gz: ce083237fba85ad5a1c020ab2daaff7cc9553c0b
5
5
  SHA512:
6
- metadata.gz: 4ce3e1436b314357b8290c5cd8f147b209029ea77d9fcf4166e14e3f0f47abfb82f5592bc0478a01c122b669a18050822ba68ba8dffeb11a08737d594a3a063f
7
- data.tar.gz: c27ce9f7d9d00005b2b16d9ccb48af8d4ee2ca18f48b30606aebd1346d37b1f514566a142e5d5a922248ff5ab0cbd869718d5ed87fe8d0ecea5e3a13c482ffc0
6
+ metadata.gz: 83037032d994c2edc903ca02842ac25a862e3be36cbaba41a910423aacaf67647d8e77ae474a6585bd2b0c4154b79f41580ffb5c55844573d437c4ff96a9956a
7
+ data.tar.gz: 74b6b805415141237b872f9a40f04c1b48a96afde381643942f49486e673135d84a5b69668c91178951cc5af4bf6cf95930036bfe4c7ad7d187078f9be287b59
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.0.2
2
+ - Add rule option
3
+
4
+ ## 0.0.1
5
+ - 1st Release
data/README.md CHANGED
@@ -6,20 +6,34 @@ Discover haiku from text.
6
6
  - MeCab with IPADIC (e.g. `brew install mecab mecab-ipadic`)
7
7
 
8
8
  ## Example
9
+ ### Ikku::Reviewer
10
+ Ikku::Reviewer class is the main interface for this library.
11
+
9
12
  ```rb
10
- # Ikku::Reviewer class is the main interface for this library.
11
13
  require "ikku"
12
14
  reviewer = Ikku::Reviewer.new
15
+ ```
13
16
 
14
- # Judge if given text is haiku or not.
15
- reviewer.judge("古池や蛙飛び込む水の音") #=> true
17
+ ### Ikku::Reviewer#judge
18
+ Judge if given text is haiku or not.
19
+
20
+ ```rb
21
+ reviewer.judge("古池や蛙飛び込む水の音") #=> true
16
22
  reviewer.judge("ああ古池や蛙飛び込む水の音ああ") #=> false
23
+ ```
24
+
25
+ ### Ikku::Reviewer#find
26
+ Find one available haiku from given text.
17
27
 
18
- # Find one available haiku from given text.
28
+ ```rb
19
29
  reviewer.find("ああ古池や蛙飛び込む水の音ああ")
20
30
  #=> [["古池", "や"], ["蛙", "飛び込む"], ["水", "の", "音"]]
31
+ ```
32
+
33
+ ### Ikku::Reviewer#search
34
+ Search searches all available haikus from given text.
21
35
 
22
- # Search searches all available haikus from given text.
36
+ ```rb
23
37
  reviewer.search("ああ古池や蛙飛び込む水の音ああ天秤や京江戸かけて千代の春ああ")
24
38
  #=> [
25
39
  # [["古池", "や"], ["蛙", "飛び込む"], ["水", "の", "音"]],
@@ -27,3 +41,10 @@ reviewer.search("ああ古池や蛙飛び込む水の音ああ天秤や京江戸
27
41
  # ]
28
42
  #
29
43
  ```
44
+
45
+ ### rule option
46
+ Change the rule by `rule` option (default: `[5, 7, 5]`).
47
+
48
+ ```rb
49
+ reviewer.judge("すもももももももものうち", rule: [4, 3, 5]) #=> true
50
+ ```
data/lib/ikku.rb CHANGED
@@ -6,10 +6,10 @@ module Ikku
6
6
  class Reviewer
7
7
  # Find one available haiku from given text.
8
8
  # @return [Array<Array>]
9
- def find(text)
9
+ def find(text, rule: nil)
10
10
  nodes = parser.parse(text)
11
11
  nodes.length.times.find do |index|
12
- if (phrases = Scanner.new(nodes[index..-1]).scan)
12
+ if (phrases = Scanner.new(nodes[index..-1], rule: rule).scan)
13
13
  break phrases
14
14
  end
15
15
  end
@@ -17,16 +17,16 @@ module Ikku
17
17
 
18
18
  # Judge if given text is haiku or not.
19
19
  # @return [true, false]
20
- def judge(text)
21
- !Scanner.new(parser.parse(text), exactly: true).scan.nil?
20
+ def judge(text, rule: nil)
21
+ !Scanner.new(parser.parse(text), exactly: true, rule: rule).scan.nil?
22
22
  end
23
23
 
24
24
  # Search all available haikus from given text.
25
25
  # @return [Array<Array>]
26
- def search(text)
26
+ def search(text, rule: nil)
27
27
  nodes = parser.parse(text)
28
28
  nodes.length.times.map do |index|
29
- Scanner.new(nodes[index..-1]).scan
29
+ Scanner.new(nodes[index..-1], rule: rule).scan
30
30
  end.compact
31
31
  end
32
32
 
@@ -39,13 +39,14 @@ module Ikku
39
39
 
40
40
  # Find one haiku that starts from the 1st node of given nodes.
41
41
  class Scanner
42
- RULE = [5, 7, 5]
42
+ DEFAULT_RULE = [5, 7, 5]
43
43
 
44
44
  attr_writer :count
45
45
 
46
- def initialize(nodes, exactly: false)
46
+ def initialize(nodes, exactly: false, rule: nil)
47
47
  @exactly = exactly
48
48
  @nodes = nodes
49
+ @rule = rule
49
50
  end
50
51
 
51
52
  def scan
@@ -85,13 +86,13 @@ module Ikku
85
86
  end
86
87
 
87
88
  def first_of_phrase?
88
- RULE.inject([]) do |array, length|
89
+ rule.inject([]) do |array, length|
89
90
  array << array.last.to_i + length
90
91
  end.include?(count)
91
92
  end
92
93
 
93
94
  def has_full_count?
94
- count == RULE.inject(0, :+)
95
+ count == rule.inject(0, :+)
95
96
  end
96
97
 
97
98
  def has_valid_first_node?
@@ -103,18 +104,22 @@ module Ikku
103
104
  end
104
105
 
105
106
  def max_consumable_length
106
- RULE[0..phrase_index].inject(0, :+) - count
107
+ rule[0..phrase_index].inject(0, :+) - count
107
108
  end
108
109
 
109
110
  def phrase_index
110
- RULE.length.times.find do |index|
111
- count < RULE[0..index].inject(0, :+)
112
- end || RULE.length - 1
111
+ rule.length.times.find do |index|
112
+ count < rule[0..index].inject(0, :+)
113
+ end || rule.length - 1
113
114
  end
114
115
 
115
116
  def phrases
116
117
  @phrases ||= []
117
118
  end
119
+
120
+ def rule
121
+ @rule || DEFAULT_RULE
122
+ end
118
123
  end
119
124
 
120
125
  class Parser
data/lib/ikku/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ikku
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ikku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - CHANGELOG.md
77
78
  - Gemfile
78
79
  - LICENSE.txt
79
80
  - README.md