## Mass syllabifier using CMU Pronouncing Dictionary and 
##   NIST Sylabifier. 
## Program takes in file of words to be syllabified separated
##   by spaces. Creates dictionary of word:syllabification.
##   This is outputted as a text file with one dictionary
##   entry per line

import sys
from nltk.corpus import cmudict
import NIST


## needs textfile of words to exist in specified directory
## imports words from file as list of words
def getWords(wordLst):

    if(len(sys.argv) == 2):
        fileName = sys.argv[1]
    else:
        print("Error. No word file provided.")
        sys.exit()

    wordFile = open(fileName,'r') 

    words = ""
    for line in wordFile:
        words = words + ' ' + line
    words = words.split()

    for i in words:
        wordLst.append(i)

    return wordLst


## looks up each word in cmudict and adds the word and pronunciation
## to a dictionary. Values are in list format to allow for 
## Multiple pronunciations.
def getArpabet(wordLst):
    pronounceDict = {}
    CMUDict = cmudict.dict()

    for word in wordLst:
        unicodeWord = unicode(word) 

        try:
            # print CMUDict[unicodeWord]
            pronounceDict[word] = CMUDict[unicodeWord]

        except:

            print(unicodeWord + " not found in CMUDict")
            sys.exit()

    return pronounceDict


def getSyllabDict(ArpabetDict):
    syllabDict = {}

    for key in ArpabetDict:

        syllabDict[key] = []

        for pronunciation in ArpabetDict[key]:

            syllabification = getSyllabification(pronunciation)
            syllabDict[key].append(syllabification)
            
        # print(syllabDict[key])
    return syllabDict


def getSyllabification(pronunciation):
    ArpString = ""

    for phone in pronunciation:
        aPhone = phone.encode('ascii','ignore')

        if(len(aPhone) == 2):
            if(aPhone[1].isdigit()):
                aPhone = aPhone[:1]
                
        else:
            if(len(aPhone) == 3):
                if(aPhone[2].isdigit()):
                    aPhone = aPhone[:2]

        ArpString = ArpString + aPhone + " "

    ## ArpString ready for NIST
    finalSyllab = NIST.syllabify(ArpString)

    return finalSyllab


## dictionary format: word: [[syllab1],[syllab2]...]
## this function not passing. Need actual dictionary format to proceed.
def printDictToFile(Dict):
    outFile = open("./corpusFiles/topSyllabDict.txt",'w')

    for entry in Dict:

        #print(Dict[entry])
        outFile.write(str(entry))
        outFile.write(" ")
        NumOfSyllabs = len(Dict[entry])

        for i in range(0,NumOfSyllabs):

            print(str(Dict[entry][i]))
            outFile.write(" ")
            outFile.write(str(Dict[entry][i]))

        outFile.write("\n")

    outFile.close()
    return


def main():
    
    wordLst = []

    wordLst = getWords(wordLst)
    
    ArpabetDict = getArpabet(wordLst)

    syllabDict = getSyllabDict(ArpabetDict)
    
    # print NIST.syllabify("hh ae v ih ng")

    printDictToFile(syllabDict)


main()

@@ -921,38 +921,59 @@
 mat 
-to allow for %0A## Multiple
+with unicode phonemes%0A## only takes the first
  pron
+o
 unci
@@ -977,18 +977,44 @@
 nciation
-s.
+ for CMU when multiple exist
 %0Adef get
@@ -1172,23 +1172,37 @@
         
-# print
+pronounceDict%5Bword%5D =
  CMUDict
@@ -1214,16 +1214,19 @@
 odeWord%5D
+%5B0%5D
 %0A       
@@ -1222,32 +1222,40 @@
 %5B0%5D%0A            
+# print 
 pronounceDict%5Bwo
@@ -1257,39 +1257,16 @@
 ct%5Bword%5D
- = CMUDict%5BunicodeWord%5D
 %0A%0A      
@@ -1470,90 +1470,8 @@
 t:%0A%0A
-        syllabDict%5Bkey%5D = %5B%5D%0A%0A        for pronunciation in ArpabetDict%5Bkey%5D:%0A%0A    
     
@@ -1515,27 +1515,26 @@
 ion(
-pronunciation)%0A    
+ArpabetDict%5Bkey%5D)%0A
     
@@ -1552,24 +1552,19 @@
 ict%5Bkey%5D
-.append(
+ = 
 syllabif
@@ -1570,30 +1570,16 @@
 fication
-)%0A            
 %0A       
@@ -1600,24 +1600,25 @@
 bDict%5Bkey%5D)%0A
+%0A
     return s
@@ -1701,16 +1701,18 @@
 or phone
+me
  in pron
@@ -1740,16 +1740,18 @@
 hone
+me
  = phone
 .enc
@@ -1746,16 +1746,18 @@
  = phone
+me
 .encode(
@@ -1792,24 +1792,26 @@
 f(len(aPhone
+me
 ) == 2):%0A   
@@ -1828,16 +1828,18 @@
 f(aPhone
+me
 %5B1%5D.isdi
@@ -1860,32 +1860,34 @@
           aPhone
+me
  = aPhone%5B:1%5D%0A  
@@ -1879,16 +1879,18 @@
 = aPhone
+me
 %5B:1%5D%0A   
@@ -1942,16 +1942,18 @@
 n(aPhone
+me
 ) == 3):
@@ -1978,16 +1978,18 @@
 f(aPhone
+me
 %5B2%5D.isdi
@@ -2022,16 +2022,18 @@
   aPhone
+me
  = aPhon
@@ -2033,16 +2033,18 @@
 = aPhone
+me
 %5B:2%5D%0A%0A  
@@ -2079,16 +2079,18 @@
 + aPhone
+me
  + %22 %22%0A%0A
@@ -2969,28 +2969,31 @@
 fy(%22
-hh ae v ih ng
+s p eh sh ah l
 %22)%0A%0A   
+ #
  pri

limited CMU output to 1 pronunciation