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 StartedThe 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 CodeThere 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. TwitterThis 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 Action4. Translate the PhraseThe 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
|
- Home
- Python
- Ras Pi
-
Pi Hardware
- Pi-Hacks
-
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 >
- PiGlow & Email
- Pibrella Alarm System
- SMS with Python >
- Pi-Hacks 3
- Minecraft
- Computing
- Contact Me
- Random Hacks