diff --git a/final_project/machinetranslation/__init__.py b/final_project/machinetranslation/__init__.py new file mode 100644 index 000000000..67ce7b73e --- /dev/null +++ b/final_project/machinetranslation/__init__.py @@ -0,0 +1 @@ +from . import translator \ No newline at end of file diff --git a/final_project/machinetranslation/tests.py b/final_project/machinetranslation/tests.py new file mode 100644 index 000000000..807a47e19 --- /dev/null +++ b/final_project/machinetranslation/tests.py @@ -0,0 +1,15 @@ +import unittest + +from translator import english_to_french, french_to_english + +class TestEnglishToFrench(unittest.TestCase): + def testE2F(self): + self.assertEqual(english_to_french('hello'), 'bonjour') + self.assertNotEqual(english_to_french('yes'), 'bonjour') + +class TestFrenchToEnglish(unittest.TestCase): + def testF2E(self): + self.assertEqual(french_to_english('bonjour'), 'hello') + self.assertNotEqual(french_to_english('oui'), 'hello') + +unittest.main() \ No newline at end of file diff --git a/final_project/machinetranslation/tests/tests copy.py b/final_project/machinetranslation/tests/tests copy.py new file mode 100644 index 000000000..807a47e19 --- /dev/null +++ b/final_project/machinetranslation/tests/tests copy.py @@ -0,0 +1,15 @@ +import unittest + +from translator import english_to_french, french_to_english + +class TestEnglishToFrench(unittest.TestCase): + def testE2F(self): + self.assertEqual(english_to_french('hello'), 'bonjour') + self.assertNotEqual(english_to_french('yes'), 'bonjour') + +class TestFrenchToEnglish(unittest.TestCase): + def testF2E(self): + self.assertEqual(french_to_english('bonjour'), 'hello') + self.assertNotEqual(french_to_english('oui'), 'hello') + +unittest.main() \ No newline at end of file diff --git a/final_project/machinetranslation/translator.py b/final_project/machinetranslation/translator.py new file mode 100644 index 000000000..c6439e91c --- /dev/null +++ b/final_project/machinetranslation/translator.py @@ -0,0 +1,18 @@ +""" +Defines functions to translate from e2f and f2e +""" +from deep_translator import MyMemoryTranslator + +def english_to_french(english_text): + """ + Translates english text to french + """ + french_text = MyMemoryTranslator(source='en', target='fr').translate(english_text) + return french_text + +def french_to_english(french_text): + """ + Translates french text to english + """ + english_text = MyMemoryTranslator(source='fr', target='en').translate(french_text) + return english_text diff --git a/final_project/server.py b/final_project/server.py index a776f7ae3..3cffba4d5 100755 --- a/final_project/server.py +++ b/final_project/server.py @@ -7,18 +7,18 @@ @app.route("/englishToFrench") def englishToFrench(): textToTranslate = request.args.get('textToTranslate') - # Write your code here - return "Translated text to French" + french_text = translator.english_to_french(textToTranslate) + return french_text @app.route("/frenchToEnglish") def frenchToEnglish(): textToTranslate = request.args.get('textToTranslate') - # Write your code here - return "Translated text to English" + english_text = translator.french_to_english(textToTranslate) + return english_text @app.route("/") def renderIndexPage(): - # Write the code to render template + return render_template('index.html') if __name__ == "__main__": app.run(host="0.0.0.0", port=8080)