Class: Kotoshu::Readers::LookupBuilder

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

Overview

Builder for creating Lookup::Lookuper instances from Hunspell data.

This class can either read from files or accept pre-read aff/dic data.

Examples:

Building a lookuper from files

builder = LookupBuilder.new('en_US.aff', 'en_US.dic')
lookuper = builder.build

Building a lookuper from pre-read data

aff_reader = AffReader.new('en_US.aff')
aff_data = aff_reader.read
dic_reader = DicReader.new('en_US.dic')
words = dic_reader.read
builder = LookupBuilder.from_data(aff_data, words)
lookuper = builder.build

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aff_path, dic_path, encoding: 'UTF-8', script: :latin) ⇒ LookupBuilder

Create a new LookupBuilder from file paths.

Parameters:

  • aff_path (String)

    Path to the .aff file

  • dic_path (String)

    Path to the .dic file

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

    File encoding (default: ‘UTF-8’)

  • script (Symbol) (defaults to: :latin)

    The script type for condition checking (default: :latin)



35
36
37
38
39
40
41
42
# File 'lib/kotoshu/readers/lookup_builder.rb', line 35

def initialize(aff_path, dic_path, encoding: 'UTF-8', script: :latin)
  @aff_path = aff_path
  @dic_path = dic_path
  @encoding = encoding
  @script = script
  @aff_data = nil
  @words = nil
end

Instance Attribute Details

#aff_dataObject (readonly)

Returns the value of attribute aff_data.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def aff_data
  @aff_data
end

#aff_pathObject (readonly)

Returns the value of attribute aff_path.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def aff_path
  @aff_path
end

#dic_pathObject (readonly)

Returns the value of attribute dic_path.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def dic_path
  @dic_path
end

#encodingObject (readonly)

Returns the value of attribute encoding.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def encoding
  @encoding
end

#scriptObject (readonly)

Returns the value of attribute script.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def script
  @script
end

#wordsObject (readonly)

Returns the value of attribute words.



27
28
29
# File 'lib/kotoshu/readers/lookup_builder.rb', line 27

def words
  @words
end

Class Method Details

.from_data(aff_data, words) ⇒ LookupBuilder

Create a new LookupBuilder from pre-read data.

Parameters:

  • aff_data (Hash)

    Raw aff data from AffReader

  • words (Array<Word>)

    Word entries from DicReader

Returns:



49
50
51
52
53
54
# File 'lib/kotoshu/readers/lookup_builder.rb', line 49

def self.from_data(aff_data, words)
  builder = new(nil, nil)
  builder.instance_variable_set(:@aff_data, aff_data)
  builder.instance_variable_set(:@words, words)
  builder
end

Instance Method Details

#buildAlgorithms::Lookup::Lookuper

Build the Lookuper instance.

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kotoshu/readers/lookup_builder.rb', line 59

def build
  # Read files if data not already provided
  aff_data_to_use = @aff_data || read_aff_data
  words_to_use = @words || read_dic_data(aff_data_to_use)

  # Build the aff structure for Lookuper
  aff = build_aff_structure(aff_data_to_use)

  # Build the dic structure for Lookuper
  dic = build_dic_structure(words_to_use)

  # Create and return the Lookuper
  Algorithms::Lookup::Lookuper.new(aff, dic)
end