Module: JLPT::Dictionary

Defined in:
lib/jlpt/core/dictionary.rb

Overview

Thread-safe In-Memory Dictionary Manager for JLPT Vocabulary and Kanji Datasets.

Loaded from Bluskyo JLPT_Vocabulary datasets (N5 to N1).

Constant Summary collapse

DATA_DIR =
File.expand_path('../data', __dir__)
MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.kanji_exists?(char) ⇒ Boolean

Check if a kanji exists in the JLPT kanji dataset

Parameters:

  • char (String)

Returns:

  • (Boolean)


62
63
64
# File 'lib/jlpt/core/dictionary.rb', line 62

def kanji_exists?(char)
  !kanji_level(char).nil?
end

.kanji_level(char) ⇒ Symbol?

Look up the JLPT level of a single Kanji character

Parameters:

  • char (String)

    single Kanji character

Returns:

  • (Symbol, nil)

    :n5, :n4, :n3, :n2, :n1 or nil if non-kanji / unknown



42
43
44
45
46
47
48
# File 'lib/jlpt/core/dictionary.rb', line 42

def kanji_level(char)
  ensure_loaded!
  return nil if char.nil? || char.to_s.strip.empty?

  kanji_char = char.to_s.strip[0]
  @kanji_index[kanji_char]
end

.reading_for(word) ⇒ String?

Look up reading (furigana) for a vocabulary word

Parameters:

  • word (String)

Returns:

  • (String, nil)


31
32
33
34
35
36
# File 'lib/jlpt/core/dictionary.rb', line 31

def reading_for(word)
  ensure_loaded!
  return nil if word.nil? || word.to_s.strip.empty?

  @readings_index[word.to_s.strip]
end

.vocab_exists?(word) ⇒ Boolean

Check if a word exists in the JLPT vocabulary dataset

Parameters:

  • word (String)

Returns:

  • (Boolean)


54
55
56
# File 'lib/jlpt/core/dictionary.rb', line 54

def vocab_exists?(word)
  !vocab_level(word).nil?
end

.vocab_level(word) ⇒ Symbol?

Look up the JLPT level of a vocabulary word (dictionary form or lemma)

Parameters:

  • word (String)

    vocabulary word

Returns:

  • (Symbol, nil)

    :n5, :n4, :n3, :n2, :n1 or nil if unknown



20
21
22
23
24
25
# File 'lib/jlpt/core/dictionary.rb', line 20

def vocab_level(word)
  ensure_loaded!
  return nil if word.nil? || word.to_s.strip.empty?

  @vocab_index[word.to_s.strip]
end