Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'truncate', False)
wordEntryList = wordTier.entryList
# Do the naive alignment
for startT, stopT, label in utteranceTier.entryList:
wordList = label.split()
# Get the list of phones in each word
superPhoneList = []
numPhones = 0
i = 0
while i < len(wordList):
word = wordList[i]
try:
firstSyllableList = isleDict.lookup(word)[0][0][0]
except isletool.WordNotInISLE:
wordList.pop(i)
continue
phoneList = [phone for syllable in firstSyllableList
for phone in syllable]
superPhoneList.append(phoneList)
numPhones += len(phoneList)
i += 1
# Get the naive alignment for words, if alignment doesn't
# already exist for words
subWordEntryList = []
subPhoneEntryList = []
if wordTier is not None:
subWordEntryList = wordTier.crop(startT, stopT,
"truncated", False).entryList
isleDict = isletool.LexicalTool(isleFN)
existFNList = utils.findFiles(outputPath, filterPaths=".txt")
for fn in utils.findFiles(tgInfoPath, filterExt=".txt",
skipIfNameInList=existFNList):
if os.path.exists(join(outputPath, fn)):
continue
print(fn)
dataList = utils.openCSV(tgInfoPath, fn)
dataList = [row[2] for row in dataList] # start, stop, tmpLabel
outputList = []
for tmpLabel in dataList:
if tmpLabel not in skipList:
syllableCount, phoneCount = isletool.getNumPhones(isleDict,
tmpLabel,
maxFlag=True)
else:
syllableCount, phoneCount = 0, 0
outputList.append("%d,%d" % (syllableCount, phoneCount))
outputTxt = "\n".join(outputList)
with open(join(outputPath, fn), "w") as fd:
fd.write(outputTxt)
def checkFunc(word):
try:
isleDict.lookup(word)
except isletool.WordNotInISLE:
returnVal = False
else:
returnVal = True
return returnVal
def manualPhoneCount(tgInfoPath, isleFN, outputPath, skipList=None):
if skipList is None:
skipList = []
utils.makeDir(outputPath)
isleDict = isletool.LexicalTool(isleFN)
existFNList = utils.findFiles(outputPath, filterPaths=".txt")
for fn in utils.findFiles(tgInfoPath, filterExt=".txt",
skipIfNameInList=existFNList):
if os.path.exists(join(outputPath, fn)):
continue
print(fn)
dataList = utils.openCSV(tgInfoPath, fn)
dataList = [row[2] for row in dataList] # start, stop, tmpLabel
outputList = []
for tmpLabel in dataList:
if tmpLabel not in skipList:
syllableCount, phoneCount = isletool.getNumPhones(isleDict,
tmpLabel,
#encoding: utf-8
'''
Examples of how to use pysle's pronunciationtools code
'''
from os.path import join
from pysle import isletool
from pysle import pronunciationtools
root = join(".", "files")
isleDict = isletool.LexicalTool(join(root, 'ISLEdict_sample.txt'))
# In the first example we determine the syllabification of a word,
# as it was said. (Of course, this is just an estimate)
print('-' * 50)
searchWord = 'another'
anotherPhoneList = ['n', '@', 'th', 'r']
isleWordList = isleDict.lookup(searchWord)
returnList = pronunciationtools.findBestSyllabification(
isleDict, searchWord, anotherPhoneList)
(stressedSyllable, stressedPhone, syllableList, syllabification,
stressedSyllableIndexList, stressedPhoneIndexList,
flattenedStressIndexList) = returnList
print(searchWord)
print(anotherPhoneList)
print(stressedSyllableIndexList) # We can see the first syllable was elided
def simplifyPronunciation(phoneList):
'''
Simplifies pronunciation
Removes diacritics and unifies vowels and rhotics
'''
retList = []
for phone in phoneList:
# Remove diacritics
for diacritic in isletool.diacriticList:
phone = phone.replace(diacritic, u'')
# Unify rhotics
if 'r' in phone:
phone = 'r'
phone = phone.lower()
# Unify vowels
if isletool.isVowel(phone):
phone = u'V'
# Only represent the string by its first letter
try:
phone = phone[0]
except IndexError:
#encoding: utf-8
'''
Examples that use the isletool
- looking up words based on their pronunciation
- syllabifying a user-specified phone list based on a pronunciation
in the dictionary
'''
from os.path import join
from pysle import isletool
root = join(".", "files")
isleDict = isletool.LexicalTool(join(root, 'ISLEdict_sample.txt'))
# In this first example we look up the syllabification of a word and
# get it's stress information.
searchWord = 'catatonic'
lookupResults = isleDict.lookup(searchWord)
firstEntry = lookupResults[0][0]
firstSyllableList = firstEntry[0]
firstSyllableList = ".".join([u" ".join(syllable)
for syllable in firstSyllableList])
firstStressList = firstEntry[1]
print(searchWord)
print(firstSyllableList)
print(firstStressList) # 3rd syllable carries stress
def syllabifyTextgrids(tgPath, islePath):
isleDict = isletool.LexicalTool(islePath)
outputPath = join(tgPath, "syllabifiedTGs")
utils.makeDir(outputPath)
skipLabelList = ["", "xx", "", "{B_TRANS}", '{E_TRANS}']
for fn in utils.findFiles(tgPath, filterExt=".TextGrid"):
if os.path.exists(join(outputPath, fn)):
continue
tg = tgio.openTextgrid(join(tgPath, fn))
syllableTG = praattools.syllabifyTextgrid(isleDict, tg, "words",
"phones",
skipLabelList=skipLabelList)
# In the fourth example, we try to find word pairs in the dictionary
# Once, found, they could be fed into findBestSyllabification() for
# example.
print('-' * 50)
sentenceList = ["another", "australian", "seal", "pumpkins", "parley"]
retList = isletool.autopair(isleDict, sentenceList)[0]
for sentence in retList:
print(sentence)
# In the fifth example, we try to get a pronunciation for a whole
# sentence all at once.
print('-' * 50)
sentence = "do you want another pumpkinseed"
phoneList = isletool.transcribe(isleDict, sentence, 'longest')
print(phoneList)
#encoding: utf-8
'''
An example of how to syllabify a textgrid.
This code is an optional feature of pysle that requires the
praatio library.
'''
from os.path import join
from praatio import tgio
from pysle import isletool
from pysle import praattools
root = join('.', 'files')
isleDict = isletool.LexicalTool(join(root, "ISLEdict_sample.txt"))
tg = tgio.openTextgrid(join(root, "pumpkins.TextGrid"))
# Get the syllabification tiers and add it to the textgrid
syllableTG = praattools.syllabifyTextgrid(isleDict, tg, "word", "phone",
skipLabelList=["", ])
tg.addTier(syllableTG.tierDict["syllable"])
tg.addTier(syllableTG.tierDict["tonicSyllable"])
tg.addTier(syllableTG.tierDict["tonicVowel"])
tg.save(join(root, "pumpkins_with_syllables.TextGrid"))