ginipick commited on
Commit
b5b4dbb
·
verified ·
1 Parent(s): 8bd4269

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +25 -13
src/main.py CHANGED
@@ -81,9 +81,12 @@ def translate_korean_to_english(text):
81
  quoted_match = re.search(r"'([^']*)'", text)
82
  if quoted_match:
83
  quoted_word = quoted_match.group(1)
 
84
 
85
  # 따옴표로 묶인 단어 먼저 번역
86
  url = "https://translate.googleapis.com/translate_a/single"
 
 
87
  params = {
88
  "client": "gtx",
89
  "sl": "ko",
@@ -95,17 +98,28 @@ def translate_korean_to_english(text):
95
  if response.status_code == 200:
96
  translated_word = response.json()[0][0][0].upper()
97
 
98
- # 원본 텍스트에서 따옴표로 묶인 부분을 임시 마커로 대체
99
- text = text.replace(f"'{quoted_word}'", "QUOTED_WORD_MARKER")
100
-
101
- # 나머지 문장 번역
102
- params["q"] = text
103
- response = requests.get(url, params=params)
104
- if response.status_code == 200:
105
- translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
106
- # 마커를 번역된 단어로 대체
107
- translated_text = translated_text.replace("QUOTED_WORD_MARKER", f"'{translated_word}'")
108
- return translated_text
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  return text
111
  except Exception as e:
@@ -113,8 +127,6 @@ def translate_korean_to_english(text):
113
  return text
114
 
115
 
116
-
117
-
118
  @app.route('/')
119
  def index():
120
  return render_template('index.html', title=app.config['TITLE'])
 
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",
 
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
  return text
128
 
129
 
 
 
130
  @app.route('/')
131
  def index():
132
  return render_template('index.html', title=app.config['TITLE'])