Class: Lingo::Database::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/lingo/database/source.rb,
lib/lingo/database/source/multi_key.rb,
lib/lingo/database/source/key_value.rb,
lib/lingo/database/source/word_class.rb,
lib/lingo/database/source/multi_value.rb,
lib/lingo/database/source/single_word.rb

Overview

– Die Klasse Source stellt eine einheitliche Schnittstelle auf die unterschiedlichen Formate von Wörterbuch-Quelldateien bereit. Die Identifizierung der Quelldatei erfolgt über die ID der Datei, so wie sie in der Sprachkonfigurationsdatei de.lang unter language/dictionary/databases hinterlegt ist.

Die Verarbeitung der Wörterbücher erfolgt mittels des Iterators each, der für jede Zeile der Quelldatei ein Array bereitstellt in der Form [ key, [val1, val2, ...] ].

Nicht korrekt erkannte Zeilen werden abgewiesen und in eine Revoke-Datei gespeichert, die an der Dateiendung .rev zu erkennen ist. ++

Direct Known Subclasses

KeyValue, MultiKey, MultiValue, SingleWord, WordClass

Defined Under Namespace

Classes: KeyValue, MultiKey, MultiValue, SingleWord, WordClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, lingo, def_wc_default = nil) ⇒ Source

Returns a new instance of Source



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lingo/database/source.rb', line 58

def initialize(id, lingo, def_wc_default = nil)
  @config = lingo.database_config(id)

  source_file = Lingo.find(:dict, name = @config['name'], relax: true)

  reject_file = begin
    Lingo.find(:store, source_file) << '.rev'
  rescue NoWritableStoreError, SourceFileNotFoundError
  end

  @src = Pathname.new(source_file)
  @rej = Pathname.new(reject_file) if reject_file

  raise SourceFileNotFoundError.new(name, id) unless @src.exist?

  @def = @config.fetch('def-wc', def_wc_default)
  @def = @def.downcase if @def
  @sep = @config['separator']

  @wrd = "(?:#{Language::Char::ANY})+"
  @pat = /^#{@wrd}$/

  @pos = @rej_cnt = 0
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos



56
57
58
# File 'lib/lingo/database/source.rb', line 56

def pos
  @pos
end

Class Method Details

.get(name, *args) ⇒ Object



52
53
54
# File 'lib/lingo/database/source.rb', line 52

def self.get(name, *args)
  Lingo.get_const(name, self).new(*args)
end

Instance Method Details

#eachObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lingo/database/source.rb', line 87

def each
  reject_file = @rej.open('w', encoding: ENC) if @rej

  @src.each_line($/, encoding: ENC) { |line|
    @pos += length = line.bytesize

    line.strip!
    next if line.empty? || line.start_with?('#')

    line.chomp!
    line.replace(Unicode.downcase(line))

    if length < 4096 && line =~ @pat
      yield convert_line(line, $1, $2)
    else
      @rej_cnt += 1
      reject_file.puts(line) if reject_file
    end
  }

  self
ensure
  if reject_file
    reject_file.close
    @rej.delete if @rej.size == 0
  end
end

#rejectedObject



119
120
121
# File 'lib/lingo/database/source.rb', line 119

def rejected
  [@rej_cnt, @rej]
end

#set(db, key, val) ⇒ Object



115
116
117
# File 'lib/lingo/database/source.rb', line 115

def set(db, key, val)
  db[key] = val
end

#sizeObject



83
84
85
# File 'lib/lingo/database/source.rb', line 83

def size
  @src.size
end