class Opener::OpinionDetectorBasic::Kaf::Opinion

Constants

OPINION_HOLDERS

Opinion holders for each language code.

Attributes

holders[RW]
left_candidates[RW]
right_candidates[RW]
target_ids[RW]
term[R]

Public Class Methods

new(term) click to toggle source
# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 24
def initialize term
  @term       = term
  @holders    = []
  @target_ids = []

  @left_candidates  = []
  @right_candidates = []
end

Public Instance Methods

ids() click to toggle source

Returns the term ids of the opinion expression.

@return [Array]

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 38
def ids
  @ids ||= term.list_ids.sort
end
lexicon_id() click to toggle source
# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 60
def lexicon_id
  @lexicon_id ||= term.lexicon_id
end
obtain_holders(sentences, language) click to toggle source

Obtain the opinion holders from the terms that belong to the same sentence.

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 83
def obtain_holders(sentences, language)
  sentence_terms = sentences[sentence]
  sentence_terms.each do |term|
    if OPINION_HOLDERS[language]&.include?(term.lemma)
      @holders << term.id
      break
    end
  end
end
obtain_targets(sentences) click to toggle source

Get the potential right and left candidates of the sentence and decide which ones are the actual targets of the opinion

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 97
def obtain_targets(sentences)
  sentence_terms = sentences[sentence]
  max_distance = 3
  terms_count = sentence_terms.count

  index = -1
  sentence_terms.each_with_index do |term, i|
    if ids.include?(term.id)
      index = i
    end
  end

  unless index+1 >= terms_count
    min = index+1
    max = [index+1+max_distance,terms_count].min
    @right_candidates = filter_candidates(sentence_terms[min..max])
  end

  index = 0
  sentence_terms.each_with_index do |term, i|
    if ids.include?(term.id)
      index = i
      break # needed for left_candidates
    end
  end

  unless index == 0
    min = [0, index-1-max_distance].max
    max = index
    @left_candidates = filter_candidates(sentence_terms[min..max])
  end

  if right_candidates.any?
    @target_ids << right_candidates.first.id
  end
  if left_candidates.any?
    @target_ids << left_candidates.last.id
  end
end
polarity() click to toggle source

Returns the polarity of the opinion.

@return [String]

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 69
def polarity
  @polarity ||= if strength > 0
    'positive'
  elsif strength < 0
    'negative'
  else
    'neutral'
  end
end
sentence() click to toggle source

Returns the sentence id of the opinion.

@return [String]

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 47
def sentence
  @sentence ||= term.sentence
end
strength() click to toggle source

Returns the strength of the opinion.

@return [Integer]

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 56
def strength
  @strength ||= term.accumulated_strength
end

Protected Instance Methods

filter_candidates(sentence_terms) click to toggle source

Filters candidate terms depending on their part of speech and if they are already part of the expression.

@return [Hash]

# File lib/opener/opinion_detector_basic/kaf/opinion.rb, line 145
def filter_candidates sentence_terms
  sentence_terms.select{|t| (t.pos == 'N' || t.pos == 'R') && !ids.include?(t.id)}
end