MontrealCorpusTools / Montreal-Forced-Aligner

Command line utility for forced alignment using Kaldi
https://montrealcorpustools.github.io/Montreal-Forced-Aligner/
MIT License
1.29k stars 242 forks source link

[Question] about: how to get phoneme from input word #653

Closed phamkhactu closed 1 year ago

phamkhactu commented 1 year ago

First of all, I would like to said that: Thanks for your excellent repo.

I want to get phoneme from my models that I've trained with input text For example my pseudo:

word ='Hello"
model = mfa.load(pretrained_model)
phoneme = model.phoneme(word) 

Expected phoneme output: 
print(phoneme) -->: ['h', 'ə', 'l', 'oʊ']

Could you guide me for doing that. Many thanks

mmcauliffe commented 1 year ago

You can pipe "hello" to mfa g2p using '-' instead of input paths: https://montreal-forced-aligner.readthedocs.io/en/latest/user_guide/workflows/dictionary_generating.html#piping-stdin-stdout

So this would be a command like mfa g2p - english_us_mfa - and if you pipe in hello it will pipe out hello h ə l oʊ. That's probably the easiest route (or using subprocess in python code), but you could also do something like

from montreal_forced_aligner.g2p.generator import (
    PyniniConsoleGenerator
)

g2p_model_path = "/path/to/g2p_model.zip"

g2p = PyniniConsoleGenerator(
            g2p_model_path=g2p_model_path
        )

word ="Hello"
pronunciations = g2p.rewriter(word)
print(pronunciations[0].split())   # Should print ['h', 'ə', 'l', 'oʊ']
v-nhandt21 commented 10 months ago

You can pipe "hello" to mfa g2p using '-' instead of input paths: https://montreal-forced-aligner.readthedocs.io/en/latest/user_guide/workflows/dictionary_generating.html#piping-stdin-stdout

So this would be a command like mfa g2p - english_us_mfa - and if you pipe in hello it will pipe out hello h ə l oʊ. That's probably the easiest route (or using subprocess in python code), but you could also do something like

from montreal_forced_aligner.g2p.generator import (
    PyniniConsoleGenerator
)

g2p_model_path = "/path/to/g2p_model.zip"

g2p = PyniniConsoleGenerator(
            g2p_model_path=g2p_model_path
        )

word ="Hello"
pronunciations = g2p.rewriter(word)
print(pronunciations[0].split())   # Should print ['h', 'ə', 'l', 'oʊ']

I update the source code for more correctly when calling with python

That we must call g2p setup before infer the model: https://github.com/MontrealCorpusTools/Montreal-Forced-Aligner/blob/e08da3b843bc34b10224bef49c98e47ade6339c0/montreal_forced_aligner/g2p/generator.py#L510

from montreal_forced_aligner.g2p.generator import (
    PyniniConsoleGenerator
)

g2p_model_path = "/path/to/g2p_model.zip"

g2p = PyniniConsoleGenerator(
            g2p_model_path=g2p_model_path
        )

g2p.setup()

word ="Hello"
pronunciations = g2p.rewriter(word)
print(pronunciations[0].split())   # Should print ['h', 'ə', 'l', 'oʊ']