TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Ras Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • Ferminal
    • Crypto Tracker
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me
  • Random Hacks

What is it?


Simply send your tweet to @PiTests and the T.T.B. will translate your message and return the phrase in English.  Or, when running, send a tweet in English and the TTB will convert it into the chosen language, in the examples German or French. This hack variation of the translation program that was built with the Google translate API uses http://mymemory.translated.net/, which has a limit for 1000 words/day free usage.   Installation is very simple, use PIP in the LX terminal and type the following: sudo pip install translate.  You can check out further code and updates on the homepage.  Combine this Twitter and you have a Translation Twitter Bot.  

1. Getting Started


The hack uses a Raspberry Pi 2 to make use of the faster processor speed.  Boot up the Pi, update and upgrade, open the LX Terminal and type:

$ sudo apt-get update
$ sudo apt-get upgrade 


The simplest way to download and install the program is to use the use the pip package, in the LX Terminal type:

$  sudo pip install translate.

2. The Translation Code


There are several codes that can be used with the translation, more details can be found on the software website.  The two main code lines used in the hack are show below .  The two letters used to identify the language are available here.  In this example I am using fr which means French.

Code 1: Convert from English to French

from translate import Translator
translator= Translator(to_lang="fr")
translation = translator.translate("This is a pen.")
print translation


This result is in translation from English to French and is return as a Unicode string.   To translate form say German to English then you have to set the language as auto and then write a phrase in German.

Code 2: Concert from German to English
​

from_lang = auto
to_lang = 'eng''


3. Twitter


This hacks requires you to have a Twitter Authorisation for the API set up which enables you to stream tweets, grab users names and post back to the timeline. In this project, post with a picture or just text.  If you do not require the Twitter codes then read on else, pop over to the Twitter Setup Tutorial.

In the program the first line takes each single tweet,
tweet_to_check = tweet.text 
Next, it checks that the tweet contains the user ID @PiTests, if so then the this is the sign that the 'phrase' is going to be translated into another language, currently French.
does_the_tweet_contain_key_word = tweet_to_check.find("@PiTests") 
The code line
end_of_key_phrase = tweet_to_check.find(" ")
checks for the space after the @PiTest which means the 'phrase' to translate is next, after the space.  This line, 
final_message_to_translate = tweet_to_check[end_of_key_phrase:] 
pulls out the phrase and then saves it as a variable called final_message_to_translate.  Now you have the phrase that needs translating, this can then be imported into the translation code in step 2.
       

T.T.B in Action



4. Translate the Phrase


The main part of the program concerns the conversion of the tweet into the new language in this example French.  This is sorted using the code
​translator = Translator(to_lang="fr") ###English to French
Then the phrase that was pulled out and stored in the variable in step 3 is passed into the translation code, like in step 2.
translation = translator.translate(phrase) ###translate phrase from twitter
The translation is returned as Unicode and this works in Twitter but will produce the letter 'u' before each phrase. To make the phrase look better convert the translation into a string using
translation  =  str(translation)
Next pull down he ID of the user who set the tweet so that you can message them back with their translated phrase.                    
user = str(tweet.user.screen_name)
final_message = "@%s," %(user), translation 

Finally send the message either with or without an image.            
api.update_status(status = final_message)
or
api.update_with_media(photo_path, final_message

5. Adding an Error Message


If the phrase cannot be converted then a error message is sent back to the person who sent the original tweet.  This may be because the Unicode cannot be converted to a string especially with characters like the beautiful umlaut! The errors handling section took a few attempts to fix mainly because of the str / tuple argument which kept crashing the program.  I also had the issue that when the error message was sent, if it sent another error message then Twitter thought the Translation Bot was spamming accounts.  To overcome this Hippy Jim suggested using the current time and addign it to the tweet so that each one was unique.  end = time.asctime( time.localtime(time.time()) )
The time, user ID and an error message are then combined to create a final error message which is sent to the Twitter user.


 photo_path_error = '/home/pi/error.jpg'
 print "error"
 user = str(tweet.user.screen_name)
 error_tweet = "Error, please try another phrase", end
 error = "@%s" %(user), error_tweet
 api.update_with_media(photo_path_error, error)

 

Download the Code


Picture
Powered by Create your own unique website with customizable templates.