Class: LangExtract::Core::FuzzyAlignmentPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/langextract/core/fuzzy_alignment_policy.rb

Overview

Validates semantic constraints that depend on the target-to-source token mapping rather than only the selected source interval.

Defined Under Namespace

Classes: MappingState

Constant Summary collapse

NEGATION_TOKENS =
%w[
  aint arent cannot cant couldnt deny denied denies denying didnt doesnt
  dont hadnt hardly hasnt havent isnt mustnt neither neednt never no none
  not shouldnt shant wasnt werent without wont wouldnt
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_tokens:, fuzzy_threshold:) ⇒ FuzzyAlignmentPolicy

Returns a new instance of FuzzyAlignmentPolicy.



28
29
30
31
# File 'lib/langextract/core/fuzzy_alignment_policy.rb', line 28

def initialize(source_tokens:, fuzzy_threshold:)
  @source_tokens = source_tokens
  @fuzzy_threshold = fuzzy_threshold
end

Class Method Details

.negation_token?(text) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/langextract/core/fuzzy_alignment_policy.rb', line 24

def self.negation_token?(text)
  NEGATION_TOKENS.include?(TokenSimilarity.normalize(text))
end

Instance Method Details

#score_for(target_tokens, source_indices) ⇒ Object

Returns the best semantically valid average score, or nil when the selected source tokens cannot be mapped safely to the target tokens.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/langextract/core/fuzzy_alignment_policy.rb', line 35

def score_for(target_tokens, source_indices)
  return nil if target_tokens.empty? || source_indices.empty?

  states = [initial_state]
  source_indices.each do |source_index|
    states = advance_states(states, target_tokens, source_tokens.fetch(source_index).text)
    return nil if states.empty?
  end

  best_valid_score(states, required_negation_mask(target_tokens), source_indices.length)
end