ginipick commited on
Commit
fb8ddc5
·
verified ·
1 Parent(s): c050c45

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +39 -26
src/main.py CHANGED
@@ -19,6 +19,8 @@ dataset, list_2000_tokens = dg.load_data()
19
 
20
  def find_quoted_words(text):
21
  """작은따옴표로 묶인 단어들을 찾는 함수"""
 
 
22
  return re.findall(r"'([^']*)'", text)
23
 
24
  def spell_out_word(word):
@@ -48,35 +50,46 @@ def translate_quoted_word(word):
48
  def translate_korean_to_english(text):
49
  """전체 텍스트 번역 함수"""
50
  try:
 
 
 
 
 
 
51
  # 1. 따옴표로 묶인 부분을 찾아서 따로 번역
52
- pattern = r"'([^']*)'|([^']+)"
53
- parts = re.findall(pattern, text)
54
- translated_parts = []
55
 
56
- for quoted, unquoted in parts:
57
- if quoted: # 따옴표로 묶인 부분
58
- translated_word = translate_quoted_word(quoted)
59
- translated_parts.append(f"'{translated_word}'")
60
- elif unquoted: # 일반 텍스트
61
- # 일반 텍스트 번역
62
- url = "https://translate.googleapis.com/translate_a/single"
63
- params = {
64
- "client": "gtx",
65
- "sl": "ko",
66
- "tl": "en",
67
- "dt": "t",
68
- "q": unquoted.strip()
69
- }
70
- response = requests.get(url, params=params)
71
- if response.status_code == 200:
72
- translated = ' '.join(item[0] for item in response.json()[0] if item[0])
73
- translated_parts.append(translated)
74
- else:
75
- translated_parts.append(unquoted)
76
 
77
- # 번역된 부분들을 합치기
78
- result = ''.join(translated_parts).strip()
79
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  except Exception as e:
81
  print(f"Translation error: {e}")
82
  return text
 
19
 
20
  def find_quoted_words(text):
21
  """작은따옴표로 묶인 단어들을 찾는 함수"""
22
+ # 따옴표가 없는 단어에 따옴표 추가 (예: 한국'을 '한국'으로)
23
+ text = re.sub(r"(\w+)'", r"'\1'", text)
24
  return re.findall(r"'([^']*)'", text)
25
 
26
  def spell_out_word(word):
 
50
  def translate_korean_to_english(text):
51
  """전체 텍스트 번역 함수"""
52
  try:
53
+ # 입력 텍스트에서 따옴표 형식 검사 및 수정
54
+ text = text.replace("'", "'").replace("'", "'") # 스마트 따옴표를 일반 따옴표로 변환
55
+
56
+ # 잘못된 따옴표 위치 수정 (예: 한국'을 '한국'으로)
57
+ text = re.sub(r"(\w+)'", r"'\1'", text)
58
+
59
  # 1. 따옴표로 묶인 부분을 찾아서 따로 번역
60
+ quoted_words = find_quoted_words(text)
61
+ translated_quoted = {}
 
62
 
63
+ # 따옴표 안의 단어들 먼저 번역
64
+ for word in quoted_words:
65
+ translated = translate_quoted_word(word)
66
+ translated_quoted[word] = translated
67
+ # 임시 마커로 대체
68
+ text = text.replace(f"'{word}'", f"QUOTED_{len(translated_quoted)}_")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # 전체 문장 번역
71
+ url = "https://translate.googleapis.com/translate_a/single"
72
+ params = {
73
+ "client": "gtx",
74
+ "sl": "ko",
75
+ "tl": "en",
76
+ "dt": "t",
77
+ "q": text
78
+ }
79
+ response = requests.get(url, params=params)
80
+
81
+ if response.status_code == 200:
82
+ translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
83
+
84
+ # 번역된 텍스트에서 마커를 번역된 단어로 대체
85
+ for i, (original, translated) in enumerate(translated_quoted.items(), 1):
86
+ translated_text = translated_text.replace(f"QUOTED_{i}_", f"'{translated}'")
87
+
88
+ # 불필요한 공백 정리
89
+ translated_text = re.sub(r'\s+', ' ', translated_text).strip()
90
+ return translated_text
91
+ else:
92
+ raise Exception(f"Translation API returned status code: {response.status_code}")
93
  except Exception as e:
94
  print(f"Translation error: {e}")
95
  return text