Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +69 -41
src/main.py
CHANGED
@@ -47,55 +47,83 @@ def translate_quoted_word(word):
|
|
47 |
print(f"Word translation error: {e}")
|
48 |
return word
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def is_english(text):
|
51 |
"""텍스트가 영어인지 확인하는 함수"""
|
52 |
# 영어 알파벳과 기본적인 문장부호만 포함되어 있는지 확인
|
53 |
english_pattern = re.compile(r'^[A-Za-z\s\'".,!?-]+$')
|
54 |
return bool(english_pattern.match(text.replace("'", "")))
|
55 |
|
56 |
-
def process_english_input(text):
|
57 |
-
"""영어 입력을 처리하는 함수"""
|
58 |
-
# 따옴표로 묶인 단어들을 그대로 유지하면서 나머지 텍스트는 그대로 반환
|
59 |
-
return text
|
60 |
-
|
61 |
def translate_korean_to_english(text):
|
62 |
"""전체 텍스트 번역 함수"""
|
63 |
try:
|
64 |
-
#
|
|
|
|
|
|
|
65 |
if is_english(text):
|
66 |
-
return
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
}
|
88 |
-
response = requests.get(url, params=params)
|
89 |
-
if response.status_code == 200:
|
90 |
-
translated = ' '.join(item[0] for item in response.json()[0] if item[0])
|
91 |
-
translated_parts.append(translated)
|
92 |
-
else:
|
93 |
-
translated_parts.append(unquoted)
|
94 |
-
|
95 |
-
# 번역된 부분들을 합치기
|
96 |
-
result = ''.join(translated_parts).strip()
|
97 |
-
return result
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
except Exception as e:
|
100 |
print(f"Translation error: {e}")
|
101 |
return text
|
@@ -108,11 +136,11 @@ def result():
|
|
108 |
return render_template('error.html', error="Please enter text to translate")
|
109 |
|
110 |
try:
|
111 |
-
#
|
112 |
-
|
113 |
|
114 |
-
#
|
115 |
-
english_text =
|
116 |
if not english_text:
|
117 |
raise Exception("Translation failed")
|
118 |
|
|
|
47 |
print(f"Word translation error: {e}")
|
48 |
return word
|
49 |
|
50 |
+
|
51 |
+
|
52 |
+
def normalize_quotes(text):
|
53 |
+
"""따옴표 형식을 정규화하는 함수"""
|
54 |
+
# 따옴표가 없는 단어 끝에 붙은 따옴표 처리
|
55 |
+
text = re.sub(r"(\w+)'", r"'\1'", text)
|
56 |
+
# 한쪽 따옴표만 있는 경우 처리
|
57 |
+
text = re.sub(r"'(\w+)(?!')", r"'\1'", text)
|
58 |
+
text = re.sub(r"(?<!')(\w+)'", r"'\1'", text)
|
59 |
+
return text
|
60 |
+
|
61 |
+
def normalize_quotes(text):
|
62 |
+
"""따옴표 형식을 정규화하는 함수"""
|
63 |
+
# 따옴표가 없는 단어 끝에 붙은 따옴표 처리
|
64 |
+
text = re.sub(r"(\w+)'", r"'\1'", text)
|
65 |
+
# 한쪽 따옴표만 있는 경우 처리
|
66 |
+
text = re.sub(r"'(\w+)(?!')", r"'\1'", text)
|
67 |
+
text = re.sub(r"(?<!')(\w+)'", r"'\1'", text)
|
68 |
+
return text
|
69 |
+
|
70 |
def is_english(text):
|
71 |
"""텍스트가 영어인지 확인하는 함수"""
|
72 |
# 영어 알파벳과 기본적인 문장부호만 포함되어 있는지 확인
|
73 |
english_pattern = re.compile(r'^[A-Za-z\s\'".,!?-]+$')
|
74 |
return bool(english_pattern.match(text.replace("'", "")))
|
75 |
|
|
|
|
|
|
|
|
|
|
|
76 |
def translate_korean_to_english(text):
|
77 |
"""전체 텍스트 번역 함수"""
|
78 |
try:
|
79 |
+
# 입력 텍스트 정규화
|
80 |
+
text = normalize_quotes(text)
|
81 |
+
|
82 |
+
# 영어 입력 확인
|
83 |
if is_english(text):
|
84 |
+
return text
|
85 |
|
86 |
+
# 따옴표로 묶인 단어들 찾기
|
87 |
+
quoted_words = re.findall(r"'([^']*)'", text)
|
88 |
+
translated_quoted = {}
|
89 |
+
|
90 |
+
# 따옴표 안의 단어들 먼저 번역
|
91 |
+
for word in quoted_words:
|
92 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
93 |
+
params = {
|
94 |
+
"client": "gtx",
|
95 |
+
"sl": "ko",
|
96 |
+
"tl": "en",
|
97 |
+
"dt": "t",
|
98 |
+
"q": word
|
99 |
+
}
|
100 |
+
response = requests.get(url, params=params)
|
101 |
+
if response.status_code == 200:
|
102 |
+
translated = response.json()[0][0][0].upper()
|
103 |
+
translated_quoted[word] = translated
|
104 |
+
# 임시 마커로 대체
|
105 |
+
text = text.replace(f"'{word}'", f"QUOTED_{len(translated_quoted)}_")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# 전체 문장 번역
|
108 |
+
params = {
|
109 |
+
"client": "gtx",
|
110 |
+
"sl": "ko",
|
111 |
+
"tl": "en",
|
112 |
+
"dt": "t",
|
113 |
+
"q": text
|
114 |
+
}
|
115 |
+
response = requests.get(url, params=params)
|
116 |
+
|
117 |
+
if response.status_code == 200:
|
118 |
+
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
119 |
+
|
120 |
+
# 번역된 텍스트에서 마커를 번역된 단어로 대체
|
121 |
+
for i, (original, translated) in enumerate(translated_quoted.items(), 1):
|
122 |
+
translated_text = translated_text.replace(f"QUOTED_{i}_", f"'{translated}'")
|
123 |
+
|
124 |
+
return translated_text
|
125 |
+
else:
|
126 |
+
raise Exception(f"Translation API returned status code: {response.status_code}")
|
127 |
except Exception as e:
|
128 |
print(f"Translation error: {e}")
|
129 |
return text
|
|
|
136 |
return render_template('error.html', error="Please enter text to translate")
|
137 |
|
138 |
try:
|
139 |
+
# 입력 텍스트 정규화
|
140 |
+
input_text = normalize_quotes(input_text)
|
141 |
|
142 |
+
# 번역 수행
|
143 |
+
english_text = translate_korean_to_english(input_text)
|
144 |
if not english_text:
|
145 |
raise Exception("Translation failed")
|
146 |
|