class Opener::OpinionDetectorBasic::Kaf::Document
Attributes
document[RW]
opinion_strength[RW]
pretty[RW]
timestamp[RW]
Public Class Methods
new(file, options = {})
click to toggle source
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 8 def initialize file, options = {} @document = Nokogiri.XML file @timestamp = options[:timestamp] @opinion_strength = options[:opinion_strength] @pretty = options[:pretty] || false raise 'Error parsing input. Input is required to be KAF' unless is_kaf? end
Public Instance Methods
add_linguistic_processor()
click to toggle source
Add linguistic processor layer with basic information (version, timestamp, description etc) in the KAF file.
# 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 timestamp 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)
click to toggle source
Adds the entire opinion in the KAF file.
# 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)
click to toggle source
Method for adding opinion holders, targets and expressions.
# 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()
click to toggle source
Remove the opinions layer from the KAF file if it exists and add a new one.
# 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?()
click to toggle source
Check if input is a KAF file. @return [Boolean]
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 135 def is_kaf? !!document.at_xpath('KAF') end
language()
click to toggle source
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 24 def language @language ||= document.at_xpath('KAF').attr('xml:lang') end
method_missing(method, *args, &block)
click to toggle source
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 139 def method_missing method, *args, &block @document.send method, *args, &block end
new_node(tag, parent)
click to toggle source
Creates a new node in the KAF file.
# 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()
click to toggle source
Get terms grouped by sentence.
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 31 def sentences @sentences ||= terms.group_by{ |t| t.sentence } end
terms()
click to toggle source
# File lib/opener/opinion_detector_basic/kaf/document.rb, line 18 def terms @terms ||= document.xpath('KAF/terms/term').map do |term| Term.new term, self, language end end