{"id":101,"date":"2023-07-20T13:30:25","date_gmt":"2023-07-20T05:30:25","guid":{"rendered":"http:\/\/www.leyiwangyou.top\/?p=101"},"modified":"2023-07-20T13:49:13","modified_gmt":"2023-07-20T05:49:13","slug":"%e5%bc%80%e5%8f%91%e4%ba%86%e4%b8%aatranslator","status":"publish","type":"post","link":"http:\/\/www.leyiwangyou.top\/?p=101","title":{"rendered":"\u5f00\u53d1\u4e86\u4e2atranslator"},"content":{"rendered":"<p><iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=660 height=86 src=\"\/\/music.163.com\/outchain\/player?type=2&#038;id=1862753740&#038;auto=1&#038;height=66\"><\/iframe><\/p>\n<blockquote>\n<p><strong>\u5f00\u53d1\u4e86\u4e00\u4e2a\u8f6f\u4ef6\uff0c\u5c06\u82f1\u6587\u6587\u672c\u7ffb\u8bd1\u6210\u4e2d\u6587<\/strong><br \/>\n\u529f\u80fd\u4ecb\u7ecd\uff1a<\/p>\n<ol>\n<li>\u53cc\u51fb\u6253\u5f00translate.exe<\/li>\n<li>\u9009\u62e9\u9700\u8981\u8fdb\u884c\u7ffb\u8bd1\u7684\u6587\u672c\u6587\u4ef6<\/li>\n<li>\u7b49\u5f85\u7ffb\u8bd1\u5b8c\u6210\u5e76\u4e14\u4f1a\u751f\u6210\u6587\u4ef6\u540d_translated.txt\u6587\u4ef6<\/li>\n<\/ol>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2023\/07\/img-20230720121045.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2023\/07\/img-20230720121404.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2023\/07\/img-20230720121332.png\" alt=\"\" \/><\/p>\n<blockquote>\n<p>\u529f\u80fd\u5b9e\u73b0\u903b\u8f91: <\/p>\n<ol>\n<li>\u4f7f\u7528\u963f\u91cc\u673a\u5668\u7ffb\u8bd1API<\/li>\n<li>\u7528python\u5b9e\u73b0\u6587\u4ef6\u8bfb\u53d6\u53ca\u5199\u5165API\u8fd4\u56de\u7ffb\u8bd1\u7684\u7ed3\u679c<\/li>\n<li>\u5f00\u53d1\u7528\u6237\u64cd\u4f5c\u9875\u9762<\/li>\n<li>\u5c06\u6587\u4ef6\u6253\u5305\u6210EXE\u53ef\u6267\u884c\u6587\u4ef6<\/li>\n<\/ol>\n<\/blockquote>\n<h2>\u4f7f\u7528\u963f\u91cc\u673a\u5668\u7ffb\u8bd1API<\/h2>\n<ol>\n<li>\u9996\u5148\u5f00\u901a\u673a\u5668\u7ffb\u8bd1\u670d\u52a1<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2023\/07\/img-20230720122150-300x217.png\" alt=\"\" \/><\/li>\n<li>\u521b\u5efa\u7528\u6237\u4ea7\u751faccess_key_id\u548caccess_key_secret<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2023\/07\/img-20230720122429-300x254.png\" alt=\"\" \/><\/li>\n<\/ol>\n<h2>\u7528python\u5b9e\u73b0\u6587\u4ef6\u8bfb\u53d6\u53ca\u5199\u5165API\u8fd4\u56de\u7ffb\u8bd1\u7684\u7ed3\u679c<\/h2>\n<pre><code>from alibabacloud_alimt20181012.client import Client as alimt20181012Client\nfrom alibabacloud_tea_openapi import models as open_api_models\nfrom alibabacloud_alimt20181012 import models as alimt_20181012_models\nfrom alibabacloud_tea_util import models as util_models\n\n # \u6362\u6210\u4f60\u8d26\u6237\u4ea7\u751f\u7684id\u548csecret\nACCESS_KEY_ID = &#039;123&#039;\nACCESS_KEY_SECRET = &#039;456&#039;\n\ndef create_client(\n    access_key_id: str,\n    access_key_secret: str,\n) -&gt; alimt20181012Client:\n    config = open_api_models.Config(\n        access_key_id=access_key_id,\n        access_key_secret=access_key_secret\n    )\n    config.endpoint = f&#039;mt.cn-hangzhou.aliyuncs.com&#039;\n    return alimt20181012Client(config)\n\ndef translate(text,client):\n    translate_general_request = alimt_20181012_models.TranslateGeneralRequest(\n        format_type=&#039;text&#039;,\n        source_language=&#039;en&#039;,\n        target_language=&#039;zh&#039;,\n        source_text=text,\n        scene=&#039;general&#039;\n    )\n    runtime = util_models.RuntimeOptions()\n    resp = client.translate_general_with_options(translate_general_request, runtime)\n    print(resp)\n    return resp.body.data.__dict__[&#039;translated&#039;]\n\ndef translate_file(file_path):\n    client = create_client(ACCESS_KEY_ID, ACCESS_KEY_SECRET)\n\n    # \u751f\u6210\u7ffb\u8bd1\u540e\u7684\u5b57\u5e55\u6587\u4ef6\u540d\n    translated_file_path = file_path.replace(&#039;.txt&#039;, f&#039;_translated.txt&#039;)\n\n    # \u6253\u5f00\u539f\u59cb\u6587\u4ef6\u548c\u7ffb\u8bd1\u540e\u7684\u6587\u4ef6\n    with open(file_path, &#039;r&#039;) as in_f, open(translated_file_path, &#039;w&#039;, encoding=&#039;utf-8&#039;) as out_f:\n        for line in in_f:\n            if line.strip() == &quot;&quot;:\n                continue\n            translated_line = translate(line,client)\n            out_f.write(translated_line)\n\n    print(f&#039;Translated file saved as {translated_file_path}&#039;)\n\nif __name__ == &#039;__main__&#039;:\n    translate_file(&#039;D:\\\\xxx.txt&#039;)<\/code><\/pre>\n<h2>\u5f00\u53d1\u7528\u6237\u64cd\u4f5c\u9875\u9762<\/h2>\n<pre><code># \u4fee\u6539\u4e0a\u9762\u7684\u4ee3\u7801 \u65b0\u589e\u754c\u9762\u64cd\u4f5c\nfrom tkinter import Tk\nfrom tkinter.filedialog import askopenfilename\nfrom tkinter import messagebox\n\n# \u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u8def\u5f84\ndef get_file_path():\n    # \u521b\u5efa\u4e00\u4e2a\u6839\u7a97\u53e3\uff0c\u4f46\u4e0d\u663e\u793a\n    root = Tk()\n    root.withdraw()\n\n    # \u663e\u793a\u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846\uff0c\u5e76\u8fd4\u56de\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u8def\u5f84\n    file_path = askopenfilename()\n\n    return file_path\n\n# \u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u8def\u5f84\u5e76\u8fdb\u884c\u7ffb\u8bd1\nfile_path = get_file_path()\nif file_path:\n    translate_file(file_path)\n    messagebox.showinfo(&quot;\u7ffb\u8bd1\u5b8c\u6210&quot;, &quot;\u7ffb\u8bd1\u5df2\u5b8c\u6210\uff01&quot;)<\/code><\/pre>\n<h2>\u5c06\u6587\u4ef6\u6253\u5305\u6210EXE\u53ef\u6267\u884c\u6587\u4ef6<\/h2>\n<ol>\n<li>\u5b89\u88c5PyInstaller\uff1a\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PyInstaller\uff1a\n<pre><code>pip install pyinstaller<\/code><\/pre>\n<\/li>\n<li>\u521b\u5efa\u6253\u5305\u811a\u672c\uff1a\u5728\u60a8\u7684Python\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3abuild.py\u7684\u65b0\u6587\u4ef6\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9:\n<pre><code>import PyInstaller.__main__\nif __name__ == &#039;__main__&#039;:\nPyInstaller.__main__.run([\n    &#039;your_script.py&#039;,  # \u60a8\u7684Python\u6587\u4ef6\u540d\u79f0\n    &#039;--onefile&#039;,\n    &#039;--noconsole&#039;,  # \u5982\u679c\u60a8\u60f3\u9690\u85cf\u63a7\u5236\u53f0\u7a97\u53e3\n])<\/code><\/pre>\n<\/li>\n<li>\u6253\u5305\u4ee3\u7801\uff1a\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\u4e2d\uff0c\u5207\u6362\u5230build.py\u6240\u5728\u7684\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6253\u5305\u60a8\u7684\u4ee3\u7801\uff1a\n<pre><code>py -3 build.py<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u4e86\u4e00\u4e2a\u8f6f\u4ef6\uff0c\u5c06\u82f1\u6587\u6587\u672c\u7ffb\u8bd1\u6210\u4e2d\u6587 \u529f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":6,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":112,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/112"}],"wp:attachment":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}