Module: JLPT::Kanji
- Defined in:
- lib/jlpt/core/kanji.rb
Overview
Dedicated Kanji Character Analyzer and Extractor.
Identifies Kanji characters in Japanese text, maps them to JLPT levels (N5-N1), calculates kanji density, and extracts unique kanji filtered by level.
Constant Summary collapse
- KANJI_REGEX =
/[\u4E00-\u9FFF]/.freeze
Class Method Summary collapse
-
.breakdown(text) ⇒ Hash{Symbol => Array<String>}
Detailed breakdown of Kanji by JLPT level.
-
.density(text) ⇒ Float
Calculate Kanji density ratio (Kanji characters / Total characters).
-
.extract(text, level: nil) ⇒ Array<String>
Extract all Kanji characters from text.
Class Method Details
.breakdown(text) ⇒ Hash{Symbol => Array<String>}
Detailed breakdown of Kanji by JLPT level
44 45 46 47 48 49 50 51 |
# File 'lib/jlpt/core/kanji.rb', line 44 def breakdown(text) res = { n5: [], n4: [], n3: [], n2: [], n1: [], out_of_jlpt: [] } extract(text).each do |k| lvl = Dictionary.kanji_level(k) res.key?(lvl) ? res[lvl] << k : res[:out_of_jlpt] << k end res end |
.density(text) ⇒ Float
Calculate Kanji density ratio (Kanji characters / Total characters)
32 33 34 35 36 37 38 |
# File 'lib/jlpt/core/kanji.rb', line 32 def density(text) cleaned = Preprocessor.clean(text) return 0.0 if cleaned.empty? kanji_count = cleaned.scan(KANJI_REGEX).length (kanji_count.to_f / cleaned.length).round(4) end |
.extract(text, level: nil) ⇒ Array<String>
Extract all Kanji characters from text
18 19 20 21 22 23 24 25 26 |
# File 'lib/jlpt/core/kanji.rb', line 18 def extract(text, level: nil) return [] if text.nil? || text.to_s.strip.empty? kanjis = Preprocessor.clean(text).scan(KANJI_REGEX).uniq return kanjis if level.nil? target_level = level.to_s.downcase.to_sym kanjis.select { |k| Dictionary.kanji_level(k) == target_level } end |