Class: Kotoshu::MultiLanguageChecker

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

Overview

Multi-language document checker.

Splits a Documents::Document into language-tagged segments via Language::Segmenter, resolves a Spellchecker for each segment's detected language, and emits a flat list of Kotoshu::Models::SemanticErrors carrying source_range from the original document. The result is one sorted stream of errors that editors / plugins can highlight without caring that the document mixed languages.

Real documents mix languages routinely: code comments in English embedded in French prose, a Japanese paper with an English abstract, German quotes in a Spanish essay. Without per-segment detection, every word from the "other" language is reported as a spelling error against the wrong dictionary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segmenter: nil) ⇒ MultiLanguageChecker

Returns a new instance of MultiLanguageChecker.

Parameters:

  • segmenter (Language::Segmenter, nil) (defaults to: nil)

    defaults to a new Segmenter with the default Detector



29
30
31
32
# File 'lib/kotoshu/multi_language_checker.rb', line 29

def initialize(segmenter: nil)
  @segmenter = segmenter || Language::Segmenter.new
  @spellcheckers = {}
end

Instance Attribute Details

#segmenterLanguage::Segmenter (readonly)

Returns:



21
22
23
# File 'lib/kotoshu/multi_language_checker.rb', line 21

def segmenter
  @segmenter
end

#spellcheckersHash{String => Spellchecker} (readonly)

Returns per-language cache so repeated segments of the same language reuse the same checker.

Returns:

  • (Hash{String => Spellchecker})

    per-language cache so repeated segments of the same language reuse the same checker



25
26
27
# File 'lib/kotoshu/multi_language_checker.rb', line 25

def spellcheckers
  @spellcheckers
end

Instance Method Details

#check(document) ⇒ Array<Models::SemanticError>

Check a document, returning one sorted stream of errors across every detected language. Errors carry source_range resolved via the document's text-node mapping.

Parameters:

Returns:



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

def check(document)
  unless document.is_a?(Kotoshu::Documents::Document)
    raise ArgumentError,
          "document must be a Kotoshu::Documents::Document"
  end

  @segmenter.segment(document).flat_map do |segment|
    check_segment(segment, document)
  end.sort
end