Benchmarking notebooks for various Persian G2P models, comparing their performance on the SentenceBench dataset, including Homo-GE2PE and Homo-T5.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Benchmark_Homo_T5.ipynb 51KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. {
  2. "nbformat": 4,
  3. "nbformat_minor": 0,
  4. "metadata": {
  5. "colab": {
  6. "provenance": [],
  7. "collapsed_sections": [
  8. "AdU8VMTIOWLZ",
  9. "a3zuvbqx2l68",
  10. "XjAPkfq7SF87",
  11. "R6PE5ds45TPr",
  12. "y73zFlRGIbt9",
  13. "oBgNtpFQDwku",
  14. "JGEUIrbi9kNH",
  15. "fTRgGM_8_Fwg",
  16. "jPXWBZ4R_bGs"
  17. ]
  18. },
  19. "kernelspec": {
  20. "name": "python3",
  21. "display_name": "Python 3"
  22. },
  23. "language_info": {
  24. "name": "python"
  25. },
  26. "gpuClass": "standard"
  27. },
  28. "cells": [
  29. {
  30. "cell_type": "markdown",
  31. "source": [
  32. "# Setup Environment"
  33. ],
  34. "metadata": {
  35. "id": "9sEfZoepGP8x"
  36. }
  37. },
  38. {
  39. "cell_type": "code",
  40. "source": [
  41. "! pip install hazm==0.10.0"
  42. ],
  43. "metadata": {
  44. "colab": {
  45. "base_uri": "https://localhost:8080/",
  46. "height": 1000
  47. },
  48. "id": "u6n8Hc1hQSy7",
  49. "outputId": "ae910928-3ef8-4627-9f18-78b26448e7a6"
  50. },
  51. "execution_count": null,
  52. "outputs": [
  53. {
  54. "output_type": "stream",
  55. "name": "stdout",
  56. "text": [
  57. "Collecting hazm==0.10.0\n",
  58. " Downloading hazm-0.10.0-py3-none-any.whl.metadata (11 kB)\n",
  59. "Collecting fasttext-wheel<0.10.0,>=0.9.2 (from hazm==0.10.0)\n",
  60. " Downloading fasttext_wheel-0.9.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (16 kB)\n",
  61. "Collecting flashtext<3.0,>=2.7 (from hazm==0.10.0)\n",
  62. " Downloading flashtext-2.7.tar.gz (14 kB)\n",
  63. " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
  64. "Collecting gensim<5.0.0,>=4.3.1 (from hazm==0.10.0)\n",
  65. " Downloading gensim-4.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (8.1 kB)\n",
  66. "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in /usr/local/lib/python3.11/dist-packages (from hazm==0.10.0) (3.9.1)\n",
  67. "Collecting numpy==1.24.3 (from hazm==0.10.0)\n",
  68. " Downloading numpy-1.24.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.6 kB)\n",
  69. "Collecting python-crfsuite<0.10.0,>=0.9.9 (from hazm==0.10.0)\n",
  70. " Downloading python_crfsuite-0.9.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.3 kB)\n",
  71. "Requirement already satisfied: scikit-learn<2.0.0,>=1.2.2 in /usr/local/lib/python3.11/dist-packages (from hazm==0.10.0) (1.6.1)\n",
  72. "Collecting pybind11>=2.2 (from fasttext-wheel<0.10.0,>=0.9.2->hazm==0.10.0)\n",
  73. " Downloading pybind11-2.13.6-py3-none-any.whl.metadata (9.5 kB)\n",
  74. "Requirement already satisfied: setuptools>=0.7.0 in /usr/local/lib/python3.11/dist-packages (from fasttext-wheel<0.10.0,>=0.9.2->hazm==0.10.0) (75.2.0)\n",
  75. "Collecting scipy<1.14.0,>=1.7.0 (from gensim<5.0.0,>=4.3.1->hazm==0.10.0)\n",
  76. " Downloading scipy-1.13.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB)\n",
  77. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.6/60.6 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  78. "\u001b[?25hRequirement already satisfied: smart-open>=1.8.1 in /usr/local/lib/python3.11/dist-packages (from gensim<5.0.0,>=4.3.1->hazm==0.10.0) (7.1.0)\n",
  79. "Requirement already satisfied: click in /usr/local/lib/python3.11/dist-packages (from nltk<4.0.0,>=3.8.1->hazm==0.10.0) (8.1.8)\n",
  80. "Requirement already satisfied: joblib in /usr/local/lib/python3.11/dist-packages (from nltk<4.0.0,>=3.8.1->hazm==0.10.0) (1.5.0)\n",
  81. "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.11/dist-packages (from nltk<4.0.0,>=3.8.1->hazm==0.10.0) (2024.11.6)\n",
  82. "Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from nltk<4.0.0,>=3.8.1->hazm==0.10.0) (4.67.1)\n",
  83. "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from scikit-learn<2.0.0,>=1.2.2->hazm==0.10.0) (3.6.0)\n",
  84. "Requirement already satisfied: wrapt in /usr/local/lib/python3.11/dist-packages (from smart-open>=1.8.1->gensim<5.0.0,>=4.3.1->hazm==0.10.0) (1.17.2)\n",
  85. "Downloading hazm-0.10.0-py3-none-any.whl (892 kB)\n",
  86. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m892.6/892.6 kB\u001b[0m \u001b[31m20.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  87. "\u001b[?25hDownloading numpy-1.24.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)\n",
  88. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m17.3/17.3 MB\u001b[0m \u001b[31m54.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  89. "\u001b[?25hDownloading fasttext_wheel-0.9.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB)\n",
  90. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.4/4.4 MB\u001b[0m \u001b[31m48.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  91. "\u001b[?25hDownloading gensim-4.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (26.7 MB)\n",
  92. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m26.7/26.7 MB\u001b[0m \u001b[31m17.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  93. "\u001b[?25hDownloading python_crfsuite-0.9.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
  94. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m32.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  95. "\u001b[?25hDownloading pybind11-2.13.6-py3-none-any.whl (243 kB)\n",
  96. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m243.3/243.3 kB\u001b[0m \u001b[31m13.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  97. "\u001b[?25hDownloading scipy-1.13.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB)\n",
  98. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m38.6/38.6 MB\u001b[0m \u001b[31m13.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  99. "\u001b[?25hBuilding wheels for collected packages: flashtext\n",
  100. " Building wheel for flashtext (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
  101. " Created wheel for flashtext: filename=flashtext-2.7-py2.py3-none-any.whl size=9300 sha256=622a0106ef602f8322cd8f75bcb6baca71a2b02160579a81a307afae3af9ca79\n",
  102. " Stored in directory: /root/.cache/pip/wheels/49/20/47/f03dfa8a7239c54cbc44ff7389eefbf888d2c1873edaaec888\n",
  103. "Successfully built flashtext\n",
  104. "Installing collected packages: flashtext, python-crfsuite, pybind11, numpy, scipy, fasttext-wheel, gensim, hazm\n",
  105. " Attempting uninstall: numpy\n",
  106. " Found existing installation: numpy 2.0.2\n",
  107. " Uninstalling numpy-2.0.2:\n",
  108. " Successfully uninstalled numpy-2.0.2\n",
  109. " Attempting uninstall: scipy\n",
  110. " Found existing installation: scipy 1.15.2\n",
  111. " Uninstalling scipy-1.15.2:\n",
  112. " Successfully uninstalled scipy-1.15.2\n",
  113. "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
  114. "treescope 0.1.9 requires numpy>=1.25.2, but you have numpy 1.24.3 which is incompatible.\n",
  115. "pymc 5.22.0 requires numpy>=1.25.0, but you have numpy 1.24.3 which is incompatible.\n",
  116. "thinc 8.3.6 requires numpy<3.0.0,>=2.0.0, but you have numpy 1.24.3 which is incompatible.\n",
  117. "jaxlib 0.5.1 requires numpy>=1.25, but you have numpy 1.24.3 which is incompatible.\n",
  118. "albucore 0.0.24 requires numpy>=1.24.4, but you have numpy 1.24.3 which is incompatible.\n",
  119. "albumentations 2.0.6 requires numpy>=1.24.4, but you have numpy 1.24.3 which is incompatible.\n",
  120. "tensorflow 2.18.0 requires numpy<2.1.0,>=1.26.0, but you have numpy 1.24.3 which is incompatible.\n",
  121. "tsfresh 0.21.0 requires scipy>=1.14.0; python_version >= \"3.10\", but you have scipy 1.13.1 which is incompatible.\n",
  122. "blosc2 3.3.2 requires numpy>=1.26, but you have numpy 1.24.3 which is incompatible.\n",
  123. "jax 0.5.2 requires numpy>=1.25, but you have numpy 1.24.3 which is incompatible.\u001b[0m\u001b[31m\n",
  124. "\u001b[0mSuccessfully installed fasttext-wheel-0.9.2 flashtext-2.7 gensim-4.3.3 hazm-0.10.0 numpy-1.24.3 pybind11-2.13.6 python-crfsuite-0.9.11 scipy-1.13.1\n"
  125. ]
  126. },
  127. {
  128. "output_type": "display_data",
  129. "data": {
  130. "application/vnd.colab-display-data+json": {
  131. "pip_warning": {
  132. "packages": [
  133. "numpy"
  134. ]
  135. },
  136. "id": "1234d2b3469f47be95bf845976ce5afd"
  137. }
  138. },
  139. "metadata": {}
  140. }
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "source": [
  146. "!pip install numpy==1.26.0"
  147. ],
  148. "metadata": {
  149. "colab": {
  150. "base_uri": "https://localhost:8080/"
  151. },
  152. "id": "iA2Jjex-KMqx",
  153. "outputId": "d4af6237-5535-49a7-9fbc-81f79d071e13"
  154. },
  155. "execution_count": null,
  156. "outputs": [
  157. {
  158. "output_type": "stream",
  159. "name": "stdout",
  160. "text": [
  161. "Collecting numpy==1.26.0\n",
  162. " Downloading numpy-1.26.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (58 kB)\n",
  163. "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/58.5 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.5/58.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  164. "\u001b[?25hDownloading numpy-1.26.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)\n",
  165. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m18.2/18.2 MB\u001b[0m \u001b[31m91.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  166. "\u001b[?25hInstalling collected packages: numpy\n",
  167. " Attempting uninstall: numpy\n",
  168. " Found existing installation: numpy 1.24.3\n",
  169. " Uninstalling numpy-1.24.3:\n",
  170. " Successfully uninstalled numpy-1.24.3\n",
  171. "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
  172. "hazm 0.10.0 requires numpy==1.24.3, but you have numpy 1.26.0 which is incompatible.\n",
  173. "thinc 8.3.6 requires numpy<3.0.0,>=2.0.0, but you have numpy 1.26.0 which is incompatible.\n",
  174. "tsfresh 0.21.0 requires scipy>=1.14.0; python_version >= \"3.10\", but you have scipy 1.13.1 which is incompatible.\u001b[0m\u001b[31m\n",
  175. "\u001b[0mSuccessfully installed numpy-1.26.0\n"
  176. ]
  177. }
  178. ]
  179. },
  180. {
  181. "cell_type": "code",
  182. "source": [
  183. "from IPython.display import display, HTML\n",
  184. "\n",
  185. "display(HTML(\"\"\"\n",
  186. "<div style='color: white; background-color: #f44336; padding: 10px; border-radius: 5px;'>\n",
  187. " <strong>Please restart the notebook!</strong> Click on <b>Runtime</b> → <b>Restart session</b> and then re-run all cells.\n",
  188. "</div>\n",
  189. "\"\"\"))"
  190. ],
  191. "metadata": {
  192. "colab": {
  193. "base_uri": "https://localhost:8080/",
  194. "height": 54
  195. },
  196. "id": "QP854R4YHf4I",
  197. "outputId": "83993944-7c9f-431d-b7b7-626b113bb483"
  198. },
  199. "execution_count": null,
  200. "outputs": [
  201. {
  202. "output_type": "display_data",
  203. "data": {
  204. "text/plain": [
  205. "<IPython.core.display.HTML object>"
  206. ],
  207. "text/html": [
  208. "\n",
  209. "<div style='color: white; background-color: #f44336; padding: 10px; border-radius: 5px;'>\n",
  210. " <strong>Please restart the notebook!</strong> Click on <b>Runtime</b> → <b>Restart session</b> and then re-run all cells.\n",
  211. "</div>\n"
  212. ]
  213. },
  214. "metadata": {}
  215. }
  216. ]
  217. },
  218. {
  219. "cell_type": "code",
  220. "source": [
  221. "!pip install -q --upgrade --no-cache-dir gdown"
  222. ],
  223. "metadata": {
  224. "id": "EVO9pn8Ou3o1"
  225. },
  226. "execution_count": null,
  227. "outputs": []
  228. },
  229. {
  230. "cell_type": "code",
  231. "source": [
  232. "!pip install -q unidecode\n",
  233. "!pip install -q transformers"
  234. ],
  235. "metadata": {
  236. "colab": {
  237. "base_uri": "https://localhost:8080/"
  238. },
  239. "id": "brKU69ZQvEiz",
  240. "outputId": "cbc3c0b7-ae8f-4baf-a133-b4c29897716a"
  241. },
  242. "execution_count": null,
  243. "outputs": [
  244. {
  245. "output_type": "stream",
  246. "name": "stdout",
  247. "text": [
  248. "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/235.8 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m235.8/235.8 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  249. "\u001b[?25h"
  250. ]
  251. }
  252. ]
  253. },
  254. {
  255. "cell_type": "code",
  256. "source": [
  257. "!pip install jiwer"
  258. ],
  259. "metadata": {
  260. "colab": {
  261. "base_uri": "https://localhost:8080/"
  262. },
  263. "id": "grp-l-cbGNWm",
  264. "outputId": "2cb8b107-24fb-4537-cc61-f3093a759fc0"
  265. },
  266. "execution_count": null,
  267. "outputs": [
  268. {
  269. "output_type": "stream",
  270. "name": "stdout",
  271. "text": [
  272. "Collecting jiwer\n",
  273. " Downloading jiwer-3.1.0-py3-none-any.whl.metadata (2.6 kB)\n",
  274. "Requirement already satisfied: click>=8.1.8 in /usr/local/lib/python3.11/dist-packages (from jiwer) (8.1.8)\n",
  275. "Collecting rapidfuzz>=3.9.7 (from jiwer)\n",
  276. " Downloading rapidfuzz-3.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n",
  277. "Downloading jiwer-3.1.0-py3-none-any.whl (22 kB)\n",
  278. "Downloading rapidfuzz-3.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)\n",
  279. "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m46.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
  280. "\u001b[?25hInstalling collected packages: rapidfuzz, jiwer\n",
  281. "Successfully installed jiwer-3.1.0 rapidfuzz-3.13.0\n"
  282. ]
  283. }
  284. ]
  285. },
  286. {
  287. "cell_type": "code",
  288. "source": [
  289. "import pandas as pd\n",
  290. "import re\n",
  291. "from jiwer import cer"
  292. ],
  293. "metadata": {
  294. "id": "dQ0osefGGSpJ"
  295. },
  296. "execution_count": null,
  297. "outputs": []
  298. },
  299. {
  300. "cell_type": "markdown",
  301. "source": [
  302. "# Setup Model"
  303. ],
  304. "metadata": {
  305. "id": "Nwt1YBYVqcva"
  306. }
  307. },
  308. {
  309. "cell_type": "code",
  310. "source": [
  311. "!gdown -q 1UcpwwxdODeyRho1yeHzvfHZAS6jaIdch # The Checkpoint\n",
  312. "!unzip -q t5-chpt.zip\n",
  313. "!rm ./t5-chpt.zip"
  314. ],
  315. "metadata": {
  316. "id": "x-kHFEm8u8Xg"
  317. },
  318. "execution_count": null,
  319. "outputs": []
  320. },
  321. {
  322. "cell_type": "code",
  323. "source": [
  324. "!gdown -q 11Yb0QjyP2R3RvN1oSCX9m0DL2_bzDeZS # Parsivar for normalization\n",
  325. "!unzip -q ./Parsivar.zip\n",
  326. "!rm ./Parsivar.zip"
  327. ],
  328. "metadata": {
  329. "id": "CGVVxGpivULm"
  330. },
  331. "execution_count": null,
  332. "outputs": []
  333. },
  334. {
  335. "cell_type": "code",
  336. "source": [
  337. "! gdown 1OubKfFhLCVu-O43jfWyPQsZ4B2GNPM34 # GE2PE.py"
  338. ],
  339. "metadata": {
  340. "colab": {
  341. "base_uri": "https://localhost:8080/"
  342. },
  343. "id": "K-mPQF5ykcmF",
  344. "outputId": "9978105e-89c5-4d3c-dea8-72d507ce113b"
  345. },
  346. "execution_count": null,
  347. "outputs": [
  348. {
  349. "output_type": "stream",
  350. "name": "stdout",
  351. "text": [
  352. "Downloading...\n",
  353. "From (original): https://drive.google.com/uc?id=1OubKfFhLCVu-O43jfWyPQsZ4B2GNPM34\n",
  354. "From (redirected): https://drive.google.com/uc?id=1OubKfFhLCVu-O43jfWyPQsZ4B2GNPM34&confirm=t&uuid=188f1e6f-bf31-4162-8964-fd3368714ce9\n",
  355. "To: /content/GE2PE.py\n",
  356. "\r 0% 0.00/4.96k [00:00<?, ?B/s]\r100% 4.96k/4.96k [00:00<00:00, 15.1MB/s]\n"
  357. ]
  358. }
  359. ]
  360. },
  361. {
  362. "cell_type": "code",
  363. "source": [
  364. "!sed -i 's+from collections import Iterable+from collections.abc import Iterable+g' /content/Parsivar/token_merger.py"
  365. ],
  366. "metadata": {
  367. "id": "VIRvJy8naB0f"
  368. },
  369. "execution_count": null,
  370. "outputs": []
  371. },
  372. {
  373. "cell_type": "code",
  374. "source": [
  375. "from GE2PE import GE2PE\n",
  376. "\n",
  377. "g2p = GE2PE(model_path='/content/t5-chpt')\n",
  378. "\n",
  379. "g2p.generate(['تست مدل تبدیل نویسه به واج', 'این کتابِ علی است'], use_rules=True)"
  380. ],
  381. "metadata": {
  382. "colab": {
  383. "base_uri": "https://localhost:8080/"
  384. },
  385. "id": "Qs-J5B3ykaYz",
  386. "outputId": "c93bc1e9-f47b-40ea-b4d3-42123df09ce2"
  387. },
  388. "execution_count": null,
  389. "outputs": [
  390. {
  391. "output_type": "execute_result",
  392. "data": {
  393. "text/plain": [
  394. "['teste model t/bdil nevise be vaj', '@in ketabe @ali @/st']"
  395. ]
  396. },
  397. "metadata": {},
  398. "execution_count": 9
  399. }
  400. ]
  401. },
  402. {
  403. "cell_type": "markdown",
  404. "metadata": {
  405. "id": "XjAPkfq7SF87"
  406. },
  407. "source": [
  408. "# Get Evaluation Data"
  409. ]
  410. },
  411. {
  412. "cell_type": "code",
  413. "source": [
  414. "!wget https://huggingface.co/datasets/MahtaFetrat/SentenceBench/raw/main/SentenceBench.csv"
  415. ],
  416. "metadata": {
  417. "id": "qwCG0jX-88nQ",
  418. "colab": {
  419. "base_uri": "https://localhost:8080/"
  420. },
  421. "outputId": "3cc7169b-996d-4591-8e35-0e8c7b8a0a1c"
  422. },
  423. "execution_count": null,
  424. "outputs": [
  425. {
  426. "output_type": "stream",
  427. "name": "stdout",
  428. "text": [
  429. "--2025-05-13 06:47:54-- https://huggingface.co/datasets/MahtaFetrat/SentenceBench/raw/main/SentenceBench.csv\n",
  430. "Resolving huggingface.co (huggingface.co)... 3.163.189.90, 3.163.189.114, 3.163.189.37, ...\n",
  431. "Connecting to huggingface.co (huggingface.co)|3.163.189.90|:443... connected.\n",
  432. "HTTP request sent, awaiting response... 200 OK\n",
  433. "Length: 56026 (55K) [text/plain]\n",
  434. "Saving to: ‘SentenceBench.csv’\n",
  435. "\n",
  436. "\rSentenceBench.csv 0%[ ] 0 --.-KB/s \rSentenceBench.csv 100%[===================>] 54.71K --.-KB/s in 0.006s \n",
  437. "\n",
  438. "2025-05-13 06:47:54 (8.29 MB/s) - ‘SentenceBench.csv’ saved [56026/56026]\n",
  439. "\n"
  440. ]
  441. }
  442. ]
  443. },
  444. {
  445. "cell_type": "code",
  446. "source": [
  447. "sentence_bench = pd.read_csv('SentenceBench.csv')"
  448. ],
  449. "metadata": {
  450. "id": "hJO-UAPDQvcb"
  451. },
  452. "execution_count": null,
  453. "outputs": []
  454. },
  455. {
  456. "cell_type": "code",
  457. "source": [
  458. "sentence_bench.head(3)"
  459. ],
  460. "metadata": {
  461. "colab": {
  462. "base_uri": "https://localhost:8080/"
  463. },
  464. "id": "qlYbrnUa9LAN",
  465. "outputId": "3e39c590-1432-498c-9aab-60e0bc70fd12"
  466. },
  467. "execution_count": null,
  468. "outputs": [
  469. {
  470. "output_type": "execute_result",
  471. "data": {
  472. "text/plain": [
  473. " dataset grapheme \\\n",
  474. "0 homograph من قدر تو را می‌دانم \n",
  475. "1 homograph از قضای الهی به قدر الهی پناه می‌برم \n",
  476. "2 homograph به دست و صورتم کرم زدم \n",
  477. "\n",
  478. " phoneme homograph word \\\n",
  479. "0 man qadr-e to rA mi-dAnam قدر \n",
  480. "1 ?az qazAy ?elAhi be qadar-e ?elAhi panAh mi-baram قدر \n",
  481. "2 be dast-o suratam kerem zadam کرم \n",
  482. "\n",
  483. " pronunciation \n",
  484. "0 qadr \n",
  485. "1 qadar \n",
  486. "2 kerem "
  487. ],
  488. "text/html": [
  489. "\n",
  490. " <div id=\"df-9af3711a-4b17-4a4c-9f27-d0e5a5b2c708\" class=\"colab-df-container\">\n",
  491. " <div>\n",
  492. "<style scoped>\n",
  493. " .dataframe tbody tr th:only-of-type {\n",
  494. " vertical-align: middle;\n",
  495. " }\n",
  496. "\n",
  497. " .dataframe tbody tr th {\n",
  498. " vertical-align: top;\n",
  499. " }\n",
  500. "\n",
  501. " .dataframe thead th {\n",
  502. " text-align: right;\n",
  503. " }\n",
  504. "</style>\n",
  505. "<table border=\"1\" class=\"dataframe\">\n",
  506. " <thead>\n",
  507. " <tr style=\"text-align: right;\">\n",
  508. " <th></th>\n",
  509. " <th>dataset</th>\n",
  510. " <th>grapheme</th>\n",
  511. " <th>phoneme</th>\n",
  512. " <th>homograph word</th>\n",
  513. " <th>pronunciation</th>\n",
  514. " </tr>\n",
  515. " </thead>\n",
  516. " <tbody>\n",
  517. " <tr>\n",
  518. " <th>0</th>\n",
  519. " <td>homograph</td>\n",
  520. " <td>من قدر تو را می‌دانم</td>\n",
  521. " <td>man qadr-e to rA mi-dAnam</td>\n",
  522. " <td>قدر</td>\n",
  523. " <td>qadr</td>\n",
  524. " </tr>\n",
  525. " <tr>\n",
  526. " <th>1</th>\n",
  527. " <td>homograph</td>\n",
  528. " <td>از قضای الهی به قدر الهی پناه می‌برم</td>\n",
  529. " <td>?az qazAy ?elAhi be qadar-e ?elAhi panAh mi-baram</td>\n",
  530. " <td>قدر</td>\n",
  531. " <td>qadar</td>\n",
  532. " </tr>\n",
  533. " <tr>\n",
  534. " <th>2</th>\n",
  535. " <td>homograph</td>\n",
  536. " <td>به دست و صورتم کرم زدم</td>\n",
  537. " <td>be dast-o suratam kerem zadam</td>\n",
  538. " <td>کرم</td>\n",
  539. " <td>kerem</td>\n",
  540. " </tr>\n",
  541. " </tbody>\n",
  542. "</table>\n",
  543. "</div>\n",
  544. " <div class=\"colab-df-buttons\">\n",
  545. "\n",
  546. " <div class=\"colab-df-container\">\n",
  547. " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-9af3711a-4b17-4a4c-9f27-d0e5a5b2c708')\"\n",
  548. " title=\"Convert this dataframe to an interactive table.\"\n",
  549. " style=\"display:none;\">\n",
  550. "\n",
  551. " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
  552. " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
  553. " </svg>\n",
  554. " </button>\n",
  555. "\n",
  556. " <style>\n",
  557. " .colab-df-container {\n",
  558. " display:flex;\n",
  559. " gap: 12px;\n",
  560. " }\n",
  561. "\n",
  562. " .colab-df-convert {\n",
  563. " background-color: #E8F0FE;\n",
  564. " border: none;\n",
  565. " border-radius: 50%;\n",
  566. " cursor: pointer;\n",
  567. " display: none;\n",
  568. " fill: #1967D2;\n",
  569. " height: 32px;\n",
  570. " padding: 0 0 0 0;\n",
  571. " width: 32px;\n",
  572. " }\n",
  573. "\n",
  574. " .colab-df-convert:hover {\n",
  575. " background-color: #E2EBFA;\n",
  576. " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
  577. " fill: #174EA6;\n",
  578. " }\n",
  579. "\n",
  580. " .colab-df-buttons div {\n",
  581. " margin-bottom: 4px;\n",
  582. " }\n",
  583. "\n",
  584. " [theme=dark] .colab-df-convert {\n",
  585. " background-color: #3B4455;\n",
  586. " fill: #D2E3FC;\n",
  587. " }\n",
  588. "\n",
  589. " [theme=dark] .colab-df-convert:hover {\n",
  590. " background-color: #434B5C;\n",
  591. " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
  592. " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
  593. " fill: #FFFFFF;\n",
  594. " }\n",
  595. " </style>\n",
  596. "\n",
  597. " <script>\n",
  598. " const buttonEl =\n",
  599. " document.querySelector('#df-9af3711a-4b17-4a4c-9f27-d0e5a5b2c708 button.colab-df-convert');\n",
  600. " buttonEl.style.display =\n",
  601. " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
  602. "\n",
  603. " async function convertToInteractive(key) {\n",
  604. " const element = document.querySelector('#df-9af3711a-4b17-4a4c-9f27-d0e5a5b2c708');\n",
  605. " const dataTable =\n",
  606. " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
  607. " [key], {});\n",
  608. " if (!dataTable) return;\n",
  609. "\n",
  610. " const docLinkHtml = 'Like what you see? Visit the ' +\n",
  611. " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
  612. " + ' to learn more about interactive tables.';\n",
  613. " element.innerHTML = '';\n",
  614. " dataTable['output_type'] = 'display_data';\n",
  615. " await google.colab.output.renderOutput(dataTable, element);\n",
  616. " const docLink = document.createElement('div');\n",
  617. " docLink.innerHTML = docLinkHtml;\n",
  618. " element.appendChild(docLink);\n",
  619. " }\n",
  620. " </script>\n",
  621. " </div>\n",
  622. "\n",
  623. "\n",
  624. " <div id=\"df-6a4d0ec4-3b50-498c-8bf9-78597a6daaaf\">\n",
  625. " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-6a4d0ec4-3b50-498c-8bf9-78597a6daaaf')\"\n",
  626. " title=\"Suggest charts\"\n",
  627. " style=\"display:none;\">\n",
  628. "\n",
  629. "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
  630. " width=\"24px\">\n",
  631. " <g>\n",
  632. " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
  633. " </g>\n",
  634. "</svg>\n",
  635. " </button>\n",
  636. "\n",
  637. "<style>\n",
  638. " .colab-df-quickchart {\n",
  639. " --bg-color: #E8F0FE;\n",
  640. " --fill-color: #1967D2;\n",
  641. " --hover-bg-color: #E2EBFA;\n",
  642. " --hover-fill-color: #174EA6;\n",
  643. " --disabled-fill-color: #AAA;\n",
  644. " --disabled-bg-color: #DDD;\n",
  645. " }\n",
  646. "\n",
  647. " [theme=dark] .colab-df-quickchart {\n",
  648. " --bg-color: #3B4455;\n",
  649. " --fill-color: #D2E3FC;\n",
  650. " --hover-bg-color: #434B5C;\n",
  651. " --hover-fill-color: #FFFFFF;\n",
  652. " --disabled-bg-color: #3B4455;\n",
  653. " --disabled-fill-color: #666;\n",
  654. " }\n",
  655. "\n",
  656. " .colab-df-quickchart {\n",
  657. " background-color: var(--bg-color);\n",
  658. " border: none;\n",
  659. " border-radius: 50%;\n",
  660. " cursor: pointer;\n",
  661. " display: none;\n",
  662. " fill: var(--fill-color);\n",
  663. " height: 32px;\n",
  664. " padding: 0;\n",
  665. " width: 32px;\n",
  666. " }\n",
  667. "\n",
  668. " .colab-df-quickchart:hover {\n",
  669. " background-color: var(--hover-bg-color);\n",
  670. " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
  671. " fill: var(--button-hover-fill-color);\n",
  672. " }\n",
  673. "\n",
  674. " .colab-df-quickchart-complete:disabled,\n",
  675. " .colab-df-quickchart-complete:disabled:hover {\n",
  676. " background-color: var(--disabled-bg-color);\n",
  677. " fill: var(--disabled-fill-color);\n",
  678. " box-shadow: none;\n",
  679. " }\n",
  680. "\n",
  681. " .colab-df-spinner {\n",
  682. " border: 2px solid var(--fill-color);\n",
  683. " border-color: transparent;\n",
  684. " border-bottom-color: var(--fill-color);\n",
  685. " animation:\n",
  686. " spin 1s steps(1) infinite;\n",
  687. " }\n",
  688. "\n",
  689. " @keyframes spin {\n",
  690. " 0% {\n",
  691. " border-color: transparent;\n",
  692. " border-bottom-color: var(--fill-color);\n",
  693. " border-left-color: var(--fill-color);\n",
  694. " }\n",
  695. " 20% {\n",
  696. " border-color: transparent;\n",
  697. " border-left-color: var(--fill-color);\n",
  698. " border-top-color: var(--fill-color);\n",
  699. " }\n",
  700. " 30% {\n",
  701. " border-color: transparent;\n",
  702. " border-left-color: var(--fill-color);\n",
  703. " border-top-color: var(--fill-color);\n",
  704. " border-right-color: var(--fill-color);\n",
  705. " }\n",
  706. " 40% {\n",
  707. " border-color: transparent;\n",
  708. " border-right-color: var(--fill-color);\n",
  709. " border-top-color: var(--fill-color);\n",
  710. " }\n",
  711. " 60% {\n",
  712. " border-color: transparent;\n",
  713. " border-right-color: var(--fill-color);\n",
  714. " }\n",
  715. " 80% {\n",
  716. " border-color: transparent;\n",
  717. " border-right-color: var(--fill-color);\n",
  718. " border-bottom-color: var(--fill-color);\n",
  719. " }\n",
  720. " 90% {\n",
  721. " border-color: transparent;\n",
  722. " border-bottom-color: var(--fill-color);\n",
  723. " }\n",
  724. " }\n",
  725. "</style>\n",
  726. "\n",
  727. " <script>\n",
  728. " async function quickchart(key) {\n",
  729. " const quickchartButtonEl =\n",
  730. " document.querySelector('#' + key + ' button');\n",
  731. " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
  732. " quickchartButtonEl.classList.add('colab-df-spinner');\n",
  733. " try {\n",
  734. " const charts = await google.colab.kernel.invokeFunction(\n",
  735. " 'suggestCharts', [key], {});\n",
  736. " } catch (error) {\n",
  737. " console.error('Error during call to suggestCharts:', error);\n",
  738. " }\n",
  739. " quickchartButtonEl.classList.remove('colab-df-spinner');\n",
  740. " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
  741. " }\n",
  742. " (() => {\n",
  743. " let quickchartButtonEl =\n",
  744. " document.querySelector('#df-6a4d0ec4-3b50-498c-8bf9-78597a6daaaf button');\n",
  745. " quickchartButtonEl.style.display =\n",
  746. " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
  747. " })();\n",
  748. " </script>\n",
  749. " </div>\n",
  750. " </div>\n",
  751. " </div>\n"
  752. ],
  753. "application/vnd.google.colaboratory.intrinsic+json": {
  754. "type": "dataframe",
  755. "variable_name": "sentence_bench",
  756. "summary": "{\n \"name\": \"sentence_bench\",\n \"rows\": 400,\n \"fields\": [\n {\n \"column\": \"dataset\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"homograph\",\n \"mana-tts\",\n \"commonvoice\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"grapheme\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 400,\n \"samples\": [\n \"\\u0622\\u06cc\\u0627 \\u0628\\u0627\\u06cc\\u062f \\u062d\\u0642\\u06cc\\u0642\\u062a \\u0631\\u0627 \\u0628\\u0647 \\u0622\\u0646\\u200c\\u0647\\u0627 \\u0628\\u06af\\u0648\\u06cc\\u06cc\\u0645\\u061f\",\n \"\\u06a9\\u0647 \\u067e\\u06cc\\u0634 \\u0627\\u0632 \\u0627\\u0646\\u0642\\u0644\\u0627\\u0628 \\u0628\\u0647 \\u062e\\u0648\\u0627\\u0628\\u06af\\u0627\\u0647 \\u062f\\u062e\\u062a\\u0631\\u0627\\u0646 \\u0648 \\u0632\\u0646\\u0627\\u0646 \\u0646\\u0627\\u0628\\u06cc\\u0646\\u0627 \\u0627\\u062e\\u062a\\u0635\\u0627\\u0635\\u200c\\u06cc\\u0627\\u0641\\u062a\\u0647 \\u0628\\u0648\\u062f. \\u0627\\u063a\\u0644\\u0628 \\u0632\\u0646\\u0627\\u0646\\u06cc \\u06a9\\u0647 \\u062f\\u0631 \\u0627\\u06cc\\u0646 \\u062e\\u0648\\u0627\\u0628\\u06af\\u0627\\u0647 \\u0632\\u0646\\u062f\\u06af\\u06cc \\u0645\\u06cc\\u200c\\u06a9\\u0631\\u062f\\u0646\\u062f\\u060c \",\n \"\\u062f\\u0648\\u062f \\u0648 \\u0645\\u0647 \\u063a\\u0644\\u06cc\\u0638\\u06cc \\u062f\\u0631 \\u0645\\u062d\\u06cc\\u0637 \\u067e\\u06cc\\u0686\\u06cc\\u062f\\u0647 \\u0628\\u0648\\u062f\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"phoneme\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 400,\n \"samples\": [\n \"?AyA bAyad haqiqat rA be ?AnhA beguyim\\u061f\",\n \"ke piS ?az ?enqelAb be xAbgAh-e doxtarAn va zanAn-e nAbinA ?extesAsyAfte bud ?aqlab-e zanAni ke dar ?in xAbgAh zendegi mikardand\",\n \"dud-o meh-e qalizi dar mohit piCide bud\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"homograph word\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 101,\n \"samples\": [\n \"\\u06af\\u0631\\u06cc\\u0645\",\n \"\\u0633\\u0628\\u06a9\\u06cc\",\n \"\\u06a9\\u0645\\u06cc\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"pronunciation\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 210,\n \"samples\": [\n \"darham\",\n \"Sum\",\n \"moSk\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
  757. }
  758. },
  759. "metadata": {},
  760. "execution_count": 12
  761. }
  762. ]
  763. },
  764. {
  765. "cell_type": "markdown",
  766. "metadata": {
  767. "id": "wDV7ysXf2b_H"
  768. },
  769. "source": [
  770. "### Get ManaTTS"
  771. ]
  772. },
  773. {
  774. "cell_type": "code",
  775. "execution_count": null,
  776. "metadata": {
  777. "colab": {
  778. "base_uri": "https://localhost:8080/"
  779. },
  780. "id": "TcL5ZLvSSnVB",
  781. "outputId": "ea215f38-f740-4dbe-ef3c-377d920ae7a6"
  782. },
  783. "outputs": [
  784. {
  785. "output_type": "execute_result",
  786. "data": {
  787. "text/plain": [
  788. "[('در این نوشته بنا داریم با یک ابزار ساده و مکانیکی افزایش بینایی برای افراد کم\\u200cبینا ',\n",
  789. " 'dar ?in neveSte banA dArim bA yek ?abzAr-e sAde va mekAniki-ye ?afzAyeS-e binAyi barAye ?afrAd-e kam\\u200cbinA '),\n",
  790. " ('به نام بی\\u200cوپتیک یا عدسی دورنما آشنا شویم. ',\n",
  791. " 'be nAm-e biyoptik yA ?adasi-ye durnamA ?ASnA Savim'),\n",
  792. " ('دراین\\u200cصورت، انجام خودارزیابی و ارائه بازخورد بر عهده خودتان است. ',\n",
  793. " 'dar ?in surat ?anjAm-e xod?arzyAbi va ?erA?e-ye bAzxord bar ?ohde-ye xodetAn ?ast ')]"
  794. ]
  795. },
  796. "metadata": {},
  797. "execution_count": 13
  798. }
  799. ],
  800. "source": [
  801. "filtered_rows = sentence_bench[sentence_bench['dataset'] == 'mana-tts'][['grapheme', 'phoneme']]\n",
  802. "\n",
  803. "# Convert to a list of tuples\n",
  804. "mana_evaluation_data = list(filtered_rows.itertuples(index=False, name=None))\n",
  805. "\n",
  806. "mana_evaluation_data[:3]"
  807. ]
  808. },
  809. {
  810. "cell_type": "markdown",
  811. "metadata": {
  812. "id": "Jjacw9Mp2eoX"
  813. },
  814. "source": [
  815. "### Get CommonVoice"
  816. ]
  817. },
  818. {
  819. "cell_type": "code",
  820. "execution_count": null,
  821. "metadata": {
  822. "id": "-yQnqCGw26sk",
  823. "colab": {
  824. "base_uri": "https://localhost:8080/"
  825. },
  826. "outputId": "1aa853ff-f8fa-474a-8d13-b00ffa31c68c"
  827. },
  828. "outputs": [
  829. {
  830. "output_type": "execute_result",
  831. "data": {
  832. "text/plain": [
  833. "[('در اکثر شهرها، مرکزی برای خرید دوچرخه وجود دارد.',\n",
  834. " 'dar ?aksar-e Sahr-hA, markazi barAye xarid-e doCarxe vojud dArad.'),\n",
  835. " ('پس از مدرسه کودکان به سوی خانه جست و خیز کردند.',\n",
  836. " 'pas ?az madrese kudakAn be suye xAne jast-o-xiz kardand.'),\n",
  837. " ('شما نگران زن و بچه این نباش.', 'SomA negarAn-e zan-o-baCCe-ye ?in nabAS.')]"
  838. ]
  839. },
  840. "metadata": {},
  841. "execution_count": 14
  842. }
  843. ],
  844. "source": [
  845. "filtered_rows = sentence_bench[sentence_bench['dataset'] == 'commonvoice'][['grapheme', 'phoneme']]\n",
  846. "\n",
  847. "# Convert to a list of tuples\n",
  848. "commonvoice_evaluation_data = list(filtered_rows.itertuples(index=False, name=None))\n",
  849. "\n",
  850. "commonvoice_evaluation_data[:3]"
  851. ]
  852. },
  853. {
  854. "cell_type": "markdown",
  855. "metadata": {
  856. "id": "ciSPyhRc3Rvo"
  857. },
  858. "source": [
  859. "### Get Homograph"
  860. ]
  861. },
  862. {
  863. "cell_type": "code",
  864. "execution_count": null,
  865. "metadata": {
  866. "id": "XlFc5JbN3Rvz",
  867. "colab": {
  868. "base_uri": "https://localhost:8080/"
  869. },
  870. "outputId": "4e9f3376-2456-4dd1-d267-54677e05c158"
  871. },
  872. "outputs": [
  873. {
  874. "output_type": "execute_result",
  875. "data": {
  876. "text/plain": [
  877. "[('من قدر تو را می\\u200cدانم', 'man qadr-e to rA mi-dAnam', 'قدر', 'qadr'),\n",
  878. " ('از قضای الهی به قدر الهی پناه می\\u200cبرم',\n",
  879. " '?az qazAy ?elAhi be qadar-e ?elAhi panAh mi-baram',\n",
  880. " 'قدر',\n",
  881. " 'qadar'),\n",
  882. " ('به دست و صورتم کرم زدم', 'be dast-o suratam kerem zadam', 'کرم', 'kerem')]"
  883. ]
  884. },
  885. "metadata": {},
  886. "execution_count": 15
  887. }
  888. ],
  889. "source": [
  890. "filtered_rows = sentence_bench[sentence_bench['dataset'] == 'homograph'][['grapheme', 'phoneme', 'homograph word',\t'pronunciation']]\n",
  891. "\n",
  892. "# Convert to a list of tuples\n",
  893. "homograph_evaluation_data = list(filtered_rows.itertuples(index=False, name=None))\n",
  894. "\n",
  895. "homograph_evaluation_data[:3]"
  896. ]
  897. },
  898. {
  899. "cell_type": "markdown",
  900. "metadata": {
  901. "id": "R6PE5ds45TPr"
  902. },
  903. "source": [
  904. "# Evaluate Method Outputs"
  905. ]
  906. },
  907. {
  908. "cell_type": "markdown",
  909. "metadata": {
  910. "id": "y73zFlRGIbt9"
  911. },
  912. "source": [
  913. "## PER Evaluation"
  914. ]
  915. },
  916. {
  917. "cell_type": "code",
  918. "execution_count": null,
  919. "metadata": {
  920. "id": "ItuviO3w5Vzv"
  921. },
  922. "outputs": [],
  923. "source": [
  924. "def remove_non_word_chars(text):\n",
  925. " pattern = r'[^\\w\\s\\?]'\n",
  926. " cleaned_text = re.sub(pattern, ' ', text)\n",
  927. " return cleaned_text"
  928. ]
  929. },
  930. {
  931. "cell_type": "code",
  932. "execution_count": null,
  933. "metadata": {
  934. "id": "syQCurXu51TO"
  935. },
  936. "outputs": [],
  937. "source": [
  938. "def remove_white_spaces(text):\n",
  939. " cleaned_text = re.sub(r'\\s+', ' ', text)\n",
  940. " return cleaned_text.strip()"
  941. ]
  942. },
  943. {
  944. "cell_type": "code",
  945. "execution_count": null,
  946. "metadata": {
  947. "id": "V7APkVM053RP"
  948. },
  949. "outputs": [],
  950. "source": [
  951. "def get_word_only_text(text):\n",
  952. " word_only_text = remove_non_word_chars(text)\n",
  953. " extra_space_removed_text = remove_white_spaces(word_only_text)\n",
  954. "\n",
  955. " return extra_space_removed_text"
  956. ]
  957. },
  958. {
  959. "cell_type": "code",
  960. "execution_count": null,
  961. "metadata": {
  962. "id": "ROomKSao57vy"
  963. },
  964. "outputs": [],
  965. "source": [
  966. "def get_texts_cer(reference, model_output):\n",
  967. " # Preprocess input texts to only contain word characters\n",
  968. " word_only_reference = get_word_only_text(reference)\n",
  969. " word_only_output = get_word_only_text(model_output)\n",
  970. "\n",
  971. " # Return +infinity for CER if any of the texts is empty\n",
  972. " if not word_only_reference.strip() or not word_only_output.strip():\n",
  973. " return float('inf')\n",
  974. "\n",
  975. " return cer(word_only_reference, word_only_output)"
  976. ]
  977. },
  978. {
  979. "cell_type": "code",
  980. "execution_count": null,
  981. "metadata": {
  982. "id": "4vHLUjp48hc3"
  983. },
  984. "outputs": [],
  985. "source": [
  986. "def get_avg_cer_of_method(method_outputs, references):\n",
  987. " cers = []\n",
  988. " for idx, o in enumerate(method_outputs):\n",
  989. " cer = get_texts_cer(o.replace('-', ''), references[idx][1].replace('-', ''))\n",
  990. " if cer != float('inf'):\n",
  991. " cers.append(cer)\n",
  992. "\n",
  993. " return sum(cers) / len(cers)"
  994. ]
  995. },
  996. {
  997. "cell_type": "markdown",
  998. "metadata": {
  999. "id": "oBgNtpFQDwku"
  1000. },
  1001. "source": [
  1002. "## Homograph Evaluation"
  1003. ]
  1004. },
  1005. {
  1006. "cell_type": "code",
  1007. "execution_count": null,
  1008. "metadata": {
  1009. "id": "J445ULEvEEDn"
  1010. },
  1011. "outputs": [],
  1012. "source": [
  1013. "def get_homograph_performance(outputs, references):\n",
  1014. " corrects = 0\n",
  1015. " total = 0\n",
  1016. "\n",
  1017. " for idx, (g, p, homograph, right) in enumerate(references):\n",
  1018. " if homograph != '':\n",
  1019. " total += 1\n",
  1020. " if right in outputs[idx]:\n",
  1021. " corrects += 1\n",
  1022. "\n",
  1023. " return corrects / total"
  1024. ]
  1025. },
  1026. {
  1027. "cell_type": "markdown",
  1028. "metadata": {
  1029. "id": "JGEUIrbi9kNH"
  1030. },
  1031. "source": [
  1032. "# Full bench"
  1033. ]
  1034. },
  1035. {
  1036. "cell_type": "code",
  1037. "execution_count": null,
  1038. "metadata": {
  1039. "id": "fGzQvL8V9mln"
  1040. },
  1041. "outputs": [],
  1042. "source": [
  1043. "benchmark = []\n",
  1044. "\n",
  1045. "for g, p in mana_evaluation_data:\n",
  1046. " benchmark.append((g, p, '', ''))\n",
  1047. "\n",
  1048. "for g, p in commonvoice_evaluation_data:\n",
  1049. " benchmark.append((g, p, '', ''))\n",
  1050. "\n",
  1051. "for g, p, w, r in homograph_evaluation_data:\n",
  1052. " benchmark.append((g, p, w, r))\n",
  1053. "\n",
  1054. "benchmark = benchmark[:400]"
  1055. ]
  1056. },
  1057. {
  1058. "cell_type": "code",
  1059. "execution_count": null,
  1060. "metadata": {
  1061. "id": "4jlXFt8tCPWB"
  1062. },
  1063. "outputs": [],
  1064. "source": [
  1065. "def print_all_metrics(predictions):\n",
  1066. " per = get_avg_cer_of_method(predictions, benchmark) * 100\n",
  1067. " # acc, prec, recall = get_phonetic_model_performance(predictions, benchmark)\n",
  1068. " homograph = get_homograph_performance(predictions, benchmark) * 100\n",
  1069. "\n",
  1070. " print(f\"PER: \\t\\t\\t{per:.4f}\")\n",
  1071. " print(f\"HOMOGRAPH: \\t\\t{homograph:.4f}\")"
  1072. ]
  1073. },
  1074. {
  1075. "cell_type": "markdown",
  1076. "source": [
  1077. "# Inference"
  1078. ],
  1079. "metadata": {
  1080. "id": "fTRgGM_8_Fwg"
  1081. }
  1082. },
  1083. {
  1084. "cell_type": "code",
  1085. "source": [
  1086. "graphemes = [item[0] for item in benchmark]"
  1087. ],
  1088. "metadata": {
  1089. "id": "17lrgWh__Mzr"
  1090. },
  1091. "execution_count": null,
  1092. "outputs": []
  1093. },
  1094. {
  1095. "cell_type": "code",
  1096. "source": [
  1097. "import time\n",
  1098. "\n",
  1099. "start_time = time.time()\n",
  1100. "\n",
  1101. "outputs = g2p.generate(graphemes, use_rules=True)\n",
  1102. "\n",
  1103. "total_time = time.time() - start_time\n",
  1104. "avg_time = total_time / len(graphemes) if len(graphemes) > 0 else 0"
  1105. ],
  1106. "metadata": {
  1107. "id": "ajqTWtNb_HBd"
  1108. },
  1109. "execution_count": null,
  1110. "outputs": []
  1111. },
  1112. {
  1113. "cell_type": "markdown",
  1114. "source": [
  1115. "# Mapping"
  1116. ],
  1117. "metadata": {
  1118. "id": "jPXWBZ4R_bGs"
  1119. }
  1120. },
  1121. {
  1122. "cell_type": "code",
  1123. "source": [
  1124. "mapped_outputs = []\n",
  1125. "\n",
  1126. "# Define the replacements\n",
  1127. "replacements = {\n",
  1128. " 'a': 'A',\n",
  1129. " '$': 'S',\n",
  1130. " '/': 'a',\n",
  1131. " '1': '',\n",
  1132. " ';': 'Z',\n",
  1133. " '@': '?',\n",
  1134. " 'c': 'C'\n",
  1135. "}\n",
  1136. "\n",
  1137. "# Apply replacements\n",
  1138. "mapped_outputs = [\n",
  1139. " ''.join(replacements.get(char, char) for char in output)\n",
  1140. " for output in outputs\n",
  1141. "]"
  1142. ],
  1143. "metadata": {
  1144. "id": "c8C2sJjJA4na"
  1145. },
  1146. "execution_count": null,
  1147. "outputs": []
  1148. },
  1149. {
  1150. "cell_type": "markdown",
  1151. "source": [
  1152. "# Results"
  1153. ],
  1154. "metadata": {
  1155. "id": "JAIAobLFCKCr"
  1156. }
  1157. },
  1158. {
  1159. "cell_type": "code",
  1160. "source": [
  1161. "print_all_metrics(mapped_outputs)\n",
  1162. "print(f\"TOTAL TIME:\\t\\t{total_time:.4f} (s)\")\n",
  1163. "print(f\"AVG TIME:\\t\\t{avg_time:.4f} (s)+\")"
  1164. ],
  1165. "metadata": {
  1166. "colab": {
  1167. "base_uri": "https://localhost:8080/"
  1168. },
  1169. "id": "CEs_TODaAFHO",
  1170. "outputId": "b845b66a-d67d-4bc5-975e-4c0ef7ef628a"
  1171. },
  1172. "execution_count": null,
  1173. "outputs": [
  1174. {
  1175. "output_type": "stream",
  1176. "name": "stdout",
  1177. "text": [
  1178. "PER: \t\t\t4.2196\n",
  1179. "HOMOGRAPH: \t\t75.9434\n",
  1180. "TOTAL TIME:\t\t177.5523 (s)\n",
  1181. "AVG TIME:\t\t0.4439 (s)+\n"
  1182. ]
  1183. }
  1184. ]
  1185. },
  1186. {
  1187. "cell_type": "markdown",
  1188. "source": [
  1189. "# Runs\n",
  1190. "\n",
  1191. "## First:\n",
  1192. "\n",
  1193. "```\n",
  1194. "PER: \t\t\t3.9804\n",
  1195. "HOMOGRAPH: \t\t76.8868\n",
  1196. "TOTAL TIME:\t\t182.5777 (s)\n",
  1197. "AVG TIME:\t\t0.4564 (s)+\n",
  1198. "```\n",
  1199. "\n",
  1200. "## Second\n",
  1201. "\n",
  1202. "```\n",
  1203. "PER: \t\t\t3.9804\n",
  1204. "HOMOGRAPH: \t\t76.8868\n",
  1205. "TOTAL TIME:\t\t191.1550 (s)\n",
  1206. "AVG TIME:\t\t0.4779 (s)+\n",
  1207. "```\n",
  1208. "\n",
  1209. "## Third\n",
  1210. "\n",
  1211. "```\n",
  1212. "PER: \t\t\t4.2196\n",
  1213. "HOMOGRAPH: \t\t75.9434\n",
  1214. "TOTAL TIME:\t\t173.4838 (s)\n",
  1215. "AVG TIME:\t\t0.4337 (s)+\n",
  1216. "```\n",
  1217. "\n",
  1218. "## Fourth\n",
  1219. "\n",
  1220. "```\n",
  1221. "PER: \t\t\t4.2196\n",
  1222. "HOMOGRAPH: \t\t75.9434\n",
  1223. "TOTAL TIME:\t\t103.5244 (s)\n",
  1224. "AVG TIME:\t\t0.2588 (s)+\n",
  1225. "```\n",
  1226. "\n",
  1227. "## Fifth\n",
  1228. "\n",
  1229. "```\n",
  1230. "PER: \t\t\t4.2196\n",
  1231. "HOMOGRAPH: \t\t75.9434\n",
  1232. "TOTAL TIME:\t\t177.5523 (s)\n",
  1233. "AVG TIME:\t\t0.4439 (s)+\n",
  1234. "```"
  1235. ],
  1236. "metadata": {
  1237. "id": "DeOaBaWEJI6x"
  1238. }
  1239. }
  1240. ]
  1241. }