Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +66 -62
src/main.py
CHANGED
@@ -21,37 +21,33 @@ def clean_quotes(text):
|
|
21 |
"""따옴표 정리 함수"""
|
22 |
# 연속된 따옴표 제거
|
23 |
text = re.sub(r"'+", "'", text)
|
24 |
-
#
|
25 |
-
text = re.sub(r
|
26 |
return text
|
27 |
|
|
|
|
|
|
|
|
|
28 |
def normalize_quotes(text):
|
29 |
"""따옴표 형식을 정규화하는 함수"""
|
30 |
# 먼저 모든 따옴표를 정리
|
31 |
text = clean_quotes(text)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
for word in words:
|
48 |
-
if re.search(pattern, word):
|
49 |
-
# 이미 따옴표가 있는 경우는 그대로 두고, 없는 경우만 추가
|
50 |
-
if not word.startswith("'") and not word.endswith("'"):
|
51 |
-
word = f"'{word}'"
|
52 |
-
processed_words.append(word)
|
53 |
-
|
54 |
-
return ' '.join(processed_words)
|
55 |
|
56 |
def find_quoted_words(text):
|
57 |
"""작은따옴표로 묶인 단어들을 찾는 함수"""
|
@@ -63,8 +59,13 @@ def spell_out_word(word):
|
|
63 |
|
64 |
def is_english(text):
|
65 |
"""텍스트가 영어인지 확인하는 함수"""
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def translate_korean_to_english(text):
|
70 |
"""전체 텍스트 번역 함수"""
|
@@ -74,55 +75,57 @@ def translate_korean_to_english(text):
|
|
74 |
|
75 |
# 영어 입력 확인
|
76 |
if is_english(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
return text
|
78 |
|
79 |
-
#
|
|
|
80 |
quoted_words = re.findall(r"'([^']*)'", text)
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
#
|
84 |
-
|
85 |
-
if not word.strip(): # 빈 문자열 건너뛰기
|
86 |
-
continue
|
87 |
-
url = "https://translate.googleapis.com/translate_a/single"
|
88 |
-
params = {
|
89 |
-
"client": "gtx",
|
90 |
-
"sl": "ko",
|
91 |
-
"tl": "en",
|
92 |
-
"dt": "t",
|
93 |
-
"q": word
|
94 |
-
}
|
95 |
-
response = requests.get(url, params=params)
|
96 |
-
if response.status_code == 200:
|
97 |
-
translated = response.json()[0][0][0].upper()
|
98 |
-
translated_quoted[word] = translated
|
99 |
-
# 임시 마커로 대체
|
100 |
-
text = text.replace(f"'{word}'", f"QUOTED_{len(translated_quoted)}_")
|
101 |
-
|
102 |
-
# 전체 문장 번역
|
103 |
params = {
|
104 |
"client": "gtx",
|
105 |
"sl": "ko",
|
106 |
"tl": "en",
|
107 |
"dt": "t",
|
108 |
-
"q":
|
109 |
}
|
110 |
response = requests.get(url, params=params)
|
111 |
-
|
112 |
if response.status_code == 200:
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
except Exception as e:
|
123 |
print(f"Translation error: {e}")
|
124 |
return text
|
125 |
|
|
|
|
|
|
|
126 |
@app.route('/')
|
127 |
def index():
|
128 |
return render_template('index.html', title=app.config['TITLE'])
|
@@ -143,8 +146,9 @@ def result():
|
|
143 |
if not english_text:
|
144 |
raise Exception("Translation failed")
|
145 |
|
146 |
-
# 따옴표로 묶인 단어 추출
|
147 |
-
quoted_words =
|
|
|
148 |
|
149 |
# ASL 변환을 위해 따옴표 제거
|
150 |
clean_english = re.sub(r"'([^']*)'", r"\1", english_text)
|
@@ -157,7 +161,7 @@ def result():
|
|
157 |
|
158 |
for word in words:
|
159 |
word_upper = word.upper()
|
160 |
-
if
|
161 |
# 고유명사인 경우 철자를 하나씩 분리
|
162 |
spelled_word = spell_out_word(word)
|
163 |
processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
|
|
|
21 |
"""따옴표 정리 함수"""
|
22 |
# 연속된 따옴표 제거
|
23 |
text = re.sub(r"'+", "'", text)
|
24 |
+
# 불필요한 공백 제거
|
25 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
26 |
return text
|
27 |
|
28 |
+
def is_korean(text):
|
29 |
+
"""한글이 포함되어 있는지 확인"""
|
30 |
+
return bool(re.search('[가-힣]', text))
|
31 |
+
|
32 |
def normalize_quotes(text):
|
33 |
"""따옴표 형식을 정규화하는 함수"""
|
34 |
# 먼저 모든 따옴표를 정리
|
35 |
text = clean_quotes(text)
|
36 |
|
37 |
+
if is_korean(text):
|
38 |
+
# 한글 문장의 경우, 첫 번째 명사구만 따옴표로 처리
|
39 |
+
words = text.split()
|
40 |
+
first_word = words[0]
|
41 |
+
if not (first_word.startswith("'") and first_word.endswith("'")):
|
42 |
+
words[0] = f"'{first_word}'"
|
43 |
+
return ' '.join(words)
|
44 |
+
else:
|
45 |
+
# 영어 문장의 경우, 첫 단어만 따옴표로 처리
|
46 |
+
words = text.split()
|
47 |
+
if words:
|
48 |
+
if not (words[0].startswith("'") and words[0].endswith("'")):
|
49 |
+
words[0] = f"'{words[0]}'"
|
50 |
+
return ' '.join(words)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def find_quoted_words(text):
|
53 |
"""작은따옴표로 묶인 단어들을 찾는 함수"""
|
|
|
59 |
|
60 |
def is_english(text):
|
61 |
"""텍스트가 영어인지 확인하는 함수"""
|
62 |
+
# 따옴표와 기본 문장부호를 제거하고 영어 알파벳과 공백만 남김
|
63 |
+
cleaned_text = re.sub(r'[^A-Za-z\s]', '', text)
|
64 |
+
# 알파벳이 하나라도 있는지 확인
|
65 |
+
has_letters = bool(re.search('[A-Za-z]', cleaned_text))
|
66 |
+
# 알파벳과 공백 외의 문자가 없는지 확인
|
67 |
+
is_only_english = bool(re.match(r'^[A-Za-z\s]*$', cleaned_text))
|
68 |
+
return has_letters and is_only_english
|
69 |
|
70 |
def translate_korean_to_english(text):
|
71 |
"""전체 텍스트 번역 함수"""
|
|
|
75 |
|
76 |
# 영어 입력 확인
|
77 |
if is_english(text):
|
78 |
+
# 영어 입력의 경우 첫 단어만 따옴표로 처리
|
79 |
+
words = text.split()
|
80 |
+
if words:
|
81 |
+
# 이미 따옴표가 있는 경우는 그대로, 없는 경우 추가
|
82 |
+
if not (words[0].startswith("'") and words[0].endswith("'")):
|
83 |
+
words[0] = f"'{words[0]}'"
|
84 |
+
return ' '.join(words)
|
85 |
return text
|
86 |
|
87 |
+
# 한글 입력 처리
|
88 |
+
# 따옴표로 묶인 단어 찾기
|
89 |
quoted_words = re.findall(r"'([^']*)'", text)
|
90 |
+
if not quoted_words:
|
91 |
+
# 따옴표로 묶인 단어가 없는 경우, 첫 단어를 따옴표로 묶기
|
92 |
+
words = text.split()
|
93 |
+
if words:
|
94 |
+
text = f"'{words[0]}'" + text[len(words[0]):]
|
95 |
+
quoted_words = [words[0]]
|
96 |
|
97 |
+
# 첫 번째 따옴표 단어 번역
|
98 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
params = {
|
100 |
"client": "gtx",
|
101 |
"sl": "ko",
|
102 |
"tl": "en",
|
103 |
"dt": "t",
|
104 |
+
"q": quoted_words[0]
|
105 |
}
|
106 |
response = requests.get(url, params=params)
|
|
|
107 |
if response.status_code == 200:
|
108 |
+
translated_word = response.json()[0][0][0].upper()
|
109 |
+
# 임시 마커로 대체
|
110 |
+
text = text.replace(f"'{quoted_words[0]}'", "QUOTED_WORD_MARKER")
|
111 |
+
|
112 |
+
# 전체 문장 번역
|
113 |
+
params["q"] = text
|
114 |
+
response = requests.get(url, params=params)
|
115 |
+
if response.status_code == 200:
|
116 |
+
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
117 |
+
# 마커를 번역된 단어로 대체
|
118 |
+
translated_text = translated_text.replace("QUOTED_WORD_MARKER", f"'{translated_word}'")
|
119 |
+
return translated_text
|
120 |
+
|
121 |
+
return text
|
122 |
except Exception as e:
|
123 |
print(f"Translation error: {e}")
|
124 |
return text
|
125 |
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
@app.route('/')
|
130 |
def index():
|
131 |
return render_template('index.html', title=app.config['TITLE'])
|
|
|
146 |
if not english_text:
|
147 |
raise Exception("Translation failed")
|
148 |
|
149 |
+
# 따옴표로 묶인 단어 추출 (첫 번째 단어만)
|
150 |
+
quoted_words = re.findall(r"'([^']*)'", english_text)
|
151 |
+
first_quoted_word = quoted_words[0] if quoted_words else None
|
152 |
|
153 |
# ASL 변환을 위해 따옴표 제거
|
154 |
clean_english = re.sub(r"'([^']*)'", r"\1", english_text)
|
|
|
161 |
|
162 |
for word in words:
|
163 |
word_upper = word.upper()
|
164 |
+
if first_quoted_word and word_upper == first_quoted_word.upper():
|
165 |
# 고유명사인 경우 철자를 하나씩 분리
|
166 |
spelled_word = spell_out_word(word)
|
167 |
processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
|