Class: Opener::OpinionDetectorBasic::Kaf::Document
- Inherits:
-
Object
- Object
- Opener::OpinionDetectorBasic::Kaf::Document
- Defined in:
- lib/opener/opinion_detector_basic/kaf/document.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#opinion_strength ⇒ Object
Returns the value of attribute opinion_strength.
-
#pretty ⇒ Object
Returns the value of attribute pretty.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#add_linguistic_processor ⇒ Object
Add linguistic processor layer with basic information (version, timestamp, description etc) in the KAF file.
-
#add_opinion(opinion, index) ⇒ Object
Adds the entire opinion in the KAF file.
-
#add_opinion_element(node, ids) ⇒ Object
Method for adding opinion holders, targets and expressions.
-
#add_opinions_layer ⇒ Object
Remove the opinions layer from the KAF file if it exists and add a new one.
-
#initialize(file, options = {}) ⇒ Document
constructor
A new instance of Document.
-
#is_kaf? ⇒ Boolean
Check if input is a KAF file.
- #language ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
-
#new_node(tag, parent) ⇒ Object
Creates a new node in the KAF file.
-
#sentences ⇒ Object
Get terms grouped by sentence.
- #terms ⇒ Object
Constructor Details
#initialize(file, options = {}) ⇒ Document
Returns a new instance of Document.
8 9 10 11 12 13 14 15 16 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 8 def initialize file, = {} @document = Nokogiri.XML file @timestamp = [:timestamp] @opinion_strength = [:opinion_strength] @pretty = [:pretty] || false raise 'Error parsing input. Input is required to be KAF' unless is_kaf? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
139 140 141 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 139 def method_missing method, *args, &block @document.send method, *args, &block end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
6 7 8 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 6 def document @document end |
#opinion_strength ⇒ Object
Returns the value of attribute opinion_strength.
6 7 8 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 6 def opinion_strength @opinion_strength end |
#pretty ⇒ Object
Returns the value of attribute pretty.
6 7 8 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 6 def pretty @pretty end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
6 7 8 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 6 def @timestamp end |
Instance Method Details
#add_linguistic_processor ⇒ Object
Add linguistic processor layer with basic information (version, timestamp, description etc) in the KAF file.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 92 def add_linguistic_processor description = 'Basic opinion detector with Pos' last_edited = '13may2015' version = '2.0' node = new_node('linguisticProcessors', 'KAF/kafHeader') node['layer'] = 'opinions' lp_node = new_node('lp', node) lp_node['version'] = "#{last_edited}-#{version}" lp_node['name'] = description if format = '%Y-%m-%dT%H:%M:%S%Z' lp_node['timestamp'] = Time.now.strftime(format) else lp_node['timestamp'] = '*' end end |
#add_opinion(opinion, index) ⇒ Object
Adds the entire opinion in the KAF file.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 38 def add_opinion opinion, index opinion_node = new_node 'opinion', 'KAF/opinions' opinion_node['oid'] = "o#{index.to_s}" if opinion.holders.present? opinion_holder_node = new_node 'opinion_holder', opinion_node add_opinion_element opinion_holder_node, opinion.holders end opinion_target_node = new_node 'opinion_target', opinion_node if opinion.target_ids.present? add_opinion_element opinion_target_node, opinion.target_ids end expression_node = new_node 'opinion_expression', opinion_node expression_node['polarity'] = opinion.polarity expression_node['strength'] = opinion.strength.to_s expression_node['lexicon-id'] = opinion.lexicon_id if opinion.lexicon_id add_opinion_element expression_node, opinion.ids end |
#add_opinion_element(node, ids) ⇒ Object
Method for adding opinion holders, targets and expressions.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 75 def add_opinion_element node, ids lemmas = terms.select{|t| ids.include?(t.id)}.map(&:lemma).join(' ') comment = Nokogiri::XML::Comment.new(document, lemmas) node.add_child comment span_node = new_node('span', node) ids.each do |id| target_node = new_node('target', span_node) target_node['id'] = id.to_s end end |
#add_opinions_layer ⇒ Object
Remove the opinions layer from the KAF file if it exists and add a new one.
64 65 66 67 68 69 70 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 64 def add_opinions_layer existing = document.at_xpath('KAF/opinions') existing.remove if existing new_node 'opinions', 'KAF' end |
#is_kaf? ⇒ Boolean
Check if input is a KAF file.
135 136 137 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 135 def is_kaf? !!document.at_xpath('KAF') end |
#language ⇒ Object
24 25 26 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 24 def language @language ||= document.at_xpath('KAF').attr('xml:lang') end |
#new_node(tag, parent) ⇒ Object
Creates a new node in the KAF file.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 117 def new_node tag, parent if parent.is_a?(String) parent_node = document.at_xpath(parent) else parent_node = parent end node = Nokogiri::XML::Element.new(tag, document) parent_node.add_child node node end |
#sentences ⇒ Object
Get terms grouped by sentence.
31 32 33 |
# File 'lib/opener/opinion_detector_basic/kaf/document.rb', line 31 def sentences @sentences ||= terms.group_by{ |t| t.sentence } end |