Module: JLPT::PitchAccent

Defined in:
lib/jlpt/analyzers/pitch_accent.rb

Overview

Pitch Accent Profiler and Pattern Lookup.

Categorizes pitch accent types (Heiban / 平板 [0], Atamadaka / 頭高 [1], Nakadaka / 中高 [2+], Odaka / 尾高) for Japanese vocabulary words.

Constant Summary collapse

DATA_PATH =
File.expand_path('../data/pitch_accent.json', __dir__)
MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.accent_for(word) ⇒ Hash?

Lookup pitch accent for a vocabulary word

Parameters:

  • word (String)

    vocabulary word

Returns:

  • (Hash, nil)

    pitch accent hash with :type and :pattern



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jlpt/analyzers/pitch_accent.rb', line 21

def accent_for(word)
  return nil if word.nil? || word.to_s.strip.empty?

  w = word.to_s.strip
  dataset = load_dataset!

  if dataset.key?(w)
    { type: dataset[w]['type'].to_sym, pattern: dataset[w]['pattern'] }
  else
    { type: :heiban, pattern: 0 }
  end
end

.profile(text) ⇒ Hash

Profile pitch accent patterns in Japanese text

Parameters:

  • text (String)

    Japanese text

Returns:

  • (Hash)

    breakdown of pitch accent types in text



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jlpt/analyzers/pitch_accent.rb', line 38

def profile(text)
  tokens = Tokenizer.tokenize(text)
  result = { heiban: 0, atamadaka: 0, nakadaka: 0, odaka: 0 }

  tokens.each do |t|
    info = accent_for(t[:surface])
    next unless info

    type = info[:type]
    result[type] += 1 if result.key?(type)
  end
  result
end