일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 조건문
- Python
- Ai
- 검색
- Azure
- Compute
- 파라미터 힌트
- GPT
- 토끼
- 대스크
- 인컨텍스트 러닝
- 퓨샷
- 인자 힌트
- 파이썬
- 도커
- UDF
- Cognitive Service
- 컨텍스트 매니져
- Redshift
- 제로샷
- 동화책
- 아기동화
- 프롬프트 튜닝
- 반복문
- Docker
- AI 동화
- Cognitive Search
- 모험
- dask
- FOR
- Today
- Total
호랭이 분석가
[Azure] Language Translator로 번역하기 (Python) 본문
Azure Cognitive Searvice의 Translator 서비스를
Python으로 사용하기 위한 세팅을 해보도록 하겠습니다.
기본적으로 Azure의 Portal에 Translator가 생성이 되어있다고 가정하고 진행합니다.
pip install azure-ai-translation-text=1.0.0b1
우선 터미널에 azure-ai-translation-text 라이브러리를 설치해 줍니다.
from azure.ai.translation.text import TextTranslationClient, TranslatorCredential
from azure.ai.translation.text.models import InputTextItem
from azure.core.exceptions import HttpResponseError
translator_key = 'your-trans-key'
translator_endpoint = 'your_trans_endpoint'
translator_region = 'your-region'
trans_credential = TranslatorCredential(tranlator_key, translator_region)
text_translator = TextTranslationClient(endpoint=translator_endpoint,
credential=trans_credential)
Translator 서비스에서 생성된 Service Key와 Endpoint, Region을 확인하시고
위 코드를 실행하시면 준비는 되었습니다.
try:
source_language = "ko"
target_languages = ["en"]
input_text_elements = [ InputTextItem(text = "영어로 번역이 되나요?") ]
response = text_translator.translate(content = input_text_elements, to = target_languages, from_parameter = source_language)
translation = response[0] if response else None
if translation:
for translated_text in translation.translations:
print(f"Text was translated to: '{translated_text.to}' and the result is: '{translated_text.text}'.")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
한글을 영어로 번역하기 위한 코드이므로
번역할 대상이 되는 언어를 source_language에 "ko"를
번역할 언어를 target_languages에 ['en']을 입력합니다.
다른 문제가 없다면 print 영역에 영어로 번역이 되나요?
라고 입력한 내용이 번역되어 나올 것입니다.
영어 이외의 다른 언어로도 같이 번역을 하고 싶다면 리스트에 추가하면 되는데,
translator에서 사용 중인 언어표기법은 ISO 639-1을 사용하고 있습니다.
아래 위키백과 링크에 자세히 나와있으니 참고하시어 추가하시면 될 것 같습니다.
ISO 639-1 코드 목록 - 위키백과, 우리 모두의 백과사전
위키백과, 우리 모두의 백과사전. ISO 639는 언어 분류를 위해 사용되는 표준화된 명명법이다. 각 언어는 2글자(639-1) 및 3글자(639-2, 639-3)로 할당된다. 어족 ISO 언어 이름 네이티브 이름 639-1 639-2/T 63
ko.wikipedia.org
def translator(text, source, targets) :
source_language = source
target_language = targets
input_text_element = [InputTextItem(text=text)]
response = text_translator.translate(
content = input_text_element,
to = target_language,
from_parameter = source_language
)
translation = response[0] if response else None
return tranlation.translations[0].text
위 내용을 함수로 정리해 보았습니다.
번역할 대상이 되는 text와 언어인 source, targets를 사용하여 결과를 뱉어주는 함수입니다.
추가적으로 확인이 필요하시면
아래 링크를 참고하시길 바랍니다.
Quickstart: Azure Cognitive Services Translator SDKs - Azure Cognitive Services
Learn to translate text with the Translator service SDks in a programming language of your choice: C#, Java, JavaScript, or Python.
learn.microsoft.com
'Azure' 카테고리의 다른 글
[Azure] Language 서비스를 통한 문서 요약 (Python) (0) | 2023.07.16 |
---|