File size: 7,469 Bytes
45ee559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "Collapsed": "false"
   },
   "source": [
    "# Jupyter Notbook for phoneme coverage analysis\n",
    "\n",
    "This jupyter notebook checks dataset configured in config.json for phoneme coverage.\n",
    "As mentioned here https://github.com/mozilla/TTS/wiki/Dataset#what-makes-a-good-dataset a good phoneme coverage is recommended.\n",
    "\n",
    "Most parameters will be taken from config.json file in mozilla tts repo so please ensure it's configured correctly for your dataset.\n",
    "This notebook used lots of existring code from the TTS repo to ensure future compatibility.\n",
    "\n",
    "Many thanks to Neil Stoker supporting me on this topic :-).\n",
    "\n",
    "I provide this notebook without any warrenty but it's hopefully useful for your dataset analysis.\n",
    "\n",
    "Happy TTS'ing :-)\n",
    "\n",
    "Thorsten Müller\n",
    "\n",
    "* https://github.com/thorstenMueller/deep-learning-german-tts\n",
    "* https://discourse.mozilla.org/t/contributing-my-german-voice-for-tts/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false"
   },
   "outputs": [],
   "source": [
    "# set some vars\n",
    "# TTS_PATH = \"/home/thorsten/___dev/tts/mozilla/TTS\"\n",
    "CONFIG_FILE = \"/path/to/config/config.json\"\n",
    "CHARS_TO_REMOVE = \".,:!?'\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false"
   },
   "outputs": [],
   "source": [
    "# import stuff\n",
    "from TTS.config import load_config\n",
    "from TTS.tts.datasets import load_tts_samples\n",
    "from TTS.tts.utils.text.tokenizer import TTSTokenizer\n",
    "from tqdm import tqdm\n",
    "from matplotlib import pylab as plt\n",
    "from multiprocessing import Pool, cpu_count\n",
    "\n",
    "# extra imports that might not be included in requirements.txt\n",
    "import collections\n",
    "import operator\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Load config.json properties\n",
    "CONFIG = load_config(CONFIG_FILE)\n",
    "\n",
    "# Load some properties from config.json\n",
    "CONFIG_METADATA = load_tts_samples(CONFIG.datasets)[0]\n",
    "CONFIG_METADATA = CONFIG_METADATA\n",
    "CONFIG_DATASET = CONFIG.datasets[0]\n",
    "CONFIG_PHONEME_LANGUAGE = CONFIG.phoneme_language\n",
    "CONFIG_TEXT_CLEANER = CONFIG.text_cleaner\n",
    "CONFIG_ENABLE_EOS_BOS_CHARS = CONFIG.enable_eos_bos_chars\n",
    "\n",
    "# Will be printed on generated output graph\n",
    "CONFIG_RUN_NAME = CONFIG.run_name\n",
    "CONFIG_RUN_DESC = CONFIG.run_description\n",
    "\n",
    "# Needed to convert text to phonemes and phonemes to ids\n",
    "tokenizer, config = TTSTokenizer.init_from_config(CONFIG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# print some debug information on loaded config values\n",
    "print(\" > Run name: \" + CONFIG_RUN_NAME + \" (\" + CONFIG_RUN_DESC + \")\")\n",
    "print(\" > Dataset files: \" + str(len(CONFIG_METADATA)))\n",
    "print(\" > Phoneme language: \" + CONFIG_PHONEME_LANGUAGE)\n",
    "print(\" > Used text cleaner: \" + CONFIG_TEXT_CLEANER)\n",
    "print(\" > Enable eos bos chars: \" + str(CONFIG_ENABLE_EOS_BOS_CHARS))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_phoneme_from_sequence(text):\n",
    "    temp_list = []\n",
    "    if len(text[\"text\"]) > 0:\n",
    "        #temp_text = text[0].rstrip('\\n')\n",
    "        temp_text = text[\"text\"].rstrip('\\n')\n",
    "        for rm_bad_chars in CHARS_TO_REMOVE:\n",
    "            temp_text = temp_text.replace(rm_bad_chars,\"\")\n",
    "        seq = tokenizer.text_to_ids(temp_text)\n",
    "        text = tokenizer.ids_to_text(seq)\n",
    "        text = text.replace(\" \",\"\")\n",
    "        temp_list.append(text)\n",
    "    return temp_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false",
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Get phonemes from metadata\n",
    "phonemes = []\n",
    "\n",
    "with Pool(cpu_count()-1) as p:\n",
    "    \n",
    "    phonemes = list(tqdm(p.imap(get_phoneme_from_sequence, CONFIG_METADATA), total=len(CONFIG_METADATA)))\n",
    "    phonemes = [i for sub in phonemes for i in sub]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false",
    "tags": []
   },
   "outputs": [],
   "source": [
    "s = \"\"\n",
    "phonemeString = s.join(phonemes)\n",
    "\n",
    "d = {}\n",
    "collections._count_elements(d, phonemeString)\n",
    "sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1),reverse=True))\n",
    "\n",
    "# remove useless keys\n",
    "sorted_d.pop(' ', None)\n",
    "sorted_d.pop('ˈ', None)\n",
    "\n",
    "phonemesSum = len(phonemeString.replace(\" \",\"\"))\n",
    "\n",
    "print(\"Dataset contains \" + str(len(sorted_d)) + \" different ipa phonemes.\")\n",
    "print(\"Dataset consists of \" + str(phonemesSum) + \" phonemes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false",
    "tags": []
   },
   "outputs": [],
   "source": [
    "print(\"5 rarest phonemes\")\n",
    "\n",
    "rareList = dict(sorted(sorted_d.items(), key=operator.itemgetter(1), reverse=False)[:5])\n",
    "for key, value in rareList.items():\n",
    "    print(key + \" --> \" + str(value) + \" occurrences\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false"
   },
   "outputs": [],
   "source": [
    "# create plot from analysis result\n",
    "\n",
    "x = []\n",
    "y = []\n",
    "\n",
    "for key, value in sorted_d.items():\n",
    "    x.append(key)\n",
    "    y.append(value)\n",
    "\n",
    "plt.figure(figsize=(50,50))\n",
    "plt.title(\"Phoneme coverage for \" + CONFIG_RUN_NAME + \" (\" + CONFIG_RUN_DESC + \")\", fontsize=50)\n",
    "plt.xticks(fontsize=50)\n",
    "plt.yticks(fontsize=50)\n",
    "plt.barh(x,y, align='center', alpha=1.0)\n",
    "plt.gca().invert_yaxis()\n",
    "plt.ylabel('phoneme', fontsize=50)\n",
    "plt.xlabel('occurrences', fontsize=50)\n",
    "\n",
    "for i, v in enumerate(y):\n",
    "    plt.text(v + 2, i - .2, str(v), fontsize=20)\n",
    "    plt.text(v + 2, i + .2, \"(\" + str(round(100/phonemesSum * v,2)) + \"%)\", fontsize=20)\n",
    "    \n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "Collapsed": "false"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}