Git Product home page Git Product logo

applied-ai--final-project's Introduction

Applied-AI--Final-Project

Task 4 Solution

""" Author: Marco Ng Description: Controller module that handles translations using calls to IBM Watson Language Translator service. """

import json import os from ibm_watson import LanguageTranslatorV3 from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

from dotenv import load_dotenv

load_dotenv()

apikey = os.environ['apikey'] url = os.environ['url'] version = os.environ['version']

authenticator = IAMAuthenticator(apikey) language_translator = LanguageTranslatorV3( version=version, authenticator=authenticator )

language_translator.set_service_url(url)

def english_to_french(english_text): """ Takes input text in English and returns output text in French :param english_text: Input text in English :returns: Output text in French """ french_text = __translate(english_text, 'en', 'fr') return french_text

def __translate(text, lang1code, lang2code): """ Wrapper function that handles service call and output handling :param text: Input text :param lang1code: Two-character code source language (e.g. en=English, fr=French) :param lang1code: Two-character code for target language (e.g. en=English, fr=French) :returns: Output text in English """ result = language_translator.translate( text=text, model_id=lang1code+'-'+lang2code ).get_result() return result['translations'][0]['translation'] if (len(result['translations'])>0)
else 'Error: No translations available...'

def french_to_english(french_text): """ Takes input text in French and returns output text in English :param french_text: Input text in French :returns: Output text in English """ english_text = __translate(french_text, 'fr', 'en') return english_text

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.