Closed wotanzheng closed 7 years ago
Try running the following in the Python interpreter window and paste the output between triple backquotes (e.g. ```output```) here.
import logging
logging.basicConfig(level=logging.DEBUG)
import google_translate
# Make sure it says 0.2.2
print google_translate.__version__
translator = google_translate.GoogleTranslator()
print translator.translate("field managements across China, was developed to estimate rice grain yield and NUE in various rice production regions", "ja")
It still doesn't work. I am too happy to see Japanese chars. But the result is still None.
The output is
E:\google-translate-master>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>>
>>> import google_translate
>>>
>>> # Make sure it says 0.2.2
... print google_translate.__version__
0.2.2
>>> translator = google_translate.GoogleTranslator()
DEBUG:google_translate.cache.Cache:Cache initiated max_size: (500), valid_period
: (604800.0)
DEBUG:google_translate.utils:Trying to load content from file: u'E:\\google-tran
slate-master\\google_translate\\data\\languages'
DEBUG:google_translate.utils:Ignoring line: '#Chinese Simplified:zh-cn'
DEBUG:google_translate.utils:Ignoring line: '#Chinese Traditional:zh-tw'
>>> print translator.translate("field managements across China, was developed to
estimate rice grain yield and NUE in various rice production regions", "ja")
DEBUG:google_translate.translator.GoogleTranslator:Max input size: (1980)
DEBUG:google_translate.translator.GoogleTranslator:Unquoted text size: (117)
DEBUG:google_translate.translator.GoogleTranslator:Quoted text size: (153)
INFO:google_translate.translator.GoogleTranslator:Translating word: 'field manag
ements across China, was developed to estimate rice grain yield and NUE in vario
us rice production regions'
DEBUG:google_translate.translator.GoogleTranslator:src_lang: 'auto' dst_lang: 'j
a'
DEBUG:google_translate.cache.Cache:Searching cache for key: 'field managements a
cross China, was developed to estimate rice grain yield and NUE in various rice
production regionsjaauto'
INFO:google_translate.cache.Cache:Item not in cache
INFO:google_translate.translator.GoogleTranslator:Building the request
DEBUG:google_translate.translator.GoogleTranslator:Request url: u'https://transl
ate.google.ru/translate_a/single?client=t&sl=auto&tl=ja&dt=t&dt=bd&dt=rm&dt=qca&
ie=UTF-8&oe=UTF-8&tk=666080.794028&q=field%20managements%20across%20China%2C%20w
as%20developed%20to%20estimate%20rice%20grain%20yield%20and%20NUE%20in%20various
%20rice%20production%20regions'
DEBUG:google_translate.translator.GoogleTranslator:URL size: (284)
INFO:google_translate.translator.GoogleTranslator:Attempt no. 1 out of 5
DEBUG:google_translate.utils:Headers: [(u'Host', u'translate.google.ru'), (u'Acc
ept-Language', u'en-US,en;q=0.8'), (u'Accept-Encoding', u'gzip, deflate, sdch'),
(u'Referer', u'https://translate.google.ru/'), (u'Connection', u'close'), (u'Ac
cept', u'*/*'), (u'User-Agent', u'Mozilla/5.0')]
DEBUG:google_translate.utils:Proxy: None
DEBUG:google_translate.utils:Socket timeout: (10.0)
INFO:google_translate.utils:Sending request
INFO:google_translate.translator.GoogleTranslator:Parsing reply
DEBUG:google_translate.translator.GoogleTranslator:Raw data: [[["様々な米生産地
における米収量とNUDEを推定するために、中国全域の圃場管理を開発した","field manag
ement across China, was developed to estimate rice grain yield and NUDE in vario
us rice production regions",null,null,3],[null,null,"Samazamana Amerika seisanch
i ni okeru Amerika shūryō to NUDE o suitei suru tame ni, Chūgoku zen'iki no hojō
kanri o kaihatsu shita"]],null,"en",null,null,null,0.59784472,["field \u003cb\u
003e\u003ci\u003emanagement\u003c/i\u003e\u003c/b\u003e across China, was develo
ped to estimate rice grain yield and \u003cb\u003e\u003ci\u003eNUDE\u003c/i\u003
e\u003c/b\u003e in various rice production regions","field management across Chi
na, was developed to estimate rice grain yield and NUDE in various rice producti
on regions",[1],null,null,true],[["en"],null,[0.59784472],["en"]]]
DEBUG:google_translate.translator.GoogleTranslator:JSON data: [[['様々な米生産地
における米収量とNUDEを推定するために、中国全域の圃場管理を開発した', 'field mana
gement across China, was developed to estimate rice grain yield and NUDE in vari
ous rice production regions', None, None, 3], [None, None, u"Samazamana Amerika
seisanchi ni okeru Amerika shūryō to NUDE o suitei suru tame ni, Chūgoku zen'iki
no hojō kanri o kaihatsu shita"]], None, 'en', None, None, None, 0.59784472, ['
field <b><i>management</i></b> across China, was developed to estimate rice grai
n yield and <b><i>NUDE</i></b> in various rice production regions', 'field manag
ement across China, was developed to estimate rice grain yield and NUDE in vario
us rice production regions', [1], None, None, True], [['en'], None, [0.59784472]
, ['en']]]
DEBUG:google_translate.translator.GoogleTranslator:Extracted data: {'original_te
xt': 'field management across China, was developed to estimate rice grain yield
and NUDE in various rice production regions', 'extra': {}, 'has_typo': True, 'ro
manization': 'field management across China, was developed to estimate rice grai
n yield and NUDE in various rice production regions', 'src_lang': 'en', 'transla
tion': '様々な米生産地における米収量とNUDEを推定するために、中国全域の圃場管理を
開発した', 'match': 0.59784472}
DEBUG:google_translate.cache.Cache:Adding new item to cache, key: 'field managem
ents across China, was developed to estimate rice grain yield and NUE in various
rice production regionsjaauto'
None
>>>
You get None because Google detects a typo (has_typo = True).
You have two options here you can either correct all the typos in the text or use the get_info_dict method which returns the complete info dictionary and gives you more control.
Something like this should do the trick:
import google_translate
translator = google_translate.GoogleTranslator()
info_dict = translator.get_info_dict(<text here>, "ja")
print info_dict["translation"]
@wotanzheng if the above solution works please close this issue.
Thanks. it works on one sentence. I am just trying to txt batch translation. Sorry. I forgot to close it.
Since the command line utility does not work for Windows you can copy the code from https://github.com/MrS0m30n3/google-translate/blob/master/bin/google-translate#L135 (until line 155) and make some changes to make it run. Also, you should change line-150 to use the get_info_dict method like this:
info_dict = translator.get_info_dict(utils.unquote_unicode(text), args.dst_lang, args.src_lang)
trans_content.append(info_dict["translation"])
Thank you so much:-) I'll try it.
Here is a quick script i wrote just now. You can adjust it to make it better
import re
import sys
from google_translate import GoogleTranslator, utils
encoding = "UTF-8"
NEWLINE_REPLACE = '\n\u2622\n'
NEWLINE_REPLACE_PATTERN = re.compile('\n?\u2622\n?', re.UNICODE)
def translate(filename, dst_lang):
with open(filename) as input_file:
translator = GoogleTranslator()
content = [line.strip().decode(encoding) + NEWLINE_REPLACE for line in input_file]
content = ''.join(content)
quoted_punctuations = [utils.quote_unicode(char) for char in utils.PUNCTUATIONS]
quoted_content = utils.quote_unicode(content)
# Split the text into chunks of max size and translate each one
trans_content = []
for text in utils.split_text(quoted_content, GoogleTranslator.MAX_INPUT_SIZE, quoted_punctuations):
info_dict = translator.get_info_dict(utils.unquote_unicode(text), dst_lang)
trans_content.append(info_dict["translation"])
trans_content = ' '.join(trans_content)
# Replace the NEWLINE_REPLACE chars with the newline char
results = NEWLINE_REPLACE_PATTERN.sub('\n', trans_content)
print results
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: ./translate-file.py <file> <dst-lang>"
exit(1)
translate(sys.argv[1], sys.argv[2])
Thank you so much. You are so nice. I will test the program tomorrow.:-P
Me again.:-)
I just write the following code and run it and get None.