|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2020 Mobvoi AI Lab, Beijing, China (author: Fangjun Kuang) |
| 4 | +# Apache 2.0 |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | + |
| 9 | +import kaldi |
| 10 | + |
| 11 | + |
| 12 | +def get_args(): |
| 13 | + parser = argparse.ArgumentParser(description='convert text to labels') |
| 14 | + |
| 15 | + parser.add_argument('--lexicon-filename', dest='lexicon_filename', type=str) |
| 16 | + parser.add_argument('--tokens-filename', dest='tokens_filename', type=str) |
| 17 | + parser.add_argument('--dir', help='input/output dir', type=str) |
| 18 | + |
| 19 | + args = parser.parse_args() |
| 20 | + |
| 21 | + assert os.path.isfile(args.lexicon_filename) |
| 22 | + assert os.path.isfile(args.tokens_filename) |
| 23 | + assert os.path.isfile(os.path.join(args.dir, 'text')) |
| 24 | + |
| 25 | + return args |
| 26 | + |
| 27 | + |
| 28 | +def read_lexicon(filename): |
| 29 | + ''' |
| 30 | + Returns: |
| 31 | + a dict whose keys are words and values are phones. |
| 32 | + ''' |
| 33 | + lexicon = dict() |
| 34 | + with open(filename, 'r', encoding='utf-8') as f: |
| 35 | + for line in f: |
| 36 | + word_phones = line.split() |
| 37 | + assert len(word_phones) >= 2 |
| 38 | + |
| 39 | + word = word_phones[0] |
| 40 | + phones = word_phones[1:] |
| 41 | + |
| 42 | + if word not in lexicon: |
| 43 | + # if there are multiple pronunciations for a word, |
| 44 | + # we choose only the first one and drop other alternatives |
| 45 | + lexicon[word] = phones |
| 46 | + |
| 47 | + return lexicon |
| 48 | + |
| 49 | + |
| 50 | +def read_tokens(filename): |
| 51 | + ''' |
| 52 | + Returns: |
| 53 | + a dict whose keys are phones and values are phone indices |
| 54 | + ''' |
| 55 | + tokens = dict() |
| 56 | + with open(filename, 'r', encoding='utf-8') as f: |
| 57 | + for line in f: |
| 58 | + phone_index = line.split() |
| 59 | + assert len(phone_index) == 2 |
| 60 | + |
| 61 | + phone = phone_index[0] |
| 62 | + index = int(phone_index[1]) |
| 63 | + |
| 64 | + if phone == '<eps>': |
| 65 | + continue |
| 66 | + |
| 67 | + # decreased by one since we removed <eps> above |
| 68 | + index -= 1 |
| 69 | + |
| 70 | + assert phone not in tokens |
| 71 | + |
| 72 | + tokens[phone] = index |
| 73 | + |
| 74 | + assert '<blk>' in tokens |
| 75 | + |
| 76 | + # WARNING(fangjun): we assume that the blank symbol has index 0 |
| 77 | + # in the neural network output. |
| 78 | + # Do NOT confuse it with `<eps>` in fst. |
| 79 | + assert tokens['<blk>'] == 0 |
| 80 | + |
| 81 | + return tokens |
| 82 | + |
| 83 | + |
| 84 | +def read_text(filename): |
| 85 | + ''' |
| 86 | + Returns: |
| 87 | + a dict whose keys are utterance IDs and values are texts |
| 88 | + ''' |
| 89 | + transcript = dict() |
| 90 | + |
| 91 | + with open(filename, 'r', encoding='utf-8') as f: |
| 92 | + for line in f: |
| 93 | + utt_text = line.split() |
| 94 | + assert len(utt_text) >= 2 |
| 95 | + |
| 96 | + utt = utt_text[0] |
| 97 | + text = utt_text[1:] |
| 98 | + |
| 99 | + assert utt not in transcript |
| 100 | + transcript[utt] = text |
| 101 | + |
| 102 | + return transcript |
| 103 | + |
| 104 | + |
| 105 | +def phones_to_indices(phone_list, tokens): |
| 106 | + index_list = [] |
| 107 | + |
| 108 | + for phone in phone_list: |
| 109 | + assert phone in tokens |
| 110 | + |
| 111 | + index = tokens[phone] |
| 112 | + index_list.append(index) |
| 113 | + |
| 114 | + return index_list |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + args = get_args() |
| 119 | + |
| 120 | + lexicon = read_lexicon(args.lexicon_filename) |
| 121 | + |
| 122 | + tokens = read_tokens(args.tokens_filename) |
| 123 | + |
| 124 | + transcript = read_text(os.path.join(args.dir, 'text')) |
| 125 | + |
| 126 | + transcript_labels = dict() |
| 127 | + |
| 128 | + for utt, text in transcript.items(): |
| 129 | + labels = [] |
| 130 | + for t in text: |
| 131 | + # TODO(fangjun): add support for OOV. |
| 132 | + phones = lexicon[t] |
| 133 | + |
| 134 | + indices = phones_to_indices(phones, tokens) |
| 135 | + |
| 136 | + labels.extend(indices) |
| 137 | + |
| 138 | + assert utt not in transcript_labels |
| 139 | + |
| 140 | + transcript_labels[utt] = labels |
| 141 | + |
| 142 | + wspecifier = 'ark,scp:{dir}/labels.ark,{dir}/labels.scp'.format( |
| 143 | + dir=args.dir) |
| 144 | + |
| 145 | + writer = kaldi.IntVectorWriter(wspecifier) |
| 146 | + |
| 147 | + for utt, labels in transcript_labels.items(): |
| 148 | + writer.Write(utt, labels) |
| 149 | + |
| 150 | + writer.Close() |
| 151 | + |
| 152 | + print('Generated label file {}/labels.scp successfully'.format(args.dir)) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + main() |
0 commit comments