본문 바로가기
IT/Azure

Cognitive Translator 간단 테스트

by rapker 2023. 4. 26.
반응형
 
종량제와 대량 구매 중 선택할 수 있고, Translator의 제한 사항들에 대한 고려가 필요합니다.
  • S1 종량제
  • S2, S3, S4 대량 구매
 
 
 
  • 요청 당 문자 및 배열 제한
  • 시간 당 문자 제한
  • 대기 시간
  • 문장 길이 제한
 
  • 테스트 진행한 C# .sIn

translatorExample.zip
0.03MB

 

 

// 샘플코드 https://docs.microsoft.com/ko-kr/azure/cognitive-services/translator/quickstart-translator?tabs=csharp#translate-text
// 지원언어 https://docs.microsoft.com/ko-kr/azure/cognitive-services/translator/language-support#text-translation
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // Install Newtonsoft.Json with NuGet
namespace translatorExample
{
    class Program
    {
        private static readonly string subscriptionKey = "b2951cff1ff34306b1a04d1797802e99";
        private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com/";
        // Add your location, also known as region. The default is global.
        // This is required if using a Cognitive Services resource.
        private static readonly string location = "westeurope";
        private static string route = "";
        static async Task Main(string[] args)
        {
            // Input and output languages are defined as parameters.
            //string route = "/translate?api-version=3.0&from=en&to=de&to=it";      // 원본언어 지정
            //string route = "/translate?api-version=3.0&to=de&to=it";              // 원본언어 감지
            //string route = "/translate?api-version=3.0&to=ko";                    // 원본언어 감지로 한국어 번역해봄
            //string route = "/detect?api-version=3.0";                            // 번역없이 언어 감지
            SetRouteLanguage("en", "ko");
            await ReqTranslate("Hello, world!");
            await ReqTranslate("How are you?");
        }
        static void SetRouteLanguage(string _from, string _to)
        {
            route = string.Format("/translate?api-version=3.0&from={0}&to={1}", _from, _to);
        }
        static async Task ReqTranslate(string _msg)
        {
            object[] body = new object[] { new { Text = _msg } };
            var requestBody = JsonConvert.SerializeObject(body);
            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
               
                // Build the request.
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(endpoint + route);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                request.Headers.Add("Ocp-Apim-Subscription-Region", location);
                // 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();
                Console.WriteLine(result);
            }
        }
    }
}
반응형
LIST

'IT > Azure' 카테고리의 다른 글

RBAC 앱 등록  (0) 2023.04.26
리소스 그룹/구독 이동 유효성 검사  (0) 2023.04.26
VMSS Flexible Mode  (0) 2023.04.26
가상화 종류와 차이 조사  (0) 2023.04.26
Azure PowerShell Module 재설치  (0) 2023.04.26