-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation.py
More file actions
executable file
·106 lines (84 loc) · 2.96 KB
/
translation.py
File metadata and controls
executable file
·106 lines (84 loc) · 2.96 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /Users/ykobayashi/.pyenv/shims/python
import argparse
import re
import sys
import unicodedata
from typing import Optional
import pyperclip
import requests
from googletrans import Translator
def _translate_english(string: str) -> str:
"""
未使用の関数
:param string: 翻訳対象文字列
:return: 翻訳後文字列
"""
url = 'https://translate.google.com/?hl=ja#en/ja/'
headers = {"User-Agent": "Chrome/58.0.3029.110"}
params = {'q': string}
r = requests.get(url, headers=headers, params=params)
pattern = "TRANSLATED_TEXT=\'(.*?)\'"
result = re.search(pattern, r.text).group(1)
return result
def _translate_japanese(string: str) -> str:
"""
未使用の関数
:param string: 翻訳対象文字列
:return: 翻訳後文字列
"""
url = 'https://translate.google.co.jp/?hl=en#ja/en/'
headers = {"User-Agent": "Chrome/58.0.3029.110"}
params = {'q': string}
r = requests.get(url, headers=headers, params=params)
pattern = "TRANSLATED_TEXT=\'(.*?)\'"
result = re.search(pattern, r.text).group(1)
return result
def is_japanese(string: str) -> bool:
"""
日本語を含むか否かを判定
:param string: 判定対象文字列
:return: 日本語を含む場合、True
"""
for ch in string:
try:
name = unicodedata.name(ch)
except ValueError:
continue
if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
return True
return False
def main():
parser = argparse.ArgumentParser(description='標準入力をGoogle翻訳で和訳')
parser.add_argument('--input_file', '-i', nargs='?', default=None, help='入力ファイル')
parser.add_argument('--output_file', '-o', nargs='?', default=None, help='出力ファイル')
parser.add_argument('--j2e', '-j', action='store_true', help='和文英訳フラグ')
args = parser.parse_args()
input_text: Optional[str] = None
if args.input_file is None:
print('input text here.')
input_text = sys.stdin.read()
else:
try:
with open(args.input_file, 'r') as f:
input_text = f.read()
except FileNotFoundError as e:
print(f'{e.strerror}: {e.filename}')
print('\n---------------------------------------------------------\n')
input_text = input_text.replace('\n', ' ')
if not input_text:
exit(0)
if args.j2e:
result_text = Translator().translate(input_text, src="ja", dest="en").text
elif is_japanese(input_text):
result_text = Translator().translate(input_text, src="ja", dest="en").text
else:
result_text = Translator().translate(input_text, src="en", dest="ja").text
if args.output_file:
with open(args.output_file, 'w') as f:
f.write(result_text)
else:
print(result_text + '\n')
pyperclip.copy(result_text)
if __name__ == '__main__':
main()
print('complete.')