Last active
May 1, 2017 06:57
-
-
Save Naggi-Goishi/53c29ed3a7f96fffe4a4c00da53e6043 to your computer and use it in GitHub Desktop.
Rubyで漢字をひらがなに変換する ref: http://qiita.com/Naggi-Goishi/items/e9f8753e88bc9f0b67b5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
AGENT = Mechanize.new | |
BASE_URL = 'https://yomikatawa.com/kanji/' | |
def to_hiragana(kanji) | |
AGENT.get(BASE_URL + kanji).search('#content p').first.inner_text | |
end | |
def to_romaji(kanji) | |
AGENT.get(BASE_URL + kanji).search('#content p')[1].inner_text | |
end | |
# 検索結果が正しくない可能性がある時、alertがでるのでそれを所得するメソッド | |
# ひらがな所得時に確実性をもたせたい時に使う。 | |
def certain?(kanji) | |
AGENT.get(BASE_URL + kanji).search('.alert').empty? | |
end | |
to_hiragana('薔薇') | |
#=> ばら | |
to_hiragana('鶏') | |
#=> にわとり | |
to_romaji('薔薇') | |
#=> bara |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem install mechanize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment