2019년 1월 30일 수요일

python : naver translate VS google translate

파파고 번역 VS 구글 번역

1. 함수 생성

import os, urllib, requests, json
from googletrans import Translator

client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

def call_naver_api_papago_nmt(con_str, _src, _dest):
 encText = urllib.pathname2url(con_str)
 data = "source=" + _src + "&target=" + _dest + "&text=" + encText
 url = "https://openapi.naver.com/v1/papago/n2mt"
 headers = {'X-Naver-Client-Id': client_id
            , 'X-Naver-Client-Secret' : client_secret
            , 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' 
           }
 r = requests.post(url, headers=headers, data=data.encode("utf-8"))

 if(r.status_code==200):
  trn_result= json.loads(str(r.text))
  print 'Papago Trans:' ,trn_result['message']['result']['translatedText']
 else:
     print "Error Code:" + r.status_code

def call_google_trans(con_str, _src, _dest):
 # translator = Translator()
 translator = Translator(service_urls=[
       'translate.google.com',
       'translate.google.co.kr',
     ])

 tr_results = translator.translate(con_str,dest=_dest, src=_src)
 print 'Google Trans:', tr_results.text

2. korean --> english

con_str_string = ["전문가들은 사물인터넷이 가전제품을 이끄는 기술이 될 것이라고 말한다.", "한국은 전자, 자동차, 그리고 IT 분야가 세계에서 가장 영향력 있다.", "그는 큰 전자 복합기업에서 근무하는 엔지니어이다."]
src = "ko"
dest = "en"

for con_str in con_str_string:
 print 'Org String:', con_str
 call_naver_api_papago_nmt(con_str, src, dest)
 call_google_trans(con_str, src, dest)
 print '----------------'

>> 결과
Org String: 전문가들은 사물인터넷이 가전제품을 이끄는 기술이 될 것이라고 말한다.
Papago Trans: Experts say that the Internet of Things will become a leading technology for home appliances.
Google Trans: Experts say that the Internet of things will be the technology that leads home appliances.
----------------
Org String: 한국은 전자, 자동차, 그리고 IT 분야가 세계에서 가장 영향력 있다.
Papago Trans: Korea has the most influence in the world in the fields of electronics, cars, and IT.
Google Trans: Korea is the most influential in the world in electronics, automobiles, and IT.
----------------
Org String: 그는 큰 전자 복합기업에서 근무하는 엔지니어이다.
Papago Trans: He is an engineer working for a large electronics complex.
Google Trans: He is an engineer working in a large electronics complex.
----------------

3. english --> korean

con_str_string = ["Experts say that the IoT will become a leading technology in consumer electronics.", "Korea is most competent in the fields of electronics, automobiles, and IT.", "He is an engineer with a large electronics conglomerate."]
src = "en"
dest = "ko"

for con_str in con_str_string:
 print 'Org String:', con_str
 call_naver_api_papago_nmt(con_str, src, dest)
 call_google_trans(con_str, src, dest)
 print '----------------'

>> 결과
Org String: Experts say that the IoT will become a leading technology in consumer electronics.
Papago Trans: 전문가들은 IoT가 가전 분야의 선도적 기술이 될 것으로 보고 있다.
Google Trans: 전문가들은 IoT가 소비자 전자 제품의 선도 기술이 될 것이라고 말한다.
----------------
Org String: Korea is most competent in the fields of electronics, automobiles, and IT.
Papago Trans: 한국은 전자 자동차 IT 분야에서 가장 유능하다.
Google Trans: 한국은 전자, 자동차 및 IT 분야에서 가장 유능한 기업입니다.
----------------
Org String: He is an engineer with a large electronics conglomerate.
Papago Trans: 그는 대기업의 기술자다
Google Trans: 그는 거대한 전자 제품 대기업을 보유한 엔지니어입니다.
----------------

4. link
네이버 번역 with python
구글 번역 with python

댓글 없음:

댓글 쓰기

추천 게시물

python: SVD(Singular Value Decomposition)로 간단한 추천시스템 만들기( feat. surprise )

svd_example In [15]: # !pip install surprise In [21]: from...