ginipick commited on
Commit
27b91a6
·
verified ·
1 Parent(s): 262da5d

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +23 -19
src/main.py CHANGED
@@ -77,41 +77,45 @@ def spell_out_word(word):
77
  def translate_korean_text(text):
78
  """한글 전용 번역 함수"""
79
  try:
80
- # 따옴표로 묶인 단어 찾기
81
  quoted_match = re.search(r"'([^']*)'", text)
82
  if not quoted_match:
83
  return text
84
 
85
- # 1. 따옴표 안의 단어만 먼저 번역
86
- korean_word = quoted_match.group(1)
 
 
 
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": korean_word
94
  }
95
  response = requests.get(url, params=params)
96
  if response.status_code != 200:
97
  return text
98
-
99
- proper_noun = response.json()[0][0][0].upper()
100
-
101
- # 2. 나머지 문장만 따로 번역
102
- remaining_text = text[text.find("'", text.find(korean_word) + len(korean_word)) + 1:].strip()
103
- if not remaining_text:
104
- return f"'{proper_noun}'"
105
-
106
- params["q"] = remaining_text
107
- response = requests.get(url, params=params)
108
- if response.status_code != 200:
109
- return text
110
 
111
- remaining_translation = ' '.join(item[0] for item in response.json()[0] if item[0])
 
112
 
113
- # 3. 번역된 부분들 조합
114
- return f"'{proper_noun}' {remaining_translation}"
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  except Exception as e:
117
  print(f"Korean translation error: {e}")
 
77
  def translate_korean_text(text):
78
  """한글 전용 번역 함수"""
79
  try:
80
+ # 1. 따옴표로 묶인 단어 찾기 및 임시 마커로 대체
81
  quoted_match = re.search(r"'([^']*)'", text)
82
  if not quoted_match:
83
  return text
84
 
85
+ quoted_word = quoted_match.group(1)
86
+ marker = "PROPER_NOUN_MARKER"
87
+ text_with_marker = text.replace(f"'{quoted_word}'", marker)
88
+
89
+ # 2. 전체 문장 번역
90
  url = "https://translate.googleapis.com/translate_a/single"
91
  params = {
92
  "client": "gtx",
93
  "sl": "ko",
94
  "tl": "en",
95
  "dt": "t",
96
+ "q": text_with_marker
97
  }
98
  response = requests.get(url, params=params)
99
  if response.status_code != 200:
100
  return text
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ # 3. 번역된 전체 문장에서 마커를 원래 단어로 대체
103
+ translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
104
 
105
+ # 영어로 단어는 그대로 유지
106
+ if re.match(r'^[A-Z]+$', quoted_word):
107
+ final_text = translated_text.replace(marker, f"'{quoted_word}'")
108
+ else:
109
+ # 한글 단어는 번역
110
+ params["q"] = quoted_word
111
+ response = requests.get(url, params=params)
112
+ if response.status_code == 200:
113
+ proper_noun = response.json()[0][0][0].upper()
114
+ final_text = translated_text.replace(marker, f"'{proper_noun}'")
115
+ else:
116
+ final_text = translated_text.replace(marker, f"'{quoted_word}'")
117
+
118
+ return final_text
119
 
120
  except Exception as e:
121
  print(f"Korean translation error: {e}")