Azure 翻译文本 - 排除检测到的错误语言

Azure Translator Text - Exclude Wrong Detected Language

  1. 使用示例 https://docs.microsoft.com/bs-latn-ba/azure/cognitive-services/translator/quickstart-csharp-translate

  2. 当我输入"Apa Khabar"时,它检测到的语言作为ID,分数为1.0,但实际上我希望语言作为MS,以便我们的系统可以正确处理它。因为翻译的文本有不同的意思。

Apa khabar in ID = 谣言是什么? MS 中的 Apa khabar = 你好吗?

  1. 有没有办法在我使用 API 时排除 ID 语言?

谢谢。

在文档中搜索了排除语言的方法

        {
            //Console.WriteLine("Hello World!");
            // This is our main function.
            // Output languages are defined in the route.
            // For a complete list of options, see API reference.
            // https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
            string host = "https://api.cognitive.microsofttranslator.com";
            string route = "/translate?api-version=3.0&to=en";
            string subscriptionKey = "XXX";
            // Prompts you for text to translate. If you'd prefer, you can
            // provide a string as textToTranslate.
            Console.Write("Type the phrase you'd like to translate? ");
            string textToTranslate = Console.ReadLine();
            await TranslateTextRequest(subscriptionKey, host, route, textToTranslate);
        }

        // This sample requires C# 7.1 or later for async/await.
        // Async call to the Translator Text API
        static public async Task TranslateTextRequest(string subscriptionKey, string host, string route, string inputText)
        {
            /*
             * The code for your call to the translation service will be added to this
             * function in the next few sections.
             */

            object[] body = new object[] { new { Text = inputText } };
            var requestBody = JsonConvert.SerializeObject(body);

            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                // In the next few sections you'll add code to construct the request.
                // Build the request.
                // Set the method to Post.
                request.Method = HttpMethod.Post;
                // Construct the URI and add headers.
                request.RequestUri = new Uri(host + route);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                // Send the request and get response.
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                // Read response as a string.
                string result = await response.Content.ReadAsStringAsync();
                // Deserialize the response using the classes created earlier.
                TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
                // Iterate over the deserialized results.
                foreach (TranslationResult o in deserializedOutput)
                {
                    // Print the detected input language and confidence score.
                    Console.WriteLine("Detected input language: {0}\nConfidence score: {1}\n", o.DetectedLanguage.Language, o.DetectedLanguage.Score);
                    // Iterate over the results and print each translation.
                    foreach (Translation t in o.Translations)
                    {
                        Console.WriteLine("Translated to {0}: {1}", t.To, t.Text);
                    }
                }
            }
        }

获取检测到的语言作为 ID 而不是 MS 所以翻译后的句子变成了"What are the rumors"而不是"how are you"

检测函数和翻译函数return一组具有概率的语言。 在 Detect 的情况下,您必须在响应对象中过滤不需要的语言。 如果翻译 return 一种不需要的语言,您将不得不重新发布翻译,强制使用所需的语言。