rubyrubi 0.2.0 → 0.3.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: 7cb77ba08c3b3dc229e661780995c5ab14642ded
4
- data.tar.gz: 91061794091f1a8f75be1cb86474fae3434364df
3
+ metadata.gz: b005848f15f70889efb8ea6ab3fa98105e880721
4
+ data.tar.gz: 86f9fc910744efc2d59b0a9849b472add0277ee2
5
5
  SHA512:
6
- metadata.gz: a81489f35977a484b9a99904bf1fa07394cd3c1e39d5798f1bffe308ac2c27ebe57c5aedb34307d0d53e723f5f4f1e0cf507ab1a95cbc7d7afbad1f0e17f4fc1
7
- data.tar.gz: 6b29c681a67f9b066f0a8871c12a6ac2b68b9f88200ac2caa4720717e71c884cbf6003ad4b8266dfb5535527bfa3ef7e0ef22331f62da9232c01f334b9b8950d
6
+ metadata.gz: 033a61ebedf61ae3f2f2fe5dd762d9a8929f7bca7e25395a9ff045bb253ac20e9abdd3c9c99b1fad04a761454306ba352149970948b6a3708fc20a95f427cc45
7
+ data.tar.gz: ee1ce99f822b8de75a6d008a58ef5d6991e02139ab3c1615331957c18d6809c0c06432946b0127c53aede60826906cb02a07f765ff08d34fb8107143377d74ef
@@ -9,6 +9,10 @@ module Rubyrubi
9
9
  parser = Parser::Base.new(result)
10
10
  parser.parse().join('')
11
11
  end
12
+
13
+ def tag(text, ruby)
14
+ Parser.ruby_tag(text, ruby)
15
+ end
12
16
  end
13
17
  end
14
18
 
@@ -1,15 +1,18 @@
1
1
  module Rubyrubi
2
2
  module Parser
3
- KANJI = /^[一-龠]+/
4
- OKURI = /[\p{hiragana}\p{katakana}ー〜]+$/
3
+ KANJI = /[一-龠]+/
4
+ OKURI = /[\p{hiragana}\p{katakana}ー=〜・]+/
5
5
  KOMEI = /固名商品/
6
6
 
7
+ def self.ruby_tag(text, ruby)
8
+ "<ruby>#{text}<rp>(</rp><rt>#{ruby}</rt><rp>)</rp></ruby>"
9
+ end
10
+
7
11
  class Base
8
12
  def initialize(result)
9
13
  result = result.with_indifferent_access
10
14
  @result = result
11
15
  @original = result[:ResultSet][:ma_result][:word_list][:word]
12
- #p @original
13
16
  end
14
17
 
15
18
  def parse()
@@ -20,34 +23,38 @@ module Rubyrubi
20
23
  end
21
24
 
22
25
  def add_rubi_and_okuri(word)
23
- #p word
24
26
  if word['feature'] && word['feature'].scan(KOMEI).size > 0
25
27
  return word.clone
26
28
  end
27
29
 
28
- kanji = word['surface'].scan(KANJI)[0]
29
-
30
- if kanji == nil
31
- return word.clone
32
- end
30
+ kanji = word['surface'].scan(KANJI)
31
+ okuri = word['surface'].scan(OKURI)
32
+ yomi = '' + word['reading']
33
33
 
34
- okuri = word['surface'].scan(OKURI)[0]
35
- rubi = kanji == nil && okuri == nil ? nil
36
- : word['reading'].gsub(Regexp.new("#{okuri}$"), '')
34
+ return word.clone if !kanji || kanji.size == 0
37
35
 
38
- new_word = word.clone.merge({
39
- 'kanji' => kanji,
40
- 'okuri' => okuri,
41
- 'rubi' => rubi
42
- })
43
- new_word.keep_if { |k, v| v }
36
+ kanji.map.with_index do |k, i|
37
+ ret = { 'kanji' => k }
38
+ oku = okuri[i]
39
+ rubi = ''
40
+ if oku
41
+ ret.merge!({ 'okuri' => oku })
42
+ rubi = yomi.slice(0, yomi.index(oku))
43
+ yomi = yomi.slice(yomi.index(oku) + oku.length, yomi.length)
44
+ else
45
+ rubi = yomi
46
+ end
47
+ ret.merge!({ 'rubi' => rubi })
48
+ end
44
49
  end
45
50
 
46
51
  def create_markup(data)
47
- data.map do |e, i|
48
- if e['rubi']
49
- "<ruby>#{e['kanji']}<rp>(</rp><rt>#{e['rubi']}</rt><rp>)</rp></ruby>#{e['okuri'] || ''}"
50
- else
52
+ data.map.with_index do |e, i|
53
+ if e.class == Array
54
+ e.map do |r|
55
+ Parser.ruby_tag(r['kanji'], r['rubi']) << "#{r['okuri'] ? r['okuri'] : ''}"
56
+ end.join('')
57
+ elsif e['surface']
51
58
  "#{e['surface']}"
52
59
  end
53
60
  end
@@ -1,3 +1,3 @@
1
1
  module Rubyrubi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyrubi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 110chang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-08 00:00:00.000000000 Z
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler