Class: Lingo::Database::Source
- Inherits:
-
Object
- Object
- Lingo::Database::Source
- 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/single_word.rb,
lib/lingo/database/source/multi_value.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
Defined Under Namespace
Classes: KeyValue, MultiKey, MultiValue, SingleWord, WordClass
Instance Attribute Summary (collapse)
-
- (Object) pos
readonly
Returns the value of attribute pos.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) each
-
- (Source) initialize(id, lingo)
constructor
A new instance of Source.
- - (Object) set(db, key, val)
- - (Object) size
Constructor Details
- (Source) initialize(id, lingo)
A new instance of Source
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lingo/database/source.rb', line 56 def initialize(id, lingo) @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', Language::LA_UNKNOWN).downcase @sep = @config['separator'] @wrd = "(?:#{Language::Char::ANY})+" @pat = /^#{@wrd}$/ @pos = 0 end |
Instance Attribute Details
- (Object) pos (readonly)
Returns the value of attribute pos
54 55 56 |
# File 'lib/lingo/database/source.rb', line 54 def pos @pos end |
Class Method Details
+ (Object) get(name, *args)
50 51 52 |
# File 'lib/lingo/database/source.rb', line 50 def self.get(name, *args) Lingo.get_const(name, self).new(*args) end |
Instance Method Details
- (Object) each
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/lingo/database/source.rb', line 84 def each reject_file = @rej.open('w', encoding: ENC) if @rej @src.each_line($/, encoding: ENC) { |line| @pos += length = line.bytesize next if line =~ /\A\s*#/ || line.strip.empty? line.chomp! line.replace(Unicode.downcase(line)) if length < 4096 && line =~ @pat yield convert_line(line, $1, $2) else reject_file.puts(line) if reject_file end } self ensure if reject_file reject_file.close @rej.delete if @rej.size == 0 end end |
- (Object) set(db, key, val)
110 111 112 |
# File 'lib/lingo/database/source.rb', line 110 def set(db, key, val) db[key] = val end |
- (Object) size
80 81 82 |
# File 'lib/lingo/database/source.rb', line 80 def size @src.size end |