Class: Opener::OpinionDetectorBasic::Kaf::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/opinion_detector_basic/kaf/term.rb

Constant Summary collapse

CONJUNCTIONS =

Map of conjunctions per language code Deprecated

{
  'nl' => %w{, en},
  'en' => %w{, and},
  'es' => %w{, y e},
  'pt' => %w{, e},
  'it' => %w{, e ed},
  'de' => %w{, und},
  'fr' => %w{, et},
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, document, language) ⇒ Term

Returns a new instance of Term.



23
24
25
26
27
28
29
30
31
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 23

def initialize node, document, language
  @document             = document
  @node                 = node
  @sentence             = get_sentence document
  @use                  = true
  @accumulated_strength = strength
  @list_ids             = [id]
  @is_conjunction       = is_conjunction? language
end

Instance Attribute Details

#accumulated_strengthObject

Returns the value of attribute accumulated_strength.



9
10
11
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 9

def accumulated_strength
  @accumulated_strength
end

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 6

def document
  @document
end

#is_conjunctionObject (readonly)

Returns the value of attribute is_conjunction.



7
8
9
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 7

def is_conjunction
  @is_conjunction
end

#list_idsObject

Returns the value of attribute list_ids.



9
10
11
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 9

def list_ids
  @list_ids
end

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 7

def node
  @node
end

#sentenceObject (readonly)

Returns the value of attribute sentence.



7
8
9
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 7

def sentence
  @sentence
end

#useObject

Returns the value of attribute use.



9
10
11
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 9

def use
  @use
end

Instance Method Details

#get_sentence(document) ⇒ String

Returns the sentence id that the term belongs to in the document.

Returns:

  • (String)


133
134
135
136
137
138
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 133

def get_sentence(document)
  document
  .xpath("KAF/text/wf[@wid='#{target_ids.first}']")
  .first
  .attr('sent')
end

#headString

Returns the head of the term.

Returns:

  • (String)


56
57
58
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 56

def head
  @head ||= node.attr(:head).to_i
end

#head_termObject



60
61
62
63
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 60

def head_term
  return if root?
  document.terms[head-1]
end

#idString

Returns the term id.

Returns:

  • (String)


38
39
40
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 38

def id
  @id ||= node.attr :tid
end

#is_conjunction?(language) ⇒ Boolean

Checks if a term is a conjunction. Comma is identified as conjunction by default Sometimes, comma comes with space after it

Returns:

  • (Boolean)


172
173
174
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 172

def is_conjunction?(language)
  pos == 'J' || xpos == ',' || lemma == ',' || CONJUNCTIONS[language]&.include?(lemma)
end

#is_expression?TrueClass|FalseClass

Checks if a term is an expression.

Returns:

  • (TrueClass|FalseClass)


163
164
165
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 163

def is_expression?
  use && !!polarity
end

#is_intensifier?TrueClass|FalseClass

Checks if a term is an intensifier.

Returns:

  • (TrueClass|FalseClass)


145
146
147
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 145

def is_intensifier?
  sentiment_modifier == 'intensifier'
end

#is_punct?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 176

def is_punct?
  pos == '.' || lemma == '-' || xpos == ',' || lemma == ','
end

#is_shifter?TrueClass|FalseClass

Checks if a term is a shifter.

Returns:

  • (TrueClass|FalseClass)


154
155
156
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 154

def is_shifter?
  sentiment_modifier == 'shifter'
end

#lemmaString

Returns the lemma of the term.

Returns:

  • (String)


47
48
49
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 47

def lemma
  @lemma ||= node.attr :lemma
end

#lexicon_idObject



82
83
84
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 82

def lexicon_id
  @lexicon_id ||= node.attr('lexicon-id')
end

#polarityString|NilClass

Returns the polarity of the term if it exists.

Returns:

  • (String|NilClass)


101
102
103
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 101

def polarity
  @polarity ||= first_sentiment ? first_sentiment.attr('polarity') : nil
end

#posString

Returns the part of speech of the term.

Returns:

  • (String)


74
75
76
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 74

def pos
  @pos ||= node.attr('pos')
end

#root?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 65

def root?
  head == 0
end

#sentiment_modifierString|NilClass

Returns the sentiment modifier type if it exists.

Returns:

  • (String|NilClass)


91
92
93
94
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 91

def sentiment_modifier
  @sentiment_modifier ||=
    first_sentiment ? first_sentiment.attr('sentiment_modifier') : nil
end

#strengthInteger

Returns the strength of the term depending on its type.

Returns:

  • (Integer)


120
121
122
123
124
125
126
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 120

def strength
  return  1 if polarity == 'positive'
  return -1 if polarity == 'negative'
  return  2 if is_intensifier?
  return -1 if is_shifter?
  return  0
end

#target_idsArray

Returns the actual word ids that construct the lemma.

Returns:

  • (Array)


110
111
112
113
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 110

def target_ids
  @target_ids ||= node.xpath('span/target')
    .map { |target| target.attr('id') }
end

#xposObject



78
79
80
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 78

def xpos
  @xpos ||= node.attr('xpos')
end