ginipick commited on
Commit
0082fef
·
verified ·
1 Parent(s): 27b91a6

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +14 -14
src/main.py CHANGED
@@ -77,44 +77,44 @@ def spell_out_word(word):
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:
 
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
+
87
+ # 2. 전체 문장을 먼저 번역
 
 
88
  url = "https://translate.googleapis.com/translate_a/single"
89
  params = {
90
  "client": "gtx",
91
  "sl": "ko",
92
  "tl": "en",
93
  "dt": "t",
94
+ "q": text.replace(f"'{quoted_word}'", "NAME") # 임시로 'NAME'으로 대체
95
  }
96
  response = requests.get(url, params=params)
97
  if response.status_code != 200:
98
  return text
99
 
 
100
  translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
101
 
102
+ # 3. 따옴표 안의 단어 처리
103
+ if re.match(r'^[A-Za-z]+$', quoted_word): # 영어 단어인 경우
104
+ proper_noun = quoted_word.upper()
105
+ else: # 한글 단어인 경우
 
106
  params["q"] = quoted_word
107
  response = requests.get(url, params=params)
108
  if response.status_code == 200:
109
  proper_noun = response.json()[0][0][0].upper()
 
110
  else:
111
+ proper_noun = quoted_word.upper()
112
 
113
+ # 4. 최종 문장 조합
114
+ final_text = translated_text.replace("NAME", f"'{proper_noun}'")
115
+ if "name is" in final_text.lower(): # 이름 관련 문장인 경우
116
+ final_text = re.sub(r"name is\s+'", "name is '", final_text, flags=re.IGNORECASE)
117
+
118
  return final_text
119
 
120
  except Exception as e: