GithubHelp home page GithubHelp logo

relrod / bing_translator-gem Goto Github PK

View Code? Open in Web Editor NEW
111.0 8.0 55.0 147 KB

A simple Ruby interface to Bing's translation API.

Home Page: https://rubygems.org/gems/bing_translator

License: MIT License

Ruby 93.53% HTML 6.47%

bing_translator-gem's Introduction

Bing Translator

Gem bing_translator tests

This gem wraps the Microsoft Cognitive Services Translator API.

Installation

To use this rubygem:

$ sudo gem install bing_translator

With bundler:

gem "bing_translator", "~> 6.2.0"

Changelog

Information

Documentation on the Microsoft Translator API is here

bing_translator is also smart about requesting the token, and handles this behind the scenes. It will only request a token if it knows the old one expired (X seconds from when we requested the last token, where X is given to us when we make the request. As of this writing, X is consistently 8 minutes).

Getting a free Azure account

To be able to use the API freely, do the following:

  1. Go here
  2. Sign in with valid Live credentials.
  3. Add the resource 'Cognitive Services APIs'
  4. In 'RESOURCE MANAGEMENT > Keys' pick either 'KEY 1' or 'KEY 2'

Usage

require 'rubygems'
require 'bing_translator'

translator = BingTranslator.new('COGNITIVE_SUBSCRIPTION_KEY')

# Translation

spanish = translator.translate('Hello. This will be translated!', :from => 'en', :to => 'es')
spanish = translator.translate('Hello. This will be translated!', :to => 'es')

# HTML Translations
spanish_html = translator.translate('<b>Hello</b>', to: 'es', textType: 'html')

# Translation of multiple strings

result = translator.translate_array(['Hello. This will be translated!', 'This will be translated too!'], :from => :en, :to => :fr)

# Translation of multiple strings, with word alignment information

result = translator.translate_array2(['Hello. This will be translated!', 'This will be translated too!'], :from => :en, :to => :fr)

# Language Detection

locale = translator.detect('Hello. This will be translated!') # => :en

Migration to API V3

Since version 6.0.0, this gem uses Microsoft Cognitive Translation Services in version 3.

Microsoft is dropping the support of Cognitive Translation Services Version 2 in April 2019. If you want to continue using this gem, migrate to 6.0.0.

I did my best to keep the backward compatibility with the previous gem version, but there are some breaking changes:

  • I dropped the support for the #speak method. If you need it, please create a GitHub issue, and I'll consider supporting it too.
  • I changed the interface for HTML translations. See the documentation above.
  • In the API v3, Microsoft does not allow translation of texts longer than 5000 characters.

bing_translator-gem's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bing_translator-gem's Issues

Are you releasing 5.0.0?

The 5.0.0 tag which uses the new COGNITIVE_SUBSCRIPTION_KEY isn't released. Is there plans to release this? The docs or page are useless as 4.6.0 seems obsolete.

Abbreviations?

Hello Rick! @relrod

Thanks for creating this. Do you have a list of all the abbreviations for the languages?

Also, can this easily be used to offer a 'translate' button in a forum, and when pressed, it asks which language you want it translated into, and then it translates the text?

Thanks!
-Monroe

Migration to API V3

Hey guys,

I'm not sure if you saw the emails Microsoft was sending out to their customers, but they' re going to discontinue the service of Bing translator V2 on April 30, 2019.

I'm wondering if you plan on getting this reflected in the Gem and switch to V3? I'm happy to help!

Best,
Kamil

Is there a quota imposed by MS?

BingTranslator::Exception: (a:DeserializationFailed) The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Translate'. The maximum string content length quota (30720) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 128, position 113.
from /Users/nguyenchiencong/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/bing_translator-4.3.0/lib/bing_translator.rb:133:in `rescue in result'

Error with savon version greater than 2.11.0

Hi @relrod , this gem looks really helpful to my app. However I ran into an error

Bundler could not find compatible versions for gem "savon":
In snapshot (Gemfile.lock):
savon (= 2.11.2)

In Gemfile:
savon (~> 2.11.0)

bing_translator (= 5.2.0) was resolved to 5.2.0, which depends on
  savon (~> 2.10.0)

It seems not compatible with savon 2.11.0 version or higher.

Thank you

Build is failing (missing key?)

The CI is failing on every test.

  1) BingTranslator translates text
     Failure/Error: result = translator.translate message_en, :from => :en, :to => :ru
     BingTranslator::Exception:
       (s:Client) ArgumentException: The received token is of incorrect token type. : ID=0243.V2_Soap.Translate.440CE7CC
     # ./lib/bing_translator.rb:152:in `rescue in result'
     # ./lib/bing_translator.rb:150:in `result'
     # ./lib/bing_translator.rb:38:in `translate'
     # ./spec/bing_translator_spec.rb:19:in `block (2 levels) in <top (required)>'

Is the Bing key missing from the CI or is there some other issue?

getaddrinfo: Name or service not known

I am processing a large number of words and using this brilliant gem to generate an audio recording for each. My code uses a delayed_job process to run.

I keep getting this error below - then if I restart it runs ok for a while before hitting it again. I guess it could be my code also but have you seen this before?

BingTranslator::Exception: getaddrinfo: Name or service not known
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:133:in `rescue in result'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:127:in `result'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:70:in `speak'
    from /home/fluent/fluent/app/workers/bing.rb:37:in `block in lookup_words'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
    from /home/fluent/fluent/app/workers/bing.rb:21:in `lookup_words'
    from (irb):7
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

This is the codeblock (below). I assume the error is coming from Bing itself... but I don't understand what the issue is.

# Public: performs actual request to Bing Translator SOAP API
  def result(action, params = {}, &block)
    # Specify SOAP namespace in tag names (see https://github.com/savonrb/savon/issues/340 )
    params = Hash[params.map{|k,v| ["v2:#{k}", v]}]
    begin
      soap_client.call(action, message: params, &block)
    rescue AuthenticationException
      raise
    rescue StandardError => e
      # Keep old behaviour: raise only internal Exception class
      raise Exception, e.message
    end
  end

balance method not available

This is a great gem, by the way.
The balance method does not seem to be available.

audio = translator.speak "#{bing_target}", :language => :fr, :format => 'audio/mp3', :options => 'MaxQuality'
      File.open(audio_path, 'wb') { |f| f.write audio }
      log("processed: #{bing_target}, balance: #{translator.balance}")

Error (via delayed_job log)

NoMethodError: undefined method `balance' for #<BingTranslator:0x00000004dab4d0>

SSL Error while trying to use this gem

Hi, I followed all the steps in the documentation but I still keep getting this SSL Error. Is there any workaround for this?

spanish_trans.rb:4: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:164:in `rescue in result': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (BingTranslator::Exception)
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:158:in `result'
        from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:45:in `translate'
        from spanish_trans.rb:12:in `<main>'

raises a nil error when detecting white spaces

translator = BingTranslator.new(your_client_id, your_client_secret)
translator.detect(' ')

this would raise a NoMethodError: undefined methodto_sym' for nil:NilClass`, we should properly handle this either with a custom error or return a symbol for an error.

right now the work around I have is to make sure the phrase is not blank, but a better error would definitely be handier than a NoMethodError

How to test code using the bing_translator gem?

Hey there,

maybe not the best place but I cannot find any other. First of all: Thanks for the gem, makes my life a lot easier dealing with the bing API. Now, in an application I want to mock the response coming from the service in order to write some some tests for a wrapper I've built around your gem. Is there any possiblity to do that without setting up a test API key calling the API every time I'm running a bing-related test?

best and thanks,
Andi

Returning characters used/left per month

Firstly - whoah, this is an immense gem, thanks for putting it together!

Secondly, given that (assuming devs wish to stick with free translations :-) ), would it be possible to return the number of available translations left per month? This would be useful to monitor usage - not that I expect to reach 2,000,000 characters, but it might be a useful feature.

I recognise this feature might not be available in the API, so this might not be possible.

\r \n are not preserved

hopefully I implemented something incorrectly and you can correct me.

I'm seeing carriage returns stripped from translations.

e.g.

t=BingTranslator.new(ENV.fetch('BING_TRANSLATE_APPID', nil), ENV.fetch('BING_TRANSLATE_SECRET', nil))

s = 
  "<script>alert(‘hi’)</script>\r\n\r\nAlso, here's a paragraph\r\nIf you’re clear about your position, click one of the icons below (hover over the decision buttons for a description of what each one means). \r\nYou’ll be prompted to make a short statement about the reason for your decision. This makes it easy to see a summary of what everyone thinks and why. You can change your mind and edit your decision freely until the proposal closes."

t.translate(s, to: 'fr') =  
  "<script>alert(‘hi’)</script>Aussi, voici un paragraphe, si vous êtes clair sur votre poste, cliquez sur une des icônes ci-dessous (survolez les boutons de décision pour obtenir une description de ce que signifie chacun d'eux). \nVous serez invité à faire une brève déclaration sur la raison de votre décision. Il est facile de voir un résumé de ce que tout le monde pense et pourquoi. Vous pouvez changer d'avis et modifier votre décision librement jusqu'à ce que la proposition ferme."

The received token is of incorrect token type. : ID=0261.V2_Soap.Translate.2BE299C8

Hey guys, I ran into a Problem trying to translate a text.

translator = BingTranslator.new('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
translator.translate('test', from: :de, to: :en)

returns

BingTranslator::Exception: (s:Client) ArgumentException: The received token is of incorrect token type. : ID=0261.V2_Soap.Translate.2BE299C8
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:152:in `rescue in result'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:150:in `result'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:38:in `translate'
	from (irb):16
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `block in require'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
	from /Users/dochlast/projects/buddy/bin/rails:9:in `<top (required)>'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `block in load'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
	from /Users/dochlast/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /Users/dochlast/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from -e:1:in `<main>'

Any1 else running into this issue? How can we solve this? And is Azure really a SOAP Endpoint, not a REST Endpoint? And why are specs failing?

Translate Array and Detect Array is missing

As the bing API is providing translate array method its better to have one method for array to avoid bulk API calls.

http://msdn.microsoft.com/en-us/library/ff512407.aspx

A sampling of the methods that the API provides to the developer are listed below:

Translate — Translates text from one language to another
TranslateArray — Translates an array of texts from one language to another
Speak (Text to Speech) — Returns audio of the text spoken in the desired language
Detect — Detects the language of the string
Detect Array — Detects the language of an array of strings
GetTranslations — Returns an array of alternative translations for the text
GetTranslationsArray — Returns an array of alternative translations for a text array
GetLanguageNames, GetLanguagesForSpeak, GetLanguagesForTranslate — Return a list of languages supported by name, for audio and by code
AddTranslation, AddTranslationArray — Allows developers to add their own translations to the Microsoft Translator infrastructure.

bad URI(is not URI?)

Getting an error when attempting to use the translate method. I can create a translator and get my balance but the actual translate method returns this error:

BingTranslator::Exception: bad URI(is not URI?): {:host=>"api.microsofttranslator.com", :path=>"/V2/soap.svc", :port=>"80", :query=>"wsdl", :scheme=>"http", :headers=>{}, :body=>nil, :ssl_verify_peer=>true}

undefined method `headers' for HTTP:Module (BingTranslator::Exception)

As ruby novice I can't sort out the error I receive when using the basic code shown in the documentation. I was hoping someone could point me in the right direction

require 'bing_translator'

auth = YAML.load_file('bing_translate.yml')
translator = BingTranslator.new(auth['client_id'],auth['client_secret'],false,auth['azure_key'])

#This works great so I'm authorized and talking to the API
puts translator.balance

#This code here kicks out the exception
spanish = translator.translate('Hello. This will be translated!', :from => 'en', :to => 'es')
puts spanish

undefined method 'headers' for HTTP:Module (BingTranslator::Exception)
from /usr/local/rvm/gems/ruby-2.2.1/gems/bing_translator-4.4.1/lib/bing_translator.rb:158:in 'result
from /usr/local/rvm/gems/ruby-2.2.1/gems/bing_translator-4.4.1/lib/bing_translator.rb:45:in 'translate'

namespace issue

irb(main):004:0> p translator.translate 'Hello. This will be translated!', :from => 'en', :to => 'es'
warning: peer certificate won't be verified in this SSL session
Nokogiri::XML::XPath::SyntaxError: Undefined namespace prefix: //xmlns:string
from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:159:in evaluate' from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:159:inxpath'
from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:150:in map' from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:150:inxpath'
from /usr/lib/ruby/gems/1.8/gems/bing_translator-2.0.0/lib/bing_translator.rb:41:in `translate'
from (irb):4
from �:0

Error with rubyntlm

Hey @relrod , this gem looks really useful.

I tried this out but I ran into the following error, inspite of the fact that I am already on the latest version of the rubyntlm.

[4] pry(main)> spanish = translator.translate('Hello. This will be translated!', :from => 'en', :to => 'es')
ArgumentError: Invalid version of rubyntlm. Please use v0.3.2+.
from /Users/eklavya/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/httpi-2.4.2/lib/httpi/adapter/net_http.rb:63:in `check_net_ntlm_version!'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.