Class: Jelegante

Inherits:
Object
  • Object
show all
Defined in:
lib/jelegante.rb

Constant Summary collapse

JYOUYOU_KANJI_LIST =
%w[
                          
                          
                          
       沿                   
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                 稿         
                          
                          
                          
                          
            使         姿     
                          
                          
    湿                      
                      寿    
                          
         宿                 
                          
                          
                          
                          
                          
                          
                          
        西                  
                          
                          
                          
                          
                          
                退          
                          
                          
                          
           調               
                          
                         殿 
                          
                          
                          
                尿          
                          
                          
                          
                          
                          
                          
                          
                          便
               簿           
                          
       貿                   
                          
    婿           綿           
                          
                          
                          
                          
                          
                          
                     
].sort()
JINMEI_KANJI_LIST =
%w[
                          
                          
                廿          
                          
                          
                          
                          
                          
                          
                          
                          
                         椿 
                          
                          
                          
                          
                          
                          
          祿               穿 
        竿                  
                        耀  
                          
                          
                          
                          
                          
           輿      辿         
                          
                          
                      駿    
                   鹿    麿   
                          
                          
                          
                          
                          
     憎                     
                          
                          
           
].sort()
HYOUGAI_KANJI_LIST =
%w[
                          
                          
                          
                          
                          
                          竿
                          
                          
                          
    橿                      
                          
                          
                          
                          
                          
                          
                   𠮟       
                          
            廿              
                   漿       
                          
                          
                  穿        
                          
                          
                          
                          
                          
                          辿
                          
            禿              
                          
                          
                          
                          
                          
                          
                          
       輿                   
                          
                          
                          
                          
              
].sort()
(JYOUYOU_KANJI_LIST + JINMEI_KANJI_LIST + HYOUGAI_KANJI_LIST).sort()
HIRAGANA_PROP =
"\\p{Hiragana}"
KATAKANA_PROP =
"\\p{Katakana}"
KANJI_PROP =
"#{UnicodeBlock::CJK_UNIFIED_IDEOGRAPHS_PROP}#{UnicodeBlock::CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A_PROP}#{UnicodeBlock::CJK_COMPATIBILITY_IDEOGRAPHS_PROP}#{UnicodeBlock::CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B_PROP}#{UnicodeBlock::CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C_PROP}#{UnicodeBlock::CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D_PROP}#{UnicodeBlock::CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT_PROP}"
JYOUYOU_PROP =
"#{JYOUYOU_KANJI_LIST.join('')}"
JINMEI_PROP =
"#{JINMEI_KANJI_LIST.join('')}"
HYOUGAI_PROP =
"#{HYOUGAI_KANJI_LIST.join('')}"
"#{POPULAR_KANJI_LIST.join('')}"
HIRAGANA_REGEX =
/\p{Hiragana}/
KATAKANA_REGEX =
/\p{Katakana}/
KANJI_REGEX =
/[#{KANJI_PROP}]/
/[#{POPULAR_KANJI_PROP}]/
NO_KANJI_REGEX =
/[^#{KANJI_PROP}]/
/[^#{POPULAR_KANJI_PROP}]/

Class Method Summary collapse

Class Method Details

.include_hiragana?(text) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/jelegante.rb', line 196

def self.include_hiragana?(text)
  return !(HIRAGANA_REGEX.match(text).nil?);
end

.include_kanji?(text) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/jelegante.rb', line 202

def self.include_kanji?(text)
  return !(KANJI_REGEX.match(text).nil?);
end

.include_katakana?(text) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/jelegante.rb', line 199

def self.include_katakana?(text)
  return !(KATAKANA_REGEX.match(text).nil?);
end

Returns:

  • (Boolean)


205
206
207
# File 'lib/jelegante.rb', line 205

def self.include_popular_kanji?(text)
  return !(POPULAR_KANJI_REGEX.match(text).nil?);
end

.japanese?(text) ⇒ Boolean

簡易日本語判定

・ひらがな、カタカナを含む
・漢字を含むが、常用漢字・人名漢字・表外漢字以外の漢字を含まない

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
# File 'lib/jelegante.rb', line 212

def self.japanese?(text)
  return true if (self.include_hiragana?(text) || self.include_katakana?(text));

  # return true if (text.gsub(NO_KANJI_REGEX,"") =~ /^[#{POPULAR_KANJI_PROP}]+$/);
  kanjis = text.gsub(NO_KANJI_REGEX,"");
  return false if (kanjis.empty?);
  kanjis.each_char { |c| return false if (!POPULAR_KANJI_LIST.include?(c)); }
  return true;
end