Permalink
Browse files

Add prototype code

  • Loading branch information...
myokoym committed Oct 6, 2012
1 parent 6c4a3fb commit 7e8777d53525c66d0a9926a7d72a88b8276c7c6a
Showing with 67 additions and 0 deletions.
  1. +53 −0 lib/mecab-syllable/syllable.rb
  2. +1 −0 spec/spec_helper.rb
  3. +13 −0 spec/syllable_spec.rb
@@ -0,0 +1,53 @@
+# encoding: utf-8
+
+require 'MeCab'
+
+module MeCab
+ class Node
+ def part
+ surface.force_encoding("UTF-8")
+ end
+
+ def kana
+ feature.force_encoding("UTF-8").split(/,/)[8]
+ end
+ end
+
+ class Syllable
+ def initialize(text)
+ @syllables = split(text)
+ end
+
+ def count
+ @syllables.flatten.size
+ end
+
+ private
+ def split(text)
+ mecab = MeCab::Tagger.new
+ node = mecab.parseToNode(text)
+ syllables = []
+ while node
+ part = node.part
+ unless part.empty?
+ kana = node.kana
+ if kana
+ syllables << select_syllable(kana)
+ else
+ syllables << select_syllable(part)
+ end
+ end
+ node = node.next
+ end
+ syllables
+ end
+
+ def select_syllable(kana)
+ hiragana_to_katakana(kana).gsub(/[^アイウエオカ-モヤユヨラ-ロワヲンヴー]/, "").split(//)
+ end
+
+ def hiragana_to_katakana(hiragana)
+ hiragana.tr("ぁ-ゔ","ァ-ヴ")
+ end
+ end
+end
View
@@ -0,0 +1 @@
+require 'mecab-syllable/syllable'
View
@@ -0,0 +1,13 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe MeCab::Syllable do
+ subject { MeCab::Syllable }
+ it "めかぶ == 3" do
+ subject.new("めかぶ").count.should == 3
+ end
+
+ it "今日は雨 == 5" do
+ subject.new("今日は雨").count.should == 5
+ end
+end

0 comments on commit 7e8777d

Please sign in to comment.