Class: Opener::OpinionDetectorBasic::Kaf::Term
- Inherits:
-
Object
- Object
- Opener::OpinionDetectorBasic::Kaf::Term
- 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
-
#accumulated_strength ⇒ Object
Returns the value of attribute accumulated_strength.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#is_conjunction ⇒ Object
readonly
Returns the value of attribute is_conjunction.
-
#list_ids ⇒ Object
Returns the value of attribute list_ids.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#sentence ⇒ Object
readonly
Returns the value of attribute sentence.
-
#use ⇒ Object
Returns the value of attribute use.
Instance Method Summary collapse
-
#get_sentence(document) ⇒ String
Returns the sentence id that the term belongs to in the document.
-
#head ⇒ String
Returns the head of the term.
- #head_term ⇒ Object
-
#id ⇒ String
Returns the term id.
-
#initialize(node, document, language) ⇒ Term
constructor
A new instance of Term.
-
#is_conjunction?(language) ⇒ Boolean
Checks if a term is a conjunction.
-
#is_expression? ⇒ TrueClass|FalseClass
Checks if a term is an expression.
-
#is_intensifier? ⇒ TrueClass|FalseClass
Checks if a term is an intensifier.
- #is_punct? ⇒ Boolean
-
#is_shifter? ⇒ TrueClass|FalseClass
Checks if a term is a shifter.
-
#lemma ⇒ String
Returns the lemma of the term.
- #lexicon_id ⇒ Object
-
#polarity ⇒ String|NilClass
Returns the polarity of the term if it exists.
-
#pos ⇒ String
Returns the part of speech of the term.
- #root? ⇒ Boolean
-
#sentiment_modifier ⇒ String|NilClass
Returns the sentiment modifier type if it exists.
-
#strength ⇒ Integer
Returns the strength of the term depending on its type.
-
#target_ids ⇒ Array
Returns the actual word ids that construct the lemma.
- #xpos ⇒ Object
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_strength ⇒ Object
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 |
#document ⇒ Object (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_conjunction ⇒ Object (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_ids ⇒ Object
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 |
#node ⇒ Object (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 |
#sentence ⇒ Object (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 |
#use ⇒ Object
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.
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 |
#head ⇒ String
Returns the head of the term.
56 57 58 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 56 def head @head ||= node.attr(:head).to_i end |
#head_term ⇒ Object
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 |
#id ⇒ String
Returns the term id.
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
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.
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.
145 146 147 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 145 def is_intensifier? sentiment_modifier == 'intensifier' end |
#is_punct? ⇒ 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.
154 155 156 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 154 def is_shifter? sentiment_modifier == 'shifter' end |
#lemma ⇒ String
Returns the lemma of the term.
47 48 49 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 47 def lemma @lemma ||= node.attr :lemma end |
#lexicon_id ⇒ Object
82 83 84 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 82 def lexicon_id @lexicon_id ||= node.attr('lexicon-id') end |
#polarity ⇒ String|NilClass
Returns the polarity of the term if it exists.
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 |
#pos ⇒ String
Returns the part of speech of the term.
74 75 76 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 74 def pos @pos ||= node.attr('pos') end |
#root? ⇒ Boolean
65 66 67 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 65 def root? head == 0 end |
#sentiment_modifier ⇒ String|NilClass
Returns the sentiment modifier type if it exists.
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 |
#strength ⇒ Integer
Returns the strength of the term depending on its type.
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_ids ⇒ Array
Returns the actual word ids that construct the lemma.
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 |
#xpos ⇒ Object
78 79 80 |
# File 'lib/opener/opinion_detector_basic/kaf/term.rb', line 78 def xpos @xpos ||= node.attr('xpos') end |