Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +26 -13
src/main.py
CHANGED
@@ -29,33 +29,46 @@ def translate_korean_to_english(text):
|
|
29 |
try:
|
30 |
# 작은따옴표로 묶인 단어들 찾기
|
31 |
quoted_words = find_quoted_words(text)
|
|
|
32 |
|
33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
temp_text = text
|
35 |
for i, word in enumerate(quoted_words):
|
36 |
temp_text = temp_text.replace(f"'{word}'", f"QUOTED_WORD_{i}")
|
37 |
-
|
38 |
-
|
39 |
params = {
|
40 |
-
"client": "gtx",
|
41 |
"sl": "ko",
|
42 |
"tl": "en",
|
43 |
"dt": "t",
|
44 |
"q": temp_text.strip()
|
45 |
}
|
46 |
response = requests.get(url, params=params)
|
|
|
47 |
if response.status_code == 200:
|
48 |
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
49 |
|
50 |
-
# 번역된 텍스트에서 마커를
|
51 |
-
for i,
|
52 |
-
|
53 |
-
word_params = params.copy()
|
54 |
-
word_params['q'] = word
|
55 |
-
word_response = requests.get(url, params=word_params)
|
56 |
-
if word_response.status_code == 200:
|
57 |
-
translated_word = word_response.json()[0][0][0].upper()
|
58 |
-
translated_text = translated_text.replace(f"QUOTED_WORD_{i}", f"'{translated_word}'")
|
59 |
|
60 |
return translated_text
|
61 |
else:
|
|
|
29 |
try:
|
30 |
# 작은따옴표로 묶인 단어들 찾기
|
31 |
quoted_words = find_quoted_words(text)
|
32 |
+
translated_quoted_words = []
|
33 |
|
34 |
+
# 각 따옴표 단어를 먼저 번역
|
35 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
36 |
+
for word in quoted_words:
|
37 |
+
params = {
|
38 |
+
"client": "gtx",
|
39 |
+
"sl": "ko",
|
40 |
+
"tl": "en",
|
41 |
+
"dt": "t",
|
42 |
+
"q": word
|
43 |
+
}
|
44 |
+
response = requests.get(url, params=params)
|
45 |
+
if response.status_code == 200:
|
46 |
+
translated_word = response.json()[0][0][0].upper()
|
47 |
+
translated_quoted_words.append(translated_word)
|
48 |
+
else:
|
49 |
+
translated_quoted_words.append(word)
|
50 |
+
|
51 |
+
# 원본 텍스트에서 따옴표 부분을 임시 마커로 대체
|
52 |
temp_text = text
|
53 |
for i, word in enumerate(quoted_words):
|
54 |
temp_text = temp_text.replace(f"'{word}'", f"QUOTED_WORD_{i}")
|
55 |
+
|
56 |
+
# 전체 문장 번역
|
57 |
params = {
|
58 |
+
"client": "gtx",
|
59 |
"sl": "ko",
|
60 |
"tl": "en",
|
61 |
"dt": "t",
|
62 |
"q": temp_text.strip()
|
63 |
}
|
64 |
response = requests.get(url, params=params)
|
65 |
+
|
66 |
if response.status_code == 200:
|
67 |
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
68 |
|
69 |
+
# 번역된 텍스트에서 마커를 번역된 따옴표 단어로 대체
|
70 |
+
for i, translated_word in enumerate(translated_quoted_words):
|
71 |
+
translated_text = translated_text.replace(f"QUOTED_WORD_{i}", f"'{translated_word}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
return translated_text
|
74 |
else:
|