File size: 2,859 Bytes
85b7206
 
 
 
28d0c5f
 
0700cb3
 
74a35d9
85b7206
0700cb3
 
 
 
 
 
 
 
 
 
 
 
85b7206
 
 
 
 
 
 
 
 
28d0c5f
 
0700cb3
 
 
28d0c5f
 
 
 
0700cb3
 
 
 
 
 
28d0c5f
 
 
 
0700cb3
 
 
 
 
 
 
 
 
 
28d0c5f
0700cb3
28d0c5f
 
 
 
0700cb3
 
 
28d0c5f
 
0700cb3
 
 
28d0c5f
 
 
0700cb3
 
 
 
 
 
 
 
 
 
28d0c5f
 
0700cb3
28d0c5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import ModelInterfaces
import torch
import numpy as np
import epitran
import eng_to_ipa

from constants import app_logger


def get_phonem_converter(language: str):
    """
    Get the phoneme converter for the specified language.

    Args:
        language (str): The language code (e.g., 'de' for German, 'en' for English).

    Returns:
        ModelInterfaces.ITextToPhonemModel: The phoneme converter for the specified language.

    Raises:
        ValueError: If the language is not implemented.
    """
    if language == 'de':
        phonem_converter = EpitranPhonemConverter(
            epitran.Epitran('deu-Latn'))
    elif language == 'en':
        phonem_converter = EngPhonemConverter()
    else:
        raise ValueError('Language not implemented')

    return phonem_converter

class EpitranPhonemConverter(ModelInterfaces.ITextToPhonemModel):
    """
    A phoneme converter using the Epitran library for transliteration.
    """
    word_locations_in_samples = None
    audio_transcript = None

    def __init__(self, epitran_model) -> None:
        """
        Initialize the EpitranPhonemConverter with an Epitran model.

        Args:
            epitran_model: The Epitran model for transliteration.
        """
        super().__init__()
        self.epitran_model = epitran_model

    def convertToPhonem(self, sentence: str) -> str:
        """
        Convert a sentence to its phoneme representation.

        Args:
            sentence (str): The input sentence.

        Returns:
            str: The phoneme representation of the sentence.
        """
        app_logger.debug(f'starting EpitranPhonemConverter.convertToPhonem for sentence/token "{sentence}"...')
        phonem_representation = self.epitran_model.transliterate(sentence)
        app_logger.debug(f'EpitranPhonemConverter: got phonem_representation for sentence/token "{sentence}"!')
        return phonem_representation


class EngPhonemConverter(ModelInterfaces.ITextToPhonemModel):
    """
    A phoneme converter for English using the eng\_to\_ipa library.
    """

    def __init__(self,) -> None:
        """
        Initialize the EngPhonemConverter.
        """
        super().__init__()

    def convertToPhonem(self, sentence: str) -> str:
        """
        Convert a sentence to its phoneme representation.

        Args:
            sentence (str): The input sentence.

        Returns:
            str: The phoneme representation of the sentence.
        """
        app_logger.debug(f'starting EngPhonemConverter.convertToPhonem for sentence/token "{sentence}"...')
        phonem_representation = eng_to_ipa.convert(sentence)
        phonem_representation = phonem_representation.replace('*','')
        app_logger.debug(f'EngPhonemConverter: got phonem_representation for sentence/token "{sentence}"!')
        return phonem_representation