Skip to content

Instantly share code, notes, and snippets.

@ajesler
Last active August 29, 2015 14:03
Show Gist options
  • Save ajesler/ea7d4b991b793c220302 to your computer and use it in GitHub Desktop.
Save ajesler/ea7d4b991b793c220302 to your computer and use it in GitHub Desktop.
Translation practice based on grabbing tweets in the desired language and feeding through google translate and speaking the text.

Twitter Translation Practice

This project was conceived with the idea of getting some real world language practice.

Installation

  1. Install the required gems.
gem install google-translate
gem install twitter
gem install colorize
  1. Open ttp.rb and set your twitter application OAuth credentials.
    You can register an application with Twitter here.

Usage

  1. Set the search_text variable on line 69.
  2. Set the TO_LANG and FROM_LANG options on line 18 & 19
  3. Set the NO_SAY variable one line 17 to true if you dont want the tweet read out.
  4. Run the script with ruby tpp.rb

Future work

  • add a command line wrapper with options to specify search text, etc
    • option to use a user or set of users tweets.
    • option to specify source language in twitter seach and google translate
    • option to specify output language of google translate
  • get and store search results to reduce API usage.
  • method for changing search text while running.
require 'twitter'
require 'google_translate'
require 'colorize'
SEPARATOR = "@------------------------@"
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"
# http://stackoverflow.com/questions/6368659/removing-url-from-text-using-ruby
SKIP = 'n'
# get these values by registering at https://apps.twitter.com/
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_SECRET = ""
NO_SAY = false
TO_LANG = :en
FROM_LANG = :de
def print_separator
puts SEPARATOR.white
end
def strip_links(text)
text.gsub(URI_REGEX, '')
end
def create_twitter_client
Twitter::REST::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = ACCESS_TOKEN
config.access_token_secret = ACCESS_SECRET
end
end
def use_tweet(tweet_text)
print_separator
tweet_text = strip_links(tweet_text)
translator = GoogleTranslate.new
result = translator.translate(FROM_LANG, TO_LANG, tweet_text)
translation = result[0][0][0]
puts "Tweet Text (#{FROM_LANG}): #{tweet_text}".magenta
translator.say(FROM_LANG, tweet_text) unless NO_SAY
puts "What is your translation of the above tweet? ('n' to skip)"
your_translation = gets.chomp
if your_translation.eql? SKIP
puts "Skipping tweet"
return
end
puts
puts "Google Translation: #{translation}".light_green
puts "Your translation: #{your_translation}".cyan
end
client = create_twitter_client
# Set search_text to your desired search terms. The '-rt' means filter out retweets.
search_text = "tech -rt"
puts "Welcome to Twitter Translation Practice."
puts "Translating tweets with the search '#{search_text}' from #{FROM_LANG} to #{TO_LANG}"
puts
while true
tweet_text = client.search(search_text, :lang => FROM_LANG.to_s).each do |tweet|
use_tweet(tweet.text)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment