62
loading...
This website collects cookies to deliver better user experience
from twarc import Twarc2, expansions
import boto3
# Replace your bearer token below
client = Twarc2(bearer_token="REPLACE_ME")
boto3.client("translate")
.def translate(input_text, source_lang, target_lang):
translate_client = boto3.client("translate")
result = translate_client.translate_text(Text=input_text, SourceLanguageCode=source_lang,
TargetLanguageCode=target_lang)
return {"originalText": input_text, "translatedText": result.get('TranslatedText'),
"sourceLang": result.get('SourceLanguageCode'), "targetLang": result.get('TargetLanguageCode')}
search_recent
method of the twarc library and pass it a search query. This will search for Tweets from the last 7 days based on the conditions specified in the search query.from:SentimentsDev
) that are in the Hindi language (lang:hi
). Learn more about writing search queries here.def main():
# Replace the query below with your own
query = "from:SentimentsDev lang:hi -is:retweet"
# The search_all method call the recent-archive search endpoint to get Tweets based on the query
search_results = client.search_recent(query=query, max_results=100)
# Twarc returns all Tweets for the criteria set above, so we page through the results
for page in search_results:
# The Twitter API v2 returns the Tweet information and the user, media etc. separately
# so we use expansions.flatten to get all the information in a single JSON
result = expansions.flatten(page)
for tweet in result:
# Here we are calling the translate function and passing it the tweet text, the source language code
# and the target language code
response = translate(tweet['text'], tweet['lang'], 'en')
# Below we print the original text, the translated text, the source and target language codes
print("Original Text: {}".format(response['originalText']))
print("Translated Text: {}".format(response['translatedText']))
print("Source Language: {}".format(response['sourceLang']))
print("Target Language: {}".format(response['targetLang']))
if __name__ == "__main__":
main()
Original Text: आज का मौसम बहुत अच्छा है
Translated Text: Today's weather is very good
Source Language: hi
Target Language: en
hi
) and it got converted to English. Similarly, you can translate between various other languages using Amazon Translate.