async def async_test_g2p_pronounce(self):
        """Check guessed pronunciations."""
        num_guesses = 2
        fake_words = ["foo", "bar"]
        fake_phonemes = ["P1", "P2", "P3"]

        def fake_guess(words, *args, num_guesses=0, **kwargs):
            """Generate fake phonetic pronunciations."""
            for word in words:
                for _ in range(num_guesses):
                    yield word, fake_phonemes

        with patch("rhasspynlu.g2p.guess_pronunciations", new=fake_guess):
            g2p_id = str(uuid.uuid4())
            pronounce = G2pPronounce(
                id=g2p_id,
                words=fake_words,
                num_guesses=num_guesses,
                site_id=self.site_id,
                session_id=self.session_id,
            )

            # Send in request
            result = None
            async for response in self.hermes.on_message(pronounce):
                result = response

        expected_prons = [
            G2pPronunciation(phonemes=fake_phonemes, guessed=True)
            for _ in range(num_guesses)
        ]

        self.assertEqual(
            result,
            G2pPhonemes(
                id=g2p_id,
                word_phonemes={word: expected_prons
                               for word in fake_words},
                site_id=self.site_id,
                session_id=self.session_id,
            ),
        )
예제 #2
0
    async def handle_pronounce(
        self, pronounce: G2pPronounce
    ) -> typing.AsyncIterable[typing.Union[G2pPhonemes, G2pError]]:
        """Looks up or guesses word pronunciation(s)."""
        try:
            result = G2pPhonemes(
                word_phonemes={},
                id=pronounce.id,
                site_id=pronounce.site_id,
                session_id=pronounce.session_id,
            )

            # Load base dictionaries
            pronunciations: PronunciationsType = {}

            for base_dict in self.base_dictionaries:
                if base_dict.path.is_file():
                    _LOGGER.debug("Loading base dictionary from %s",
                                  base_dict.path)
                    with open(base_dict.path, "r") as base_dict_file:
                        rhasspynlu.g2p.read_pronunciations(
                            base_dict_file, word_dict=pronunciations)

            # Try to look up in dictionary first
            missing_words: typing.Set[str] = set()
            if pronunciations:
                for word in pronounce.words:
                    # Handle case transformation
                    if self.dictionary_word_transform:
                        word = self.dictionary_word_transform(word)

                    word_prons = pronunciations.get(word)
                    if word_prons:
                        # Use dictionary pronunciations
                        result.word_phonemes[word] = [
                            G2pPronunciation(phonemes=p, guessed=False)
                            for p in word_prons
                        ]
                    else:
                        # Will have to guess later
                        missing_words.add(word)
            else:
                # All words must be guessed
                missing_words.update(pronounce.words)

            if missing_words:
                if self.g2p_model:
                    _LOGGER.debug("Guessing pronunciations of %s",
                                  missing_words)
                    guesses = rhasspynlu.g2p.guess_pronunciations(
                        missing_words,
                        self.g2p_model,
                        g2p_word_transform=self.g2p_word_transform,
                        num_guesses=pronounce.num_guesses,
                    )

                    # Add guesses to result
                    for guess_word, guess_phonemes in guesses:
                        result_phonemes = result.word_phonemes.get(
                            guess_word) or []
                        result_phonemes.append(
                            G2pPronunciation(phonemes=guess_phonemes,
                                             guessed=True))
                        result.word_phonemes[guess_word] = result_phonemes
                else:
                    _LOGGER.warning(
                        "No g2p model. Cannot guess pronunciations.")

            yield result
        except Exception as e:
            _LOGGER.exception("handle_pronounce")
            yield G2pError(
                error=str(e),
                context=f"model={self.model_dir}, graph={self.graph_dir}",
                site_id=pronounce.site_id,
                session_id=pronounce.session_id,
            )