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

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +55 -60
src/main.py CHANGED
@@ -29,32 +29,7 @@ def is_korean(text):
29
  """한글이 포함되어 있는지 확인"""
30
  return bool(re.search('[가-힣]', text))
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
  """작은따옴표로 묶인 단어들을 찾는 함수"""
@@ -64,16 +39,6 @@ def spell_out_word(word):
64
  """단어를 개별 알파벳으로 분리하는 함수"""
65
  return ' '.join(list(word.lower()))
66
 
67
- def is_english(text):
68
- """텍스트가 영어인지 확인하는 함수"""
69
- # 따옴표와 기본 문장부호를 제거하고 영어 알파벳과 공백만 남김
70
- cleaned_text = re.sub(r'[^A-Za-z\s]', '', text)
71
- # 알파벳이 하나라도 있는지 확인
72
- has_letters = bool(re.search('[A-Za-z]', cleaned_text))
73
- # 알파벳과 공백 외의 문자가 없는지 확인
74
- is_only_english = bool(re.match(r'^[A-Za-z\s]*$', cleaned_text))
75
- return has_letters and is_only_english
76
-
77
  def translate_korean_to_english(text):
78
  """전체 텍스트 번역 함수"""
79
  try:
@@ -82,42 +47,72 @@ def translate_korean_to_english(text):
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:
111
- translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
112
- # 마커를 번역된 단어로 대체
113
- translated_text = translated_text.replace("QUOTED_WORD_MARKER", f"'{translated_word}'")
114
- return translated_text
 
 
 
 
 
 
 
 
 
115
 
116
  return text
117
  except Exception as e:
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'])
 
29
  """한글이 포함되어 있는지 확인"""
30
  return bool(re.search('[가-힣]', text))
31
 
32
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def find_quoted_words(text):
35
  """작은따옴표로 묶인 단어들을 찾는 함수"""
 
39
  """단어를 개별 알파벳으로 분리하는 함수"""
40
  return ' '.join(list(word.lower()))
41
 
 
 
 
 
 
 
 
 
 
 
42
  def translate_korean_to_english(text):
43
  """전체 텍스트 번역 함수"""
44
  try:
 
47
 
48
  # 영어 입력 확인
49
  if is_english(text):
50
+ # 영어 입력의 경우 따옴표 처리만 하고 그대로 반환
51
+ quoted_match = re.search(r"'([^']*)'", text)
52
+ if quoted_match:
53
+ quoted_word = quoted_match.group(1).upper()
54
+ text = re.sub(r"'[^']*'", f"'{quoted_word}'", text, 1)
55
  return text
56
 
57
  # 한글 입력 처리
58
+ quoted_match = re.search(r"'([^']*)'", text)
59
+ if quoted_match:
60
+ quoted_word = quoted_match.group(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ # 따옴표로 묶인 단어 먼저 번역
63
+ url = "https://translate.googleapis.com/translate_a/single"
64
+ params = {
65
+ "client": "gtx",
66
+ "sl": "ko",
67
+ "tl": "en",
68
+ "dt": "t",
69
+ "q": quoted_word
70
+ }
71
  response = requests.get(url, params=params)
72
  if response.status_code == 200:
73
+ translated_word = response.json()[0][0][0].upper()
74
+
75
+ # 원본 텍스트에서 따옴표로 묶인 부분을 임시 마커로 대체
76
+ text = text.replace(f"'{quoted_word}'", "QUOTED_WORD_MARKER")
77
+
78
+ # 나머지 문장 번역
79
+ params["q"] = text
80
+ response = requests.get(url, params=params)
81
+ if response.status_code == 200:
82
+ translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
83
+ # 마커를 번역된 단어로 대체
84
+ translated_text = translated_text.replace("QUOTED_WORD_MARKER", f"'{translated_word}'")
85
+ return translated_text
86
 
87
  return text
88
  except Exception as e:
89
  print(f"Translation error: {e}")
90
  return text
91
 
92
+ def is_english(text):
93
+ """텍��트가 영어인지 확인하는 함수"""
94
+ # 따옴표와 공백을 제외한 나머지 텍스트 확인
95
+ text_without_quotes = re.sub(r"'[^']*'|\s", "", text)
96
+ # 영어 알파벳과 기본 문장부호만 포함되어 있는지 확인
97
+ return bool(re.match(r'^[A-Za-z.,!?-]*$', text_without_quotes))
98
+
99
+ def normalize_quotes(text):
100
+ """따옴표 형식을 정규화하는 함수"""
101
+ # 연속된 따옴표 제거
102
+ text = re.sub(r"'+", "'", text)
103
+ # 불필요한 공백 제거
104
+ text = re.sub(r'\s+', ' ', text).strip()
105
+
106
+ # 첫 번째 단어에만 따옴표 처리
107
+ words = text.split()
108
+ if words:
109
+ # 모든 따옴표 제거 후 첫 단어에만 따옴표 추가
110
+ first_word = words[0].replace("'", "")
111
+ words[0] = f"'{first_word}'"
112
+ words[1:] = [w.replace("'", "") for w in words[1:]]
113
+ return ' '.join(words)
114
+ return text
115
+
116
  @app.route('/')
117
  def index():
118
  return render_template('index.html', title=app.config['TITLE'])