Class: Kotoshu::Readers::DicReader

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/readers/dic_reader.rb

Overview

DIC file reader for Hunspell dictionary files.

This class reads .dic files and creates a list of Word entries.

Examples:

Reading a dic file

reader = DicReader.new('en_US.dic', flag_format: 'short')
words = reader.read

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, encoding: 'UTF-8', flag_format: 'short', flag_synonyms: {}) ⇒ DicReader

Create a new DIC reader.

Parameters:

  • path (String)

    Path to the .dic file

  • encoding (String) (defaults to: 'UTF-8')

    File encoding (default: 'UTF-8')

  • flag_format (String) (defaults to: 'short')

    Flag format ('short', 'long', 'num', 'UTF-8')

  • flag_synonyms (Hash) (defaults to: {})

    Flag synonyms map



159
160
161
162
163
164
# File 'lib/kotoshu/readers/dic_reader.rb', line 159

def initialize(path, encoding: 'UTF-8', flag_format: 'short', flag_synonyms: {})
  @path = path
  @encoding = encoding
  @flag_format = flag_format
  @flag_synonyms = flag_synonyms
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



151
152
153
# File 'lib/kotoshu/readers/dic_reader.rb', line 151

def encoding
  @encoding
end

#flag_formatObject (readonly)

Returns the value of attribute flag_format.



151
152
153
# File 'lib/kotoshu/readers/dic_reader.rb', line 151

def flag_format
  @flag_format
end

#flag_synonymsObject (readonly)

Returns the value of attribute flag_synonyms.



151
152
153
# File 'lib/kotoshu/readers/dic_reader.rb', line 151

def flag_synonyms
  @flag_synonyms
end

#pathObject (readonly)

Returns the value of attribute path.



151
152
153
# File 'lib/kotoshu/readers/dic_reader.rb', line 151

def path
  @path
end

Instance Method Details

#read(source = nil) ⇒ Array<Word>

Read the dic file and return a list of Word entries.

Parameters:

  • source (FileReader, nil) (defaults to: nil)

    Optional file reader to use instead of creating a new one

Returns:

  • (Array<Word>)

    List of word entries



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kotoshu/readers/dic_reader.rb', line 170

def read(source = nil)
  owned = source.nil?
  reader = source || FileReader.new(@path, @encoding)

  begin
    words = []
    first_line = true
    expected_count = 0

    reader.each do |_line_no, line|
      if first_line
        # First line is word count
        expected_count = line.to_i
        first_line = false
        next
      end

      # Skip empty lines
      next if line.empty?

      # Parse word
      word = Word.from_line(line, flag_format: @flag_format, flag_synonyms: @flag_synonyms)
      words << word
    end

    # Verify word count
    # Note: We don't raise an error if count doesn't match, as some dictionaries have different formats

    words
  ensure
    # Close the reader only when we created it. If the caller passed
    # one in, they own its lifecycle. Without this, the underlying
    # File handle leaks (on Windows it also blocks tempfile cleanup).
    reader.close if owned
  end
end