Class: JDict::Entry

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

Constant Summary collapse

KANA_RE =
/^kana/
SENSE_RE =
/^sense/
PART_OF_SPEECH_RE =
/^\[\[([^\]]+)\]\]\s+/
MEANING_SENTINEL =
'**'
PART_OF_SPEECH_SENTINEL =
'$$'
SENSE_SENTINEL =
'%%'
LANGUAGE_SENTINEL =
'&&'
GLOSS_SENTINEL =
'@@'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kanji, kana, senses) ⇒ Entry

Create a new Entry

entry = initialize(kanji, kana, senses)


12
13
14
# File 'lib/entry.rb', line 12

def initialize(kanji, kana, senses)
  @kanji, @kana, @senses = kanji, kana, senses
end

Instance Attribute Details

#kanaObject

Returns the value of attribute kana.



9
10
11
# File 'lib/entry.rb', line 9

def kana
  @kana
end

#kanjiObject

Returns the value of attribute kanji.



9
10
11
# File 'lib/entry.rb', line 9

def kanji
  @kanji
end

#sensesObject

Returns the value of attribute senses.



9
10
11
# File 'lib/entry.rb', line 9

def senses
  @senses
end

Class Method Details

.from_sql(row) ⇒ Object

Converts an SQLite row from the index to the Entry format



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/entry.rb', line 27

def self.from_sql(row)
  kanji = row["kanji"].split(", ").map { |k| k = k.force_encoding("UTF-8") }
  kana = row["kana"].split(", ").map { |k| k = k.force_encoding("UTF-8") }
  senses = []
  row["senses"].split(SENSE_SENTINEL).sort.each do |txt|
    ary = txt.scan(PART_OF_SPEECH_RE)
    if ary.size == 1
      parts_of_speech = ary[0][0].split(PART_OF_SPEECH_SENTINEL)
      gloss_strings = txt[(ary.to_s.length-1)..-1]
    else
      parts_of_speech = nil
      gloss_strings = txt[5..-1]
    end

    gloss_strings = gloss_strings.force_encoding("UTF-8").strip.split(GLOSS_SENTINEL)

    glosses = {}
    gloss_strings.each do |str|
      lang, meaning_string = str.split(LANGUAGE_SENTINEL)
      lang = lang.to_sym
      meanings = meaning_string.split(MEANING_SENTINEL)
      (glosses[lang] ||= []) << meanings
    end
    glosses_for_lang = glosses[JDict.configuration.language] || glosses[JDict::JMDictConstants::Languages::ENGLISH]
    senses << Sense.new(parts_of_speech, glosses_for_lang) # ** is the sentinel sequence
  end
  self.new(kanji, kana, senses)
end

Instance Method Details

#senses_by_language(l) ⇒ Object

Get an array of Senses for the specified language



75
76
77
# File 'lib/entry.rb', line 75

def senses_by_language(l)
  senses.select { |s| s.language == l }
end

#to_sqlString

Converts an Entry to a string to be indexed into the SQLite database

Returns:

  • (String)

    the serialized string for this Entry



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/entry.rb', line 58

def to_sql
  sense_strings = senses.map do |s|
    sense = ''
    sense << "[[#{s.parts_of_speech.join(PART_OF_SPEECH_SENTINEL)}]] " if s.parts_of_speech
    sense << s.glosses.collect { |lang, texts| lang.to_s + LANGUAGE_SENTINEL + texts.join(MEANING_SENTINEL) }.compact.join(GLOSS_SENTINEL)
  end

  insert_data  = {
    ':kanji'   => kanji.join(", "),
    ':kana' => kana.join(", "),
    ':senses' => sense_strings.join(SENSE_SENTINEL)
  }

  return insert_data
end