Class: PROIEL::Dictionary::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/proiel/dictionary/builder.rb

Constant Summary collapse

CURRENT_SCHEMA_VERSION =
'3.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



15
16
17
18
19
20
21
# File 'lib/proiel/dictionary/builder.rb', line 15

def initialize
  @language = nil
  @license = nil
  @sources = []
  @lemmata = {}
  @valency = PROIEL::Valency::Lexicon.new
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



11
12
13
# File 'lib/proiel/dictionary/builder.rb', line 11

def language
  @language
end

#lemmataObject (readonly)

Returns the value of attribute lemmata.



13
14
15
# File 'lib/proiel/dictionary/builder.rb', line 13

def lemmata
  @lemmata
end

#licenseObject (readonly)

Returns the value of attribute license.



10
11
12
# File 'lib/proiel/dictionary/builder.rb', line 10

def license
  @license
end

#sourcesObject (readonly)

Returns the value of attribute sources.



12
13
14
# File 'lib/proiel/dictionary/builder.rb', line 12

def sources
  @sources
end

Instance Method Details

#add_source!(source) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/proiel/dictionary/builder.rb', line 23

def add_source!(source)
  raise ArgumentError, 'source expected' unless source.is_a?(PROIEL::Source)
  raise ArgumentError, 'incompatible language' unless @language.nil? or @language == source.language
  raise ArgumentError, 'incompatible license' unless @license.nil? or @license == source.license

  @language ||= source.language
  @license ||= source.license
  @sources << source

  source.tokens.each { |token| index_token!(token) }

  index_homographs!
end

#to_xml(io) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/proiel/dictionary/builder.rb', line 39

def to_xml(io)
  builder = ::Builder::XmlMarkup.new(target: io, indent: 2)
  builder.instruct! :xml, version: '1.0', encoding: 'UTF-8'
  builder.proiel('export-time': DateTime.now.xmlschema, 'schema-version': CURRENT_SCHEMA_VERSION) do
    builder.dictionary(language: @language) do
      builder.sources do
        @sources.each do |source|
          builder.source(id: source.id, license: source.license)
        end
      end

      builder.lemmata(n: @lemmata.count) do
        @lemmata.sort_by { |lemma, _| lemma.downcase }.each do |form, data|
          lemma_to_xml(builder, form, data)
        end
      end
    end
  end
end