Exemplo n.º 1
0
 def __init__(self,
              vocab: List[str],
              token_type: str = 'phone',
              pronounce_dict: PronunciationDictionary = None,
              use_blank: bool = False):
     self.labeler = WordFrameLabeler(vocab)
     self.coloring = None
     use_phone = token_type == 'phone'
     if pronounce_dict is None and use_phone:
         pronounce_dict = PronunciationDictionary.from_file(
             SETTINGS.training.phone_dictionary)
     if use_phone:
         self.coloring = LabelColoring()
         new_vocab = []
         if use_blank:
             new_vocab.append('[BLANK]')
             self.coloring.label_counter = 1
         for word in vocab:
             phone_phrases = pronounce_dict.encode(word)
             logging.info(
                 f'Using phonemes {str(phone_phrases)} for word {word}.')
             new_vocab.extend(x.text for x in phone_phrases)
             self.coloring.extend_sequence(len(phone_phrases))
         self.coloring.extend_sequence(1)  # negative label
         if use_blank:
             self.coloring.append_label(0)  # blank
         vocab = new_vocab
         phone_phrases = [PhonePhrase.from_string(x) for x in new_vocab]
         self.labeler = PhoneticFrameLabeler(phone_phrases)
     self.num_labels = len(vocab) + 1 + use_blank
     self.vocab = Vocab({word: idx for idx, word in enumerate(vocab)})
     self.negative_label = len(vocab)
     self.searcher = PhoneticTranscriptSearcher(
         phone_phrases,
         self.coloring) if use_phone else WordTranscriptSearcher(vocab)
Exemplo n.º 2
0
    def __init__(self,
                 vocab: List[str],
                 token_type: str = 'phone',
                 pronounce_dict: PronunciationDictionary = None,
                 use_blank: bool = False):

        self.coloring = None
        self.adjusted_vocab = []
        self.num_labels = 0

        # break down each vocab into phonemes
        if token_type == 'phone':
            if pronounce_dict is None:
                pronounce_dict = PronunciationDictionary.from_file(
                    SETTINGS.training.phone_dictionary)

            self.coloring = LabelColoring()
            for word in vocab:
                phone_phrases = pronounce_dict.encode(word)
                logging.info(
                    f'Using phonemes {str(phone_phrases)} for word {word}')
                self.add_vocab(x.text for x in phone_phrases)

        elif token_type == 'word':
            self.add_vocab(vocab)

        # initialize labeler; make sure this is located before adding other labels
        if token_type == 'phone':
            phone_phrases = [
                PhonePhrase.from_string(x) for x in self.adjusted_vocab
            ]
            self.labeler = PhoneticFrameLabeler(phone_phrases)
        elif token_type == 'word':
            print('labeler vocab: ', self.adjusted_vocab)
            self.labeler = WordFrameLabeler(self.adjusted_vocab)

        # initialize vocab set for the system and add negative label
        self.negative_label = len(self.adjusted_vocab)
        self.vocab = Vocab(
            {word: idx
             for idx, word in enumerate(self.adjusted_vocab)},
            oov_token_id=self.negative_label)
        self.add_vocab(['[OOV]'])

        # initialize TranscriptSearcher with the processed targets
        if token_type == 'phone':
            self.searcher = PhoneticTranscriptSearcher(phone_phrases,
                                                       self.coloring)
        elif token_type == 'word':
            self.searcher = WordTranscriptSearcher(self.adjusted_vocab)

        # add extra label for blank if necessary
        self.blank_label = -1
        if use_blank:
            self.blank_label = len(self.adjusted_vocab)
            self.add_vocab(['[BLANK]'])

        for idx, word in enumerate(self.adjusted_vocab):
            logging.info(f'target {word:10} is assigned to label {idx}')
Exemplo n.º 3
0
    def test_idx_operations(self):
        """test idx conversions
        """
        phone_phrase_str = " abc sil sp spn def ghi sil "
        pp = PhonePhrase.from_string(phone_phrase_str)

        self.assertEqual(pp.all_idx_to_transcript_idx(0), 3)
        self.assertEqual(pp.all_idx_to_transcript_idx(1), 7)
        self.assertEqual(pp.all_idx_to_transcript_idx(2), 10)
        self.assertEqual(pp.all_idx_to_transcript_idx(3), 14)
        self.assertEqual(pp.all_idx_to_transcript_idx(4), 18)
        self.assertEqual(pp.all_idx_to_transcript_idx(5), 22)
        self.assertEqual(pp.all_idx_to_transcript_idx(6), 26)
        self.assertRaises(ValueError, pp.all_idx_to_transcript_idx, 7)

        abc_pp = PhonePhrase.from_string("abc")
        def_pp = PhonePhrase.from_string("def")
        def_ghi_pp = PhonePhrase.from_string("def ghi")
        jki_pp = PhonePhrase.from_string("jki")
        sil_pp = PhonePhrase.from_string("sil")

        self.assertEqual(pp.audible_index(abc_pp), 0)
        self.assertEqual(pp.audible_index(def_pp), 1)
        self.assertEqual(pp.audible_index(def_ghi_pp), 1)
        self.assertRaises(ValueError, pp.audible_index, jki_pp)
        self.assertRaises(ValueError, pp.audible_index, sil_pp)

        self.assertEqual(pp.audible_idx_to_all_idx(0), 0)
        self.assertEqual(pp.audible_idx_to_all_idx(1), 4)
        self.assertEqual(pp.audible_idx_to_all_idx(2), 5)
        self.assertRaises(ValueError, pp.audible_idx_to_all_idx, 3)
Exemplo n.º 4
0
    def __init__(self, vocab: List[str], token_type: str = "phone", use_blank: bool = False):

        self.coloring = None
        self.adjusted_vocab = []
        self.num_labels = 0
        self.token_type = token_type
        self.pronounce_dict = None

        # break down each vocab into phonemes
        if token_type == "phone":
            self.pronounce_dict = PronunciationDictionary.from_file(Path(SETTINGS.training.phone_dictionary))

            self.coloring = LabelColoring()
            for word in vocab:
                # TODO:: we currently use single representation for simplicity
                phone_phrase = self.pronounce_dict.encode(word)[0]
                logging.info(f"Word {word: <10} has phonemes of {str(phone_phrase)}")
                self.add_vocab(list(str(phone) for phone in phone_phrase.phones))

        elif token_type == "word":
            self.add_vocab(vocab)

        # initialize vocab set for the system
        self.negative_label = len(self.adjusted_vocab)
        self.vocab = Vocab(
            {word: idx for idx, word in enumerate(self.adjusted_vocab)}, oov_token_id=self.negative_label
        )

        # initialize labeler; make sure this is located before adding other labels
        if token_type == "phone":
            phone_phrases = [PhonePhrase.from_string(x) for x in self.adjusted_vocab]
            self.labeler = PhoneticFrameLabeler(self.pronounce_dict, phone_phrases)
        elif token_type == "word":
            self.labeler = WordFrameLabeler(self.vocab)

        # add negative label
        self.add_vocab(["[OOV]"])

        # initialize TranscriptSearcher with the processed targets
        if token_type == "phone":
            self.searcher = PhoneticTranscriptSearcher(phone_phrases, self.coloring)
        elif token_type == "word":
            self.searcher = WordTranscriptSearcher(self.vocab)

        # add extra label for blank if necessary
        self.blank_label = -1
        if use_blank:
            self.blank_label = len(self.adjusted_vocab)
            self.add_vocab(["[BLANK]"])

        for idx, word in enumerate(self.adjusted_vocab):
            logging.info(f"target {word:10} is assigned to label {idx}")
Exemplo n.º 5
0
 def contains_any(self, item: str) -> bool:
     transcript = PhonePhrase.from_string(item).audible_transcript
     return any(word.audible_transcript in transcript
                for word in self.phrases)
Exemplo n.º 6
0
 def search(self, item: str) -> bool:
     transcript = PhonePhrase.from_string(item).audible_transcript
     return self.pattern.match(transcript) is not None
Exemplo n.º 7
0
 def test_basic_operations(self):
     """test PhonePhase instatniation and conversions to strgin
     """
     phone_phrase_str = " abc sil sp spn "
     pp = PhonePhrase.from_string(phone_phrase_str)
     self.assertEqual(pp.text, phone_phrase_str.strip())