ginipick commited on
Commit
669ff5b
·
verified ·
1 Parent(s): b5b4dbb

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +48 -45
src/main.py CHANGED
@@ -61,6 +61,48 @@ def spell_out_word(word):
61
  """단어를 개별 알파벳으로 분리하는 함수"""
62
  return ' '.join(list(word.lower()))
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  def translate_korean_to_english(text):
66
  """전체 텍스트 번역 함수"""
@@ -70,56 +112,16 @@ def translate_korean_to_english(text):
70
 
71
  # 영어 입력 확인
72
  if is_english(text):
73
- # 영어 입력의 경우 따옴표 처리만 하고 그대로 반환
74
  quoted_match = re.search(r"'([^']*)'", text)
75
  if quoted_match:
76
  quoted_word = quoted_match.group(1).upper()
77
  text = re.sub(r"'[^']*'", f"'{quoted_word}'", text, 1)
78
  return text
79
 
80
- # 한글 입력 처리
81
- quoted_match = re.search(r"'([^']*)'", text)
82
- if quoted_match:
83
- quoted_word = quoted_match.group(1)
84
- rest_of_text = text[text.find("'", text.find(quoted_word) + len(quoted_word)) + 1:].strip()
85
-
86
- # 따옴표로 묶인 단어 먼저 번역
87
- url = "https://translate.googleapis.com/translate_a/single"
88
-
89
- # 1. 고유명사 번역
90
- params = {
91
- "client": "gtx",
92
- "sl": "ko",
93
- "tl": "en",
94
- "dt": "t",
95
- "q": quoted_word
96
- }
97
- response = requests.get(url, params=params)
98
- if response.status_code == 200:
99
- translated_word = response.json()[0][0][0].upper()
100
-
101
- # 2. 나머지 문장 번역
102
- if rest_of_text:
103
- params["q"] = rest_of_text
104
- response = requests.get(url, params=params)
105
- if response.status_code == 200:
106
- rest_translated = ' '.join(item[0] for item in response.json()[0] if item[0])
107
- return f"'{translated_word}' {rest_translated}"
108
- else:
109
- return f"'{translated_word}'"
110
-
111
- # 따옴표가 없는 경우 전체 문장 번역
112
- url = "https://translate.googleapis.com/translate_a/single"
113
- params = {
114
- "client": "gtx",
115
- "sl": "ko",
116
- "tl": "en",
117
- "dt": "t",
118
- "q": text
119
- }
120
- response = requests.get(url, params=params)
121
- if response.status_code == 200:
122
- return ' '.join(item[0] for item in response.json()[0] if item[0])
123
 
124
  return text
125
  except Exception as e:
@@ -127,6 +129,7 @@ def translate_korean_to_english(text):
127
  return text
128
 
129
 
 
130
  @app.route('/')
131
  def index():
132
  return render_template('index.html', title=app.config['TITLE'])
@@ -278,4 +281,4 @@ def download_video(gloss_sentence):
278
  return f"Error downloading video: {str(e)}", 500
279
 
280
  if __name__ == "__main__":
281
- app.run(host="0.0.0.0", port=7860, debug=True)
 
61
  """단어를 개별 알파벳으로 분리하는 함수"""
62
  return ' '.join(list(word.lower()))
63
 
64
+ def translate_korean_text(text):
65
+ """한글 전용 번역 함수"""
66
+ try:
67
+ # 따옴표로 묶인 단어 찾기
68
+ quoted_match = re.search(r"'([^']*)'", text)
69
+ if not quoted_match:
70
+ return text
71
+
72
+ # 1. 따옴표 안의 단어만 먼저 번역
73
+ korean_word = quoted_match.group(1)
74
+ url = "https://translate.googleapis.com/translate_a/single"
75
+ params = {
76
+ "client": "gtx",
77
+ "sl": "ko",
78
+ "tl": "en",
79
+ "dt": "t",
80
+ "q": korean_word
81
+ }
82
+ response = requests.get(url, params=params)
83
+ if response.status_code != 200:
84
+ return text
85
+
86
+ proper_noun = response.json()[0][0][0].upper()
87
+
88
+ # 2. 나머지 문장만 따로 번역
89
+ remaining_text = text[text.find("'", text.find(korean_word) + len(korean_word)) + 1:].strip()
90
+ if not remaining_text:
91
+ return f"'{proper_noun}'"
92
+
93
+ params["q"] = remaining_text
94
+ response = requests.get(url, params=params)
95
+ if response.status_code != 200:
96
+ return text
97
+
98
+ remaining_translation = ' '.join(item[0] for item in response.json()[0] if item[0])
99
+
100
+ # 3. 번역된 부분들 조합
101
+ return f"'{proper_noun}' {remaining_translation}"
102
+
103
+ except Exception as e:
104
+ print(f"Korean translation error: {e}")
105
+ return text
106
 
107
  def translate_korean_to_english(text):
108
  """전체 텍스트 번역 함수"""
 
112
 
113
  # 영어 입력 확인
114
  if is_english(text):
115
+ # 기존 영어 처리 방식 유지
116
  quoted_match = re.search(r"'([^']*)'", text)
117
  if quoted_match:
118
  quoted_word = quoted_match.group(1).upper()
119
  text = re.sub(r"'[^']*'", f"'{quoted_word}'", text, 1)
120
  return text
121
 
122
+ # 한글 입력인 경우 새로운 함수로 처리
123
+ if is_korean(text):
124
+ return translate_korean_text(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  return text
127
  except Exception as e:
 
129
  return text
130
 
131
 
132
+
133
  @app.route('/')
134
  def index():
135
  return render_template('index.html', title=app.config['TITLE'])
 
281
  return f"Error downloading video: {str(e)}", 500
282
 
283
  if __name__ == "__main__":
284
+ app.run(host="0.0.0.0", port=7860, debug=True)