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

Constant Summary collapse

DEFAULT_MAX_NGRAM_SUGS =

Hunspell upstream defaults for suggester directives. These apply when the .aff file is silent on the directive. See the Hunspell documentation at https://hunspell.github.io/ and the spylls Aff defaults for reference.

4
DEFAULT_MAX_CPD_SUGS =
3
DEFAULT_MAX_DIFF =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new LookupBuilder from file paths.

Parameters:

  • aff_path (String, nil)

    Path to the .aff file (nil when constructing via .from_data with pre-read aff/dic data)

  • dic_path (String, nil)

    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)

  • aff_data (Hash, nil) (defaults to: nil)

    Pre-read aff data (skips file read in #build when provided)

  • words (Array<Word>, nil) (defaults to: nil)

    Pre-read word entries



41
42
43
44
45
46
47
48
49
# File 'lib/kotoshu/readers/lookup_builder.rb', line 41

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

Instance Attribute Details

#aff_dataObject (readonly)

Returns the value of attribute aff_data.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

def aff_data
  @aff_data
end

#aff_pathObject (readonly)

Returns the value of attribute aff_path.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

def aff_path
  @aff_path
end

#dic_pathObject (readonly)

Returns the value of attribute dic_path.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

def dic_path
  @dic_path
end

#encodingObject (readonly)

Returns the value of attribute encoding.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

def encoding
  @encoding
end

#scriptObject (readonly)

Returns the value of attribute script.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

def script
  @script
end

#wordsObject (readonly)

Returns the value of attribute words.



29
30
31
# File 'lib/kotoshu/readers/lookup_builder.rb', line 29

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:



56
57
58
# File 'lib/kotoshu/readers/lookup_builder.rb', line 56

def self.from_data(aff_data, words)
  new(nil, nil, aff_data: aff_data, words: words)
end

Instance Method Details

#buildAlgorithms::Lookup::Lookuper

Build the Lookuper instance.

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kotoshu/readers/lookup_builder.rb', line 63

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. Dictionary `ph:` morph data
  # is a source of REP entries (Hunspell 1.7+), so build_dic_structure
  # also enriches aff[:REP] — see PhRepExtractor.
  dic = build_dic_structure(words_to_use, aff: aff)

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