Class: GoogleTranslateProcessor
- Defined in:
- lib/Processors/GoogleTranslateProcessor.rb
Instance Attribute Summary collapse
-
#googleAPI ⇒ Object
Returns the value of attribute googleAPI.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#targetLang ⇒ Object
Returns the value of attribute targetLang.
-
#territoriesExclude ⇒ Object
Returns the value of attribute territoriesExclude.
-
#token ⇒ Object
Returns the value of attribute token.
Attributes inherited from Processor
#baseExecutePath, #config, #configFilePath
Instance Method Summary collapse
-
#initialize(config, configFilePath, baseExecutePath) ⇒ GoogleTranslateProcessor
constructor
A new instance of GoogleTranslateProcessor.
- #processReviews(reviews, platform) ⇒ Object
Constructor Details
#initialize(config, configFilePath, baseExecutePath) ⇒ GoogleTranslateProcessor
Returns a new instance of GoogleTranslateProcessor.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 13 def initialize(config, configFilePath, baseExecutePath) @config = config @configFilePath = configFilePath @baseExecutePath = baseExecutePath @logger = ZLogger.new(baseExecutePath) keyFilePath = Helper.unwrapRequiredParameter(config, "googleTranslateAPIKeyFilePath") if Pathname.new(keyFilePath).absolute? configDir = File.dirname(configFilePath) keyFilePath = "#{configDir}#{keyFilePath}" end @googleAPI = GoogleAPI.new(keyFilePath, baseExecutePath, ["https://www.googleapis.com/auth/cloud-translation","https://www.googleapis.com/auth/cloud-platform"]) @targetLang = Helper.unwrapRequiredParameter(config, "googleTranslateTargetLang") @territoriesExclude = [] if !config['googleTranslateTerritoriesExclude'].nil? && config['googleTranslateTerritoriesExclude'].length > 0 @territoriesExclude = config['googleTranslateTerritoriesExclude'] end puts "[GoogleTranslateProcessor] Init Success." end |
Instance Attribute Details
#googleAPI ⇒ Object
Returns the value of attribute googleAPI.
11 12 13 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 11 def googleAPI @googleAPI end |
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 11 def logger @logger end |
#targetLang ⇒ Object
Returns the value of attribute targetLang.
11 12 13 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 11 def targetLang @targetLang end |
#territoriesExclude ⇒ Object
Returns the value of attribute territoriesExclude.
11 12 13 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 11 def territoriesExclude @territoriesExclude end |
#token ⇒ Object
Returns the value of attribute token.
11 12 13 |
# File 'lib/Processors/GoogleTranslateProcessor.rb', line 11 def token @token end |
Instance Method Details
#processReviews(reviews, platform) ⇒ Object
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/Processors/GoogleTranslateProcessor.rb', line 37 def processReviews(reviews, platform) if reviews.length < 1 return reviews end reviews.each_index do |index| if territoriesExclude.include? reviews[index].territory next end puts "[GoogleTranslateProcessor] translate #{reviews[index].body} from #{reviews[index].territory} to #{targetLang}" if !reviews[index].title.nil? translateTitle = googleAPI.request("https://translation.googleapis.com/language/translate/v2", "POST", {:q => reviews[index].title, :target => targetLang}) translateTitle = translateTitle&.dig("data", "translations", 0, "translatedText") if !translateTitle.nil? && translateTitle != reviews[index].title reviews[index].title = "#{translateTitle} (#{reviews[index].title})" end end if !reviews[index].body.nil? translateBody = googleAPI.request("https://translation.googleapis.com/language/translate/v2", "POST", {:q => reviews[index].body, :target => targetLang}) translateBody = translateBody&.dig("data", "translations", 0, "translatedText") if !translateBody.nil? && translateBody != reviews[index].body body = "#{translateBody}" body += "\r\n===== Translate by Google =====\r\n" body += reviews[index].body reviews[index].body = body end end end return reviews end |