コード例 #1
0
        public async Task Translator_DictionaryTest_ItalianDictionary()
        {
            if (!EnvironmentVariablesDefined())
            {
                Assert.Inconclusive("Missing Translator Environment variables - Skipping test");
                return;
            }

            Translator translator = new Translator(translatorKey);

            CustomDictionary userCustomDictonary = new CustomDictionary();

            Dictionary <string, string> italianDictionary = new Dictionary <string, string>
            {
                { "camera", "bedroom" },
                { "foto", "personal photo" }
            };

            userCustomDictonary.AddNewLanguageDictionary("it", italianDictionary);

            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var italianSentence = "Voglio fare una foto nella camera";

            var translatedDocuments = await translator.TranslateArray(new string[] { italianSentence }, "it", "en");

            Assert.IsNotNull(translatedDocuments);
            string postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "it").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to take a personal photo in the bedroom", postProcessedMessage);
        }
コード例 #2
0
        public async Task Translator_DictionaryTest_ItalianDictionary()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "https://api.cognitive.microsoft.com/sts/v1.0/issueToken")
            .Respond("application/jwt", "<--valid-bearer-token-->");
            mockHttp.When(HttpMethod.Get, GetRequestDetect("Voglio fare una foto nella camera"))
            .Respond("application/xml", GetResponseDetect("it"));
            mockHttp.When(HttpMethod.Post, @"https://api.microsofttranslator.com/v2/Http.svc/TranslateArray2")
            .Respond("application/xml", GetResponse("Translator_DictionaryTest_ItalianDictionary.xml"));

            var translator = new Translator(_translatorKey, mockHttp.ToHttpClient());

            var userCustomDictonary = new CustomDictionary();

            var italianDictionary = new Dictionary <string, string>
            {
                { "camera", "bedroom" },
                { "foto", "personal photo" }
            };

            userCustomDictonary.AddNewLanguageDictionary("it", italianDictionary);

            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var italianSentence = "Voglio fare una foto nella camera";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { italianSentence }, "it", "en");

            Assert.IsNotNull(translatedDocuments);
            var postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "it").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to take a personal photo in the bedroom", postProcessedMessage);
        }
コード例 #3
0
        public async Task Translator_DictionaryTest_FrenchDictionary()
        {
            if (!EnvironmentVariablesDefined())
            {
                Assert.Inconclusive("Missing Translator Environment variables - Skipping test");
                return;
            }

            Translator translator = new Translator(translatorKey);

            CustomDictionary            userCustomDictonary = new CustomDictionary();
            Dictionary <string, string> frenctDictionary    = new Dictionary <string, string>
            {
                { "éclair", "eclairs tart" }
            };

            userCustomDictonary.AddNewLanguageDictionary("fr", frenctDictionary);
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArray(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            string postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to see eclairs tart", postProcessedMessage);
        }
コード例 #4
0
        public async Task Translator_DictionaryTest_FrenchDictionary()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "https://api.cognitive.microsoft.com/sts/v1.0/issueToken")
            .Respond("application/jwt", "<--valid-bearer-token-->");
            mockHttp.When(HttpMethod.Get, GetRequestDetect("Je veux voir éclair"))
            .Respond("application/xml", GetResponseDetect("fr"));
            mockHttp.When(HttpMethod.Post, @"https://api.microsofttranslator.com/v2/Http.svc/TranslateArray2")
            .Respond("application/xml", GetResponse("Translator_DictionaryTest_FrenchDictionary.xml"));

            Translator translator = new Translator(_translatorKey, mockHttp.ToHttpClient());

            CustomDictionary            userCustomDictonary = new CustomDictionary();
            Dictionary <string, string> frenctDictionary    = new Dictionary <string, string>
            {
                { "éclair", "eclairs tart" }
            };

            userCustomDictonary.AddNewLanguageDictionary("fr", frenctDictionary);
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            string postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to see eclairs tart", postProcessedMessage);
        }
コード例 #5
0
        public async Task Translator_DictionaryTest_ItalianDictionary()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, GetTranslateUri("it", "en"))
            .Respond("application/json", GetResponse("Translator_DictionaryTest_ItalianDictionary.json"));

            var translator = new Translator(_translatorKey, mockHttp.ToHttpClient());

            var userCustomDictonary = new CustomDictionary();

            var italianDictionary = new Dictionary <string, string>
            {
                { "camera", "bedroom" },
                { "foto", "personal photo" }
            };

            userCustomDictonary.AddNewLanguageDictionary("it", italianDictionary);

            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var italianSentence = "Voglio fare una foto nella camera";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { italianSentence }, "it", "en");

            Assert.IsNotNull(translatedDocuments);
            var postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "it").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to take a personal photo in the bedroom", postProcessedMessage);
        }
コード例 #6
0
        public async Task Translator_DictionaryTest_FrenchDictionary()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, GetTranslateUri("fr", "en"))
            .Respond("application/json", GetResponse("Translator_DictionaryTest_FrenchDictionary.json"));

            var translator = new Translator(_translatorKey, mockHttp.ToHttpClient());

            var userCustomDictonary = new CustomDictionary();
            var frenctDictionary    = new Dictionary <string, string>
            {
                { "éclair", "eclairs tart" }
            };

            userCustomDictonary.AddNewLanguageDictionary("fr", frenctDictionary);
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonary);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            var postProcessedMessage = customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage;

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("I want to see eclairs tart", postProcessedMessage);
        }
コード例 #7
0
        public async Task Translator_PatternsAndDictionaryTest()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "https://api.cognitive.microsoft.com/sts/v1.0/issueToken")
            .Respond("application/jwt", "<--valid-bearer-token-->");
            mockHttp.When(HttpMethod.Get, GetRequestDetect("mon nom est eta"))
            .Respond("application/xml", GetResponseDetect("fr"));
            mockHttp.When(HttpMethod.Post, @"https://api.microsofttranslator.com/v2/Http.svc/TranslateArray2")
            .Respond("application/xml", GetResponse("Translator_PatternsAndDictionaryTest.xml"));

            var translator = new Translator(_translatorKey, mockHttp.ToHttpClient());

            //creating the patterns post processor
            var attachedPostProcessors = new List <IPostProcessor>();
            var patterns       = new Dictionary <string, List <string> >();
            var frenchPatterns = new List <string> {
                "mon nom est (.+)"
            };

            patterns.Add("fr", frenchPatterns);
            IPostProcessor patternsPostProcessor = new PatternsPostProcessor(patterns);

            //attaching patterns post processor to the list of post processors
            attachedPostProcessors.Add(patternsPostProcessor);

            //creating user custom dictionary post processor
            var userCustomDictonaries = new CustomDictionary();
            var frenctDictionary      = new Dictionary <string, string>
            {
                { "etat", "Eldad" }
            };

            userCustomDictonaries.AddNewLanguageDictionary("fr", frenctDictionary);
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonaries);

            //attaching user custom dictionary post processor to the list of post processors
            attachedPostProcessors.Add(customDictionaryPostProcessor);

            var sentence = "mon nom est etat";

            //translating the document
            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { sentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            string postProcessedMessage = null;

            //test the actual use case of the compined post processors together
            foreach (var postProcessor in attachedPostProcessors)
            {
                postProcessedMessage = postProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage;
            }

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("My name is Eldad", postProcessedMessage);
        }
コード例 #8
0
        public async Task Translator_PatternsAndDictionaryTest()
        {
            if (!EnvironmentVariablesDefined())
            {
                Assert.Inconclusive("Missing Translator Environment variables - Skipping test");
                return;
            }

            //creating the patterns post processor
            List <IPostProcessor> attachedPostProcessors = new List <IPostProcessor>();
            Translator            translator             = new Translator(translatorKey);
            Dictionary <string, List <string> > patterns = new Dictionary <string, List <string> >();
            List <string> frenchPatterns = new List <string> {
                "mon nom est (.+)"
            };

            patterns.Add("fr", frenchPatterns);
            IPostProcessor patternsPostProcessor = new PatternsPostProcessor(patterns);

            //attaching patterns post processor to the list of post processors
            attachedPostProcessors.Add(patternsPostProcessor);

            //creating user custom dictionary post processor
            CustomDictionary            userCustomDictonaries = new CustomDictionary();
            Dictionary <string, string> frenctDictionary      = new Dictionary <string, string>
            {
                { "etat", "Eldad" }
            };

            userCustomDictonaries.AddNewLanguageDictionary("fr", frenctDictionary);
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonaries);

            //attaching user custom dictionary post processor to the list of post processors
            attachedPostProcessors.Add(customDictionaryPostProcessor);

            var sentence = "mon nom est etat";

            //translating the document
            var translatedDocuments = await translator.TranslateArray(new string[] { sentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            string postProcessedMessage = null;

            //test the actual use case of the compined post processors together
            foreach (IPostProcessor postProcessor in attachedPostProcessors)
            {
                postProcessedMessage = postProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage;
            }

            Assert.IsNotNull(postProcessedMessage);
            Assert.AreEqual("My name is Eldad", postProcessedMessage);
        }
コード例 #9
0
        public async Task Translator_PatternsTest_EmptyCustomLanguageDictionaryData()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, GetTranslateUri("fr", "en"))
            .Respond("application/json", GetResponse("Translator_PatternsTest_EmptyCustomLanguageDictionaryData.json"));

            var            translator                    = new Translator(_translatorKey, mockHttp.ToHttpClient());
            var            userCustomDictonaries         = new CustomDictionary();
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonaries);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            Assert.ThrowsException <ArgumentException>(() => customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage);
        }
コード例 #10
0
        public async Task Translator_PatternsTest_EmptyCustomLanguageDictionaryData()
        {
            if (!EnvironmentVariablesDefined())
            {
                Assert.Inconclusive("Missing Translator Environment variables - Skipping test");
                return;
            }

            Translator       translator                    = new Translator(translatorKey);
            CustomDictionary userCustomDictonaries         = new CustomDictionary();
            IPostProcessor   customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonaries);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArray(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            Assert.ThrowsException <ArgumentException>(() => customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage);
        }
コード例 #11
0
        public async Task Translator_PatternsTest_EmptyCustomLanguageDictionaryData()
        {
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Post, "https://api.cognitive.microsoft.com/sts/v1.0/issueToken")
            .Respond("application/jwt", "<--valid-bearer-token-->");
            mockHttp.When(HttpMethod.Get, GetRequestDetect("Je veux voir éclair"))
            .Respond("application/xml", GetResponseDetect("fr"));
            mockHttp.When(HttpMethod.Post, @"https://api.microsofttranslator.com/v2/Http.svc/TranslateArray2")
            .Respond("application/xml", GetResponse("Translator_PatternsTest_EmptyCustomLanguageDictionaryData.xml"));

            var            translator                    = new Translator(_translatorKey, mockHttp.ToHttpClient());
            var            userCustomDictonaries         = new CustomDictionary();
            IPostProcessor customDictionaryPostProcessor = new CustomDictionaryPostProcessor(userCustomDictonaries);

            var frenchSentence = "Je veux voir éclair";

            var translatedDocuments = await translator.TranslateArrayAsync(new string[] { frenchSentence }, "fr", "en");

            Assert.IsNotNull(translatedDocuments);
            Assert.ThrowsException <ArgumentException>(() => customDictionaryPostProcessor.Process(translatedDocuments[0], "fr").PostProcessedMessage);
        }