Spaces:
Paused
Paused
Update src/main.py
Browse files- src/main.py +42 -49
src/main.py
CHANGED
|
@@ -17,59 +17,52 @@ app.config['TITLE'] = 'Sign Language Translate'
|
|
| 17 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 18 |
dataset, list_2000_tokens = dg.load_data()
|
| 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):
|
| 27 |
"""단어를 개별 알파벳으로 분리하는 함수"""
|
| 28 |
return ' '.join(list(word.lower()))
|
| 29 |
|
| 30 |
-
def translate_quoted_word(word):
|
| 31 |
-
"""따옴표 안의 단어를 개별적으로 번역"""
|
| 32 |
-
try:
|
| 33 |
-
url = "https://translate.googleapis.com/translate_a/single"
|
| 34 |
-
params = {
|
| 35 |
-
"client": "gtx",
|
| 36 |
-
"sl": "ko",
|
| 37 |
-
"tl": "en",
|
| 38 |
-
"dt": "t",
|
| 39 |
-
"q": word
|
| 40 |
-
}
|
| 41 |
-
response = requests.get(url, params=params)
|
| 42 |
-
if response.status_code == 200:
|
| 43 |
-
translated = response.json()[0][0][0].upper()
|
| 44 |
-
return translated
|
| 45 |
-
return word
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Word translation error: {e}")
|
| 48 |
-
return word
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def normalize_quotes(text):
|
| 53 |
-
"""따옴표 형식을 정규화하는 함수"""
|
| 54 |
-
# 따옴표가 없는 단어 끝에 붙은 따옴표 처리
|
| 55 |
-
text = re.sub(r"(\w+)'", r"'\1'", text)
|
| 56 |
-
# 한쪽 따옴표만 있는 경우 처리
|
| 57 |
-
text = re.sub(r"'(\w+)(?!')", r"'\1'", text)
|
| 58 |
-
text = re.sub(r"(?<!')(\w+)'", r"'\1'", text)
|
| 59 |
-
return text
|
| 60 |
-
|
| 61 |
-
def normalize_quotes(text):
|
| 62 |
-
"""따옴표 형식을 정규화하는 함수"""
|
| 63 |
-
# 따옴표가 없는 단어 끝에 붙은 따옴표 처리
|
| 64 |
-
text = re.sub(r"(\w+)'", r"'\1'", text)
|
| 65 |
-
# 한쪽 따옴표만 있는 경우 처리
|
| 66 |
-
text = re.sub(r"'(\w+)(?!')", r"'\1'", text)
|
| 67 |
-
text = re.sub(r"(?<!')(\w+)'", r"'\1'", text)
|
| 68 |
-
return text
|
| 69 |
-
|
| 70 |
def is_english(text):
|
| 71 |
"""텍스트가 영어인지 확인하는 함수"""
|
| 72 |
-
# 영어 알파벳과 기본적인 문장부호만 포함되어 있는지 확인
|
| 73 |
english_pattern = re.compile(r'^[A-Za-z\s\'".,!?-]+$')
|
| 74 |
return bool(english_pattern.match(text.replace("'", "")))
|
| 75 |
|
|
@@ -89,6 +82,8 @@ def translate_korean_to_english(text):
|
|
| 89 |
|
| 90 |
# 따옴표 안의 단어들 먼저 번역
|
| 91 |
for word in quoted_words:
|
|
|
|
|
|
|
| 92 |
url = "https://translate.googleapis.com/translate_a/single"
|
| 93 |
params = {
|
| 94 |
"client": "gtx",
|
|
@@ -128,6 +123,10 @@ def translate_korean_to_english(text):
|
|
| 128 |
print(f"Translation error: {e}")
|
| 129 |
return text
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
@app.route('/translate/', methods=['POST'])
|
| 132 |
def result():
|
| 133 |
if request.method == 'POST':
|
|
@@ -242,12 +241,6 @@ def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
|
| 242 |
print(f"Error generating video: {str(e)}")
|
| 243 |
raise
|
| 244 |
|
| 245 |
-
@app.route('/')
|
| 246 |
-
def index():
|
| 247 |
-
return render_template('index.html', title=app.config['TITLE'])
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
@app.route('/video_feed')
|
| 252 |
def video_feed():
|
| 253 |
sentence = request.args.get('gloss_sentence_to_display', '')
|
|
|
|
| 17 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 18 |
dataset, list_2000_tokens = dg.load_data()
|
| 19 |
|
| 20 |
+
def clean_quotes(text):
|
| 21 |
+
"""따옴표 정리 함수"""
|
| 22 |
+
# 연속된 따옴표 제거
|
| 23 |
+
text = re.sub(r"'+", "'", text)
|
| 24 |
+
# 단어 중간의 따옴표 제거
|
| 25 |
+
text = re.sub(r"(\w)'(\w)", r"\1\2", text)
|
| 26 |
+
return text
|
| 27 |
+
|
| 28 |
+
def normalize_quotes(text):
|
| 29 |
+
"""따옴표 형식을 정규화하는 함수"""
|
| 30 |
+
# 먼저 모든 따옴표를 정리
|
| 31 |
+
text = clean_quotes(text)
|
| 32 |
+
|
| 33 |
+
# 한글 또는 영어 단어를 찾아서 처리
|
| 34 |
+
pattern = r'([가-힣A-Za-z]+)'
|
| 35 |
+
|
| 36 |
+
def process_match(match):
|
| 37 |
+
word = match.group(1)
|
| 38 |
+
# 이미 따옴표로 둘러싸인 경우는 처리하지 않음
|
| 39 |
+
if not re.match(r"'.*'", word):
|
| 40 |
+
return f"'{word}'"
|
| 41 |
+
return word
|
| 42 |
+
|
| 43 |
+
# 단어 단위로 처리
|
| 44 |
+
words = text.split()
|
| 45 |
+
processed_words = []
|
| 46 |
+
|
| 47 |
+
for word in words:
|
| 48 |
+
if re.search(pattern, word):
|
| 49 |
+
# 이미 따옴표가 있는 경우는 그대로 두고, 없는 경우만 추가
|
| 50 |
+
if not word.startswith("'") and not word.endswith("'"):
|
| 51 |
+
word = f"'{word}'"
|
| 52 |
+
processed_words.append(word)
|
| 53 |
+
|
| 54 |
+
return ' '.join(processed_words)
|
| 55 |
+
|
| 56 |
def find_quoted_words(text):
|
| 57 |
"""작은따옴표로 묶인 단어들을 찾는 함수"""
|
|
|
|
|
|
|
| 58 |
return re.findall(r"'([^']*)'", text)
|
| 59 |
|
| 60 |
def spell_out_word(word):
|
| 61 |
"""단어를 개별 알파벳으로 분리하는 함수"""
|
| 62 |
return ' '.join(list(word.lower()))
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def is_english(text):
|
| 65 |
"""텍스트가 영어인지 확인하는 함수"""
|
|
|
|
| 66 |
english_pattern = re.compile(r'^[A-Za-z\s\'".,!?-]+$')
|
| 67 |
return bool(english_pattern.match(text.replace("'", "")))
|
| 68 |
|
|
|
|
| 82 |
|
| 83 |
# 따옴표 안의 단어들 먼저 번역
|
| 84 |
for word in quoted_words:
|
| 85 |
+
if not word.strip(): # 빈 문자열 건너뛰기
|
| 86 |
+
continue
|
| 87 |
url = "https://translate.googleapis.com/translate_a/single"
|
| 88 |
params = {
|
| 89 |
"client": "gtx",
|
|
|
|
| 123 |
print(f"Translation error: {e}")
|
| 124 |
return text
|
| 125 |
|
| 126 |
+
@app.route('/')
|
| 127 |
+
def index():
|
| 128 |
+
return render_template('index.html', title=app.config['TITLE'])
|
| 129 |
+
|
| 130 |
@app.route('/translate/', methods=['POST'])
|
| 131 |
def result():
|
| 132 |
if request.method == 'POST':
|
|
|
|
| 241 |
print(f"Error generating video: {str(e)}")
|
| 242 |
raise
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
@app.route('/video_feed')
|
| 245 |
def video_feed():
|
| 246 |
sentence = request.args.get('gloss_sentence_to_display', '')
|