Skip to content

Instantly share code, notes, and snippets.

@vpnry
Created November 22, 2020 13:44
Show Gist options
  • Select an option

  • Save vpnry/260631845a925971d9ac33021fb35b60 to your computer and use it in GitHub Desktop.

Select an option

Save vpnry/260631845a925971d9ac33021fb35b60 to your computer and use it in GitHub Desktop.
Convert English to IPA - Using Python3 module eng_to_ipa
# Convert English text to IPA using eng_to_ipa
# @pnry - version 0.0.1 - public domain
# pip3 install eng_to_ipa
# Usage:
# toIPA(f, 1): paragraph by paragraph with Eng paragraph
# toIPA(f, 0): insert IPA transcription next to word
# Input file
f = "AOP Appendix.fm - sadhu"
f = f + ".txt"
# ----------------------------------------------
import eng_to_ipa as ipa
import time
def toIPA(f, y):
writ = ""
print("Transcripting " + f + " to IPA ")
print("It may take a while, please wait...")
print("")
with open(f, "r") as t:
tex = t.read()
tex = tex.strip()
tex = tex.replace("\n", " p15081553y ")
ip = ipa.convert(tex)
tex = tex.split(" p15081553y ")
ip = ip.split("p15081553y*")
para = len(tex)
print("Total: " + str(para) + " paragraphs")
if y == 1:
print("Arrange paragraph by paragraph")
for i in range(para):
print("Processing par no.: " + str(i + 1))
writ += (
"["
+ str(i + 1)
+ "] "
+ tex[i].strip()
+ "\n\n"
+ "["
+ str(i + 1)
+ "] "
+ ip[i].strip()
)
writ += "\n\n"
###
else:
print("Insert IPA next to each word")
for i in range(para):
print("Processing par no.: " + str(i + 1))
enp = tex[i].strip().split(" ")
ipp = ip[i].strip().split(" ")
for k in range(len(enp)):
writ += enp[k] + "[" + ipp[k] + "] "
writ += "\n\n"
###
with open("_ipa_" + f, "w") as p:
p.write(writ)
# ----------------------------------------------
t1 = time.time()
toIPA(f, 1)
# ----------------------------------------------
print("")
print("#-----------------------")
print("Done!")
print("Took: " + str((time.time() - t1)) + " seconds")
print("#-----------------------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment