Class Jmdict::ImportXrefListener
In: lib/jmdict/import_xref_listener.rb
Parent: Jmdict::ImportXmlListener

Methods

end_entry   new   start_entry   text  

Public Class methods

[Source]

    # File lib/jmdict/import_xref_listener.rb, line 3
 3:   def initialize(filename, version, maximum_parsed = nil)
 4:     super(filename, version, maximum_parsed)
 5: 
 6:     @word_reference_type = WordReferenceType.get_synonym()
 7:     # run-time states and objects
 8:     @_current_xrefs = Array.new
 9:     @_current_word = nil
10:   end

Public Instance methods

[Source]

    # File lib/jmdict/import_xref_listener.rb, line 63
63:   def end_entry
64:     if not @_skip_entry
65:       
66:       for xref in @_current_xrefs
67:         unless WordReference.find_by_word_id_and_reference_word_id_and_word_reference_type_id(@_current_word.id,xref.id,@word_reference_type.id)
68:           @_number_parsed                  = @_number_parsed + 1
69:           reference                        = WordReference.new
70:           reference.word_id                = @_current_word.id
71:           reference.reference_word_id      = xref.id
72:           reference.word_reference_type_id = @word_reference_type.id
73:           @records                         << reference
74:         end
75:         message("Parsed [#{@_number_parsed}]th entry.") if 0 == (@_number_parsed % @message_every_nth_time)
76:       end     
77:     end
78:     reset
79:   end

[Source]

    # File lib/jmdict/import_xref_listener.rb, line 58
58:   def start_entry(attrs)
59:     @_current_word                    = nil
60:     @_current_xrefs.clear
61:   end

[Source]

    # File lib/jmdict/import_xref_listener.rb, line 12
12:   def text(text)
13:     return unless text && @_current_tag
14:     case @_current_tag
15:     when 'ent_seq'
16:       source_tag = text
17:       @_current_word = Word.find_by_source_tag_and_language_id(source_tag,@japanese_language.id)
18:       unless @_current_word
19:         # ignore entries we have already imported
20:         @_skip_entry = @_skip_entry ? @_skip_entry : true
21:         Rails.logger.error("Unable to find a word with the source tag [#{source_tag}] for language #{@japanese_language.language}.  Did you forget to first import from JMDict?")
22:       end
23:     when 'xref' 
24:       xref_word          = nil
25:       xref_spelling      = ''
26:       xref_pronunciation = ''
27:       
28:       xref               = text.split('・')
29:       case xref.size
30:       when 0
31:         # ignore
32:       when 1
33:         xref_spelling      = xref[0]
34:         xref_word          = Word.find_by_spelling_and_language_id(xref_spelling,@japanese_language.id)
35:       when 2..3
36:         xref_spelling      = xref[0]
37:         xref_pronunciation = xref[1]
38:         xref_word = Word.find_by_spelling_and_pronunciation_and_language_id(xref_spelling,xref_pronunciation,@japanese_language.id)
39:         unless xref_word
40:           # if we cannot find a specific word with that spelling and pronunciation then 
41:           # at least get a word that matches that spelling (it may imply we have a defect 
42:           # in JMdict itself)
43:           xref_word        = Word.find_by_spelling_and_language_id(xref_spelling,@japanese_language.id)        
44:           Rails.logger.warn("Unable to find a cross-reference word with the spelling [#{xref_spelling}], pronunciation [#{xref_pronunciation}] for language #{@japanese_language.language}.  Falling back to a word with just matching spelling and language.")
45:         end
46:       else
47:         raise("We do not know how to handle xref [#{text}] with size [#{xref.size}]")        
48:       end
49: 
50:       if xref_word
51:         @_current_xrefs << xref_word
52:       else
53:         Rails.logger.error("Unable to find a cross-reference word with the spelling [#{xref_spelling}], pronunciation [#{xref_pronunciation}] for language #{@japanese_language.language}")
54:       end
55:     end
56:   end

[Validate]