Class: JDict::NokogiriDictionaryIndexer

Inherits:
DictionaryIndexer show all
Defined in:
lib/ruby-jdict/indexer/nokogiri_dictionary_indexer.rb

Instance Attribute Summary

Attributes inherited from DictionaryIndexer

#parts_of_speech

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ NokogiriDictionaryIndexer

Returns a new instance of NokogiriDictionaryIndexer.



5
6
7
8
9
10
11
# File 'lib/ruby-jdict/indexer/nokogiri_dictionary_indexer.rb', line 5

def initialize(path)
  super

  @doc = File.open(path) do |f|
    Nokogiri::XML(f) { |c| c.strict }
  end
end

Instance Method Details

#index(db_transaction, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby-jdict/indexer/nokogiri_dictionary_indexer.rb', line 13

def index(db_transaction, &block)
  raw = @doc/"./JMdict/entry"
  total = raw.count
  entries_added = 0

  raw.each do |entry|
    yield entries_added, total if block_given?

    sequence_number = entry.at(JDict::JMDictConstants::Elements::SEQUENCE).content.to_i
    kanji = (entry/JDict::JMDictConstants::Elements::KANJI).map(&:content)
    kana = (entry/JDict::JMDictConstants::Elements::KANA).map(&:content)
    senses = (entry/JDict::JMDictConstants::Elements::SENSE).map(&method(:extract_sense))

    entry = Entry.new(sequence_number, kanji, kana, senses)
    add_entry(db_transaction, entry)
    entries_added += 1
  end

  printf "\n"

  entries_added
end

#parse_parts_of_speechObject



36
37
38
# File 'lib/ruby-jdict/indexer/nokogiri_dictionary_indexer.rb', line 36

def parse_parts_of_speech
  {}
end