Class: JDict::Entry
- Inherits:
-
Object
- Object
- JDict::Entry
- 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
-
#kana ⇒ Object
Returns the value of attribute kana.
-
#kanji ⇒ Object
Returns the value of attribute kanji.
-
#senses ⇒ Object
Returns the value of attribute senses.
-
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
Class Method Summary collapse
-
.from_sql(row) ⇒ Object
Converts an SQLite row from the index to the Entry format.
Instance Method Summary collapse
-
#initialize(sequence_number, kanji, kana, senses) ⇒ Entry
constructor
Create a new Entry entry = initialize(kanji, kana, senses).
- #kana_to_s ⇒ Object
- #kanji_to_s ⇒ Object
-
#senses_by_language(l) ⇒ Object
Get an array of
Sensesfor the specified language. - #senses_to_s(delimiter = "\n") ⇒ Object
- #to_s ⇒ Object
-
#to_sql ⇒ String
Converts an Entry to a string to be indexed into the SQLite database.
Constructor Details
#initialize(sequence_number, kanji, kana, senses) ⇒ Entry
Create a new Entry
entry = initialize(kanji, kana, senses)
12 13 14 |
# File 'lib/entry.rb', line 12 def initialize(sequence_number, kanji, kana, senses) @sequence_number, @kanji, @kana, @senses = sequence_number, kanji, kana, senses end |
Instance Attribute Details
#kana ⇒ Object
Returns the value of attribute kana.
9 10 11 |
# File 'lib/entry.rb', line 9 def kana @kana end |
#kanji ⇒ Object
Returns the value of attribute kanji.
9 10 11 |
# File 'lib/entry.rb', line 9 def kanji @kanji end |
#senses ⇒ Object
Returns the value of attribute senses.
9 10 11 |
# File 'lib/entry.rb', line 9 def senses @senses end |
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
9 10 11 |
# File 'lib/entry.rb', line 9 def sequence_number @sequence_number 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 55 |
# File 'lib/entry.rb', line 27 def self.from_sql(row) sequence_number = row["sequence_number"].to_i 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.config.language] || glosses[JDict::JMDictConstants::Languages::ENGLISH] senses << Sense.new(parts_of_speech, glosses_for_lang) # ** is the sentinel sequence end self.new(sequence_number, kanji, kana, senses) end |
Instance Method Details
#kana_to_s ⇒ Object
90 91 92 |
# File 'lib/entry.rb', line 90 def kana_to_s @kana.join(', ') unless @kana.nil? end |
#kanji_to_s ⇒ Object
86 87 88 |
# File 'lib/entry.rb', line 86 def kanji_to_s @kanji.join(', ') end |
#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 |
#senses_to_s(delimiter = "\n") ⇒ Object
94 95 96 97 98 99 |
# File 'lib/entry.rb', line 94 def senses_to_s(delimiter = "\n") list = @senses.map.with_index(1) do |sense, i| "#{i}. #{sense.to_s}" end list.join("\n") end |
#to_s ⇒ Object
79 80 81 82 83 84 |
# File 'lib/entry.rb', line 79 def to_s str = "" str << "#{kanji_to_s} (#{kana_to_s})\n" str << "#{senses_to_s}\n" str end |
#to_sql ⇒ String
Converts an Entry to a string to be indexed into the SQLite database
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/entry.rb', line 59 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 # FIXME: it fails when retrieving entries from an existing index, because only one language is retrieved and the 'lang' field is nil sense << s.glosses.collect { |lang, texts| lang.to_s + LANGUAGE_SENTINEL + texts.join(MEANING_SENTINEL) }.compact.join(GLOSS_SENTINEL) end { ':sequence_number' => sequence_number.to_s, ':kanji' => kanji.join(", "), ':kana' => kana.join(", "), ':senses' => sense_strings.join(SENSE_SENTINEL) } end |