Class: Kotoshu::Readers::DicReader
- Inherits:
-
Object
- Object
- Kotoshu::Readers::DicReader
- 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.
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#flag_format ⇒ Object
readonly
Returns the value of attribute flag_format.
-
#flag_synonyms ⇒ Object
readonly
Returns the value of attribute flag_synonyms.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, encoding: 'UTF-8', flag_format: 'short', flag_synonyms: {}) ⇒ DicReader
constructor
Create a new DIC reader.
-
#read(source = nil) ⇒ Array<Word>
Read the dic file and return a list of Word entries.
Constructor Details
#initialize(path, encoding: 'UTF-8', flag_format: 'short', flag_synonyms: {}) ⇒ DicReader
Create a new DIC reader.
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
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
151 152 153 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 151 def encoding @encoding end |
#flag_format ⇒ Object (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_synonyms ⇒ Object (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 |
#path ⇒ Object (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.
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 |