ginipick commited on
Commit
c8c948e
·
verified ·
1 Parent(s): c082dde

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +25 -33
src/main.py CHANGED
@@ -31,23 +31,30 @@ def is_korean(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
  """작은따옴표로 묶인 단어들을 찾는 함수"""
@@ -75,41 +82,29 @@ def translate_korean_to_english(text):
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:
@@ -123,9 +118,6 @@ def translate_korean_to_english(text):
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'])
 
31
 
32
  def normalize_quotes(text):
33
  """따옴표 형식을 정규화하는 함수"""
34
+ # 연속된 따옴표 제거
35
+ text = re.sub(r"'+", "'", text)
36
+ # 불필요한 공백 제거
37
+ text = re.sub(r'\s+', ' ', text).strip()
38
 
39
  if is_korean(text):
40
+ # 한글 문장의 경우
41
  words = text.split()
42
+ if words:
43
+ # 번째 단어에서 따옴표 제거 후 다시 추가
44
+ first_word = words[0].replace("'", "")
45
  words[0] = f"'{first_word}'"
46
+ # 나머지 단어들에서 따옴표 제거
47
+ words[1:] = [w.replace("'", "") for w in words[1:]]
48
+ return ' '.join(words)
49
  else:
50
  # 영어 문장의 경우, 첫 단어만 따옴표로 처리
51
  words = text.split()
52
  if words:
53
+ # 번째 단어에서 따옴표 제거 후 다시 추가
54
+ first_word = words[0].replace("'", "")
55
+ words[0] = f"'{first_word}'"
56
+ return ' '.join(words)
57
+ return text
58
 
59
  def find_quoted_words(text):
60
  """작은따옴표로 묶인 단어들을 찾는 함수"""
 
82
 
83
  # 영어 입력 확인
84
  if is_english(text):
 
 
 
 
 
 
 
85
  return text
86
 
87
  # 한글 입력 처리
88
  # 따옴표로 묶인 단어 찾기
89
+ quoted_word = re.findall(r"'([^']*)'", text)[0]
90
+
91
+ # 따옴표로 묶인 단어 먼저 번역
 
 
 
 
 
 
92
  url = "https://translate.googleapis.com/translate_a/single"
93
  params = {
94
  "client": "gtx",
95
  "sl": "ko",
96
  "tl": "en",
97
  "dt": "t",
98
+ "q": quoted_word
99
  }
100
  response = requests.get(url, params=params)
101
  if response.status_code == 200:
102
  translated_word = response.json()[0][0][0].upper()
103
+
104
+ # 원본 텍스트에서 따옴표로 묶인 부분을 임시 마커로 대체
105
+ text = text.replace(f"'{quoted_word}'", "QUOTED_WORD_MARKER")
106
+
107
+ # 나머지 문장 번역
108
  params["q"] = text
109
  response = requests.get(url, params=params)
110
  if response.status_code == 200:
 
118
  print(f"Translation error: {e}")
119
  return text
120
 
 
 
 
121
  @app.route('/')
122
  def index():
123
  return render_template('index.html', title=app.config['TITLE'])