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 ⇒ 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.
78 79 80 81 82 83 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 78 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.
70 71 72 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 70 def encoding @encoding end |
#flag_format ⇒ Object (readonly)
Returns the value of attribute flag_format.
70 71 72 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 70 def flag_format @flag_format end |
#flag_synonyms ⇒ Object (readonly)
Returns the value of attribute flag_synonyms.
70 71 72 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 70 def flag_synonyms @flag_synonyms end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
70 71 72 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 70 def path @path end |
Instance Method Details
#read ⇒ Array<Word>
Read the dic file and return a list of Word entries.
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 114 115 |
# File 'lib/kotoshu/readers/dic_reader.rb', line 88 def read reader = FileReader.new(@path, @encoding) 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 end |