Pocketsphinx phones
Aligning Hungarian with Pocketsphinx English
!wget https://ia600600.us.archive.org/23/items/multilingual_poetry_014_1002/hungarian_aborozo_petofi_dii.mp3
!ffmpeg -i ~/Downloads/hungarian_aborozo_petofi_dii.mp3 -acodec pcm_s16le -ac 1 -ar 16000 /tmp/samp.wav
!sox /tmp/samp.wav -r 16000 -c 1 -b 16 -e signed-integer /tmp/samp.raw
from pocketsphinx import Decoder, Config
MODEL="/opt/homebrew/Cellar/cmu-pocketsphinx/5.0.4/share/pocketsphinx/model/en-us"
RAW="/tmp/samp.raw"
config = Config(
hmm=f"{MODEL}/en-us",
allphone=f"{MODEL}/en-us-phone.lm.bin",
lm=None,
allphone_ci=True,
dict=None,
samprate=16000,
)
ps = Decoder(config)
with open(RAW, "rb") as f:
ps.start_utt()
ps.process_raw(f.read(), no_search=False, full_utt=True)
ps.end_utt()
frate = ps.config.get_int("-frate")
print(f"# frate={frate} frames/sec")
for seg in ps.seg():
label = seg.word
sf = seg.start_frame
ef = seg.end_frame
start_s = sf / frate
end_s = ef / frate
dur_s = (ef - sf) / frate
print(f"{label}\t{start_s:.3f}\t{end_s:.3f}\t{dur_s:.3f}")
A borozó by Sándor Petőfi. Read in Hungarian for librivox dot org by Diana Majlinger.
Gondüző borocska mellett Vígan illan életem; Gondüző borocska mellett, Sors, hatalmad nevetem.
És mit ámultok? ha mondom, Hogy csak a bor istene, Akit én imádok, aki E kebelnek mindene.
És a bor vidám hevében Füttyentek rád, zord világ! Szívemet hol annyi kínnak Skorpiói szaggaták.
Bor taníta húrjaimra Csalni nyájas éneket; Bor taníta elfeledni, Csalfa lyányok, titeket.
Egykor majd borocska mellől A halál ha űzni jő: Még egy korty - s nevetve dűlök Jégöledbe, temető!
End of poem. This recording is in the public domain.
DATA = {
"A borozó by Sándor Petőfi.": {
"languages": ["hu", "hu", "en", "hu", "hu"],
"entities": ["B-WORK", "E-WORK", "O", "B-PERSON", "E-PERSON"]
},
"Read in Hungarian for librivox dot org by Diana Majlinger.": {
"languages": ["en", "en", "en", "en", "en", "en", "en", "en", "hu", "hu"],
"entities": ["O", "O", "O", "O", "O", "B-URL", "I-URL", "E-URL", "B-PERSON", "E-PERSON"],
"normalized": "Read in Hungarian for librivox.org by Diana Majlinger."
}
}
TEXT = """
A borozó
SÁNDOR PETŐFI.
Diana Majlinger
Gondüző borocska mellett
Vígan illan életem;
Gondüző borocska mellett,
Sors, hatalmad nevetem.
És mit ámultok? ha mondom,
Hogy csak a bor istene,
Akit én imádok, aki
E kebelnek mindene.
És a bor vidám hevében
Füttyentek rád, zord világ!
Szívemet hol annyi kínnak
Skorpiói szaggaták.
Bor taníta húrjaimra
Csalni nyájas éneket;
Bor taníta elfeledni,
Csalfa lyányok, titeket.
Egykor majd borocska mellől
A halál ha űzni jő:
Még egy korty - s nevetve dűlök
Jégöledbe, temető!
"""
word_lists = {}
for line in TEXT.strip().split("\n"):
if line.strip() == "":
continue
words = line.strip().split(" ")
cleaned_words = [w.lower().strip("-:;,!?.") for w in words if w]
only_cleaned_words = [w for w in cleaned_words if w]
espeak_out = !/opt/homebrew/bin/espeak -v hu --ipa -q --stdout "{line}"
stripped = [x.strip() for x in espeak_out if x.strip()]
phone_line = " ".join(stripped)
phone_words = phone_line.split(" ")
if len(only_cleaned_words) != len(phone_words):
print(f"WARNING: word count mismatch for line: {line}")
print(f" cleaned_words ({len(only_cleaned_words)}): {cleaned_words}")
print(f" phone_words ({len(phone_words)}): {phone_words}")
for a, b in zip(only_cleaned_words, phone_words):
if a not in word_lists:
word_lists[a] = set()
word_lists[a].add(b)
out_words = {w: list(word_lists[w]) for w in word_lists}
EN_TEXT = """
by
Read in Hungarian for librivox dot org by
End of poem. This recording is in the public domain.
"""
en_word_lists = {
"read": ["ɹˈɛd"],
"librivox": ["lˈɪbɹivˌɑːks"]
}
_EN_WORDS = EN_TEXT.strip().split()
for w in _EN_WORDS:
cleaned = w.lower().strip("-:;,!?.")
if cleaned == "":
continue
if cleaned in en_word_lists:
continue
espeak_out = !/opt/homebrew/bin/espeak -v en-us --ipa -q --stdout "{cleaned}"
stripped = [x.strip() for x in espeak_out if x.strip()]
phone_line = " ".join(stripped)
if cleaned not in en_word_lists:
en_word_lists[cleaned] = set()
en_word_lists[cleaned].add(phone_line)
en_word_lists
out_words