- Fix missing symbols and optimize performance of hyphenation

This commit is contained in:
Vadim committed 2023-06-18 01:36:03 +02:00
1 parent d00b063e4b
commit d82b655412
3 files changed
+72 -9

No files matched your search

@@ -0,0 +1,15 @@
"""Tests for hyphenation.py"""
from src.modules.Speech_Recognition.hyphenation import hyphenation
def test_hypenation():
"""Test case for hyphenation function."""
# test
assert hyphenation("begegnen", "de_DE") == ["be", "geg", "nen"]
assert hyphenation(".b,e~g'eg*nen, ", "de_DE") == [".b,e", "~g'eg", "*nen, "]
assert hyphenation("Abend, ", "de_AT") == None
assert hyphenation("Abend.", "de_AT") == None
+5 -3
View File
@@ -45,7 +45,7 @@ from src.modules.Pitcher.pitcher import (
get_pitch_with_crepe_file,
)
from src.modules.Pitcher.pitched_data import PitchedData
from src.modules.Speech_Recognition.hyphenation import hyphenation, language_check
from src.modules.Speech_Recognition.hyphenation import hyphenation, language_check, create_hyphenator
from src.modules.Speech_Recognition.Vosk import transcribe_with_vosk
from src.modules.Speech_Recognition.Whisper import transcribe_with_whisper
from src.modules.Ultrastar import ultrastar_score_calculator, ultrastar_writer, ultrastar_converter, ultrastar_parser
@@ -230,9 +230,11 @@ def hyphenate_each_word(language: str, transcribed_data: list[TranscribedData])
return None
sleep(0.1)
for i, data in tqdm(enumerate(transcribed_data)):
hyphenator = create_hyphenator(lang_region)
for i in tqdm(enumerate(transcribed_data)):
pos = i[0]
hyphenated_word.append(
hyphenation(data.word, lang_region)
hyphenation(transcribed_data[pos].word, hyphenator)
)
return hyphenated_word
+52 -6
View File
@@ -36,20 +36,66 @@ def contains_punctuation(word: str) -> bool:
return any(elem in word for elem in string.punctuation)
def hyphenation(word: str, lang_region: str) -> list[str] | None:
def clean_word(word: str):
"""Remove punctuation from word"""
cleaned_string = ""
removed_indices = []
removed_symbols = []
for i, char in enumerate(word):
if char not in string.punctuation and char not in " ":
cleaned_string += char
else:
removed_indices.append(i)
removed_symbols.append(char)
return cleaned_string, removed_indices, removed_symbols
def insert_removed_symbols(separated_array, removed_indices, symbols):
"""Insert symbols into the syllables"""
result = []
symbol_index = 0
i = 0
# Add removed symbols to the syllables
for syllable in separated_array:
tmp = ""
for char in syllable:
if i in removed_indices:
tmp += symbols[symbol_index]
symbol_index += 1
i += 1
tmp += char
i += 1
result.append(tmp)
# Add remaining symbols to the last syllable
if symbol_index < len(symbols):
tmp = result[-1]
for i in range(symbol_index, len(symbols)):
tmp += symbols[i]
result[-1] = tmp
return result
def create_hyphenator(lang_region: str) -> Hyphenator:
"""Create hyphenator"""
hyphenator = Hyphenator(lang_region)
return hyphenator
def hyphenation(word: str, hyphenator: Hyphenator) -> list[str] | None:
"""Hyphenate word"""
if contains_punctuation(word):
return None
hyphenator = Hyphenator(lang_region)
syllabus = hyphenator.syllables(word)
cleaned_string, removed_indices, removed_symbols = clean_word(word)
syllabus = hyphenator.syllables(cleaned_string)
length = len(syllabus)
if length > 1:
hyphen = []
for i in range(length):
hyphen.append(syllabus[i])
hyphen = insert_removed_symbols(hyphen, removed_indices, removed_symbols)
else:
hyphen = None