Class: Opener::ChainedDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/chained_daemon/cli.rb,
lib/opener/chained_daemon/version.rb,
lib/opener/chained_daemon/webservice.rb,
lib/opener/chained_daemon/chained_daemon.rb,
lib/opener/chained_daemon/languages_cache.rb,
lib/opener/chained_daemon/microsoft_translator.rb

Defined Under Namespace

Classes: CLI, LanguagesCache, MicrosoftTranslator, Webservice

Constant Summary collapse

VERSION =
'3.3.26'
DEFAULT_OPTIONS =
{
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ChainedDaemon

Returns a new instance of ChainedDaemon.



15
16
17
18
19
20
21
22
23
24
# File 'lib/opener/chained_daemon/chained_daemon.rb', line 15

def initialize options = {}
  @options   = DEFAULT_OPTIONS.merge options
  @queue_map = {
    'opener-language-identifier':    Opener::LanguageIdentifier.new,
    'stanza-processor':              Stanza::Processor.new,
    'opener-property-tagger':        Opener::PropertyTagger.new,
    'opener-polarity-tagger':        Opener::PolarityTagger.new,
    'opener-opinion-detector-basic': Opener::OpinionDetectorBasic.new,
  }
end

Class Method Details

.httpObject



4
5
6
7
8
9
10
# File 'lib/opener/chained_daemon/chained_daemon.rb', line 4

def self.http
  http = HTTPClient.new
  http.send_timeout    = 600
  http.receive_timeout = 600
  http.connect_timeout = 600
  http
end

Instance Method Details

#pretty_print(output) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/opener/chained_daemon/chained_daemon.rb', line 72

def pretty_print(output)
  doc = REXML::Document.new output
  doc.context[:attribute_quote] = :quote
  out = ""
  formatter = REXML::Formatters::Pretty.new
  formatter.compact = true
  formatter.write(doc, out)

  out.strip
end

#run(input, _params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/opener/chained_daemon/chained_daemon.rb', line 26

def run input, _params = {}
  params = SymMash.new _params
  params.reject!{ |k,v| v.blank? }
  params.cache_keys&.reject!{ |k,v| v.blank? }

  params.cache_keys = SymMash.new params.cache_keys&.to_h&.sort&.to_h || {}
  if params.filter_vertical and params.property_type.present?
    params.cache_keys.property_type = params.property_type
  end
  params.cache_keys.contract_ids = nil unless params.cache_keys.contract_ids

  params.cache_keys.environment ||= 'production'
  params.translate_languages    ||= []

  lang     = nil
  output   = nil
  @queue_map.each do |queue, component|
    debug_print queue, input if ENV['DEBUG']

    output = component.run input, params
    input  = output

  rescue Core::UnsupportedLanguageError
    xml  = Nokogiri.parse input
    lang = xml.root.attr('xml:lang')
    raise unless lang.in? params.translate_languages

    input = translate xml, params
    retry
  end

  if lang
    # put back original language
    xml    = Nokogiri.parse output
    xml.root.attributes['lang'].value = lang
    output = xml.to_s
  end

  output = pretty_print output if params.cache_keys.environment == 'staging'
  output

rescue Core::UnsupportedLanguageError
  puts "Error on unsupported language: #{input}" if ENV['DEBUG']
  output
end

#translate(xml, params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/opener/chained_daemon/chained_daemon.rb', line 83

def translate xml, params
  raw = xml.at :raw
  case translate_service params
  when :google
    raw.content = google_translator.translate raw.content, to: :en
  when :microsoft
    raw.content = microsoft_translator.translate raw.content, to: :en
  else
    raw.content = google_translator.translate raw.content, to: :en
  end

  xml.root.attributes['lang'].value = 'en'
  xml.to_s
end