Module: Zipang

Defined in:
lib/zipang.rb,
lib/zipang/version.rb

Constant Summary collapse

KATAKANA_PATTERN =
/^[ア-ンー〜]+$/
ZENKAKU_SPACES_PATTERN =
/\p{blank}/
GLUE =
'-'
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.clean_spaces(str) ⇒ Object



24
25
26
# File 'lib/zipang.rb', line 24

def clean_spaces(str)
  str.gsub(ZENKAKU_SPACES_PATTERN, "\s").strip.gsub(/\s/, GLUE)
end

.katakana?(str) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/zipang.rb', line 28

def katakana?(str)
  return unless str
  str.match(KATAKANA_PATTERN)
end

.to_slug(str) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zipang.rb', line 12

def to_slug(str)
  Kuromoji.process(:getAllFeatures, clean_spaces(str))
    .map{ |word, features| [word].concat features.split(',') }
    .map{ |words| words.last == '*' ? words.first : words.last }
    .map{ |word| word.tr('0-9', '0-9') }
    .map{ |word| katakana?(word) ? Romaji.kana2romaji(word) : word }
    .map{ |word| word.match(/^[0-9a-zA-Z\-]+$/) ? word.downcase : nil }
    .compact
    .join(GLUE)
    .squeeze('-')
end