title: splitting words by syllable with CMU Pronunciation Dictionary, NLTK, and Python3
body: <p>I am working on a natural language processing project and am stuck on splitting words into syllables (using <code>nltk</code> and <code>cmudict.dict()</code> in <code>python 3</code>).</p>
  
  <p>I currently count syllables by looking a word in my corpus up in the CMU Pronunciation Dictionary and counting the number of stresses in its list of phonemes. This appears to work pretty well.</p>
  
  <p>What I am stuck on is how to use this information to split the accompanying grapheme after counting, as I do not understand how to either translate the phonemes back to the graphemes (seems error prone) or use the list of phonemes to somehow split the grapheme.</p>
  
  <p>Here is the function I wrote to do this (word tokenization happens elsewhere):</p>
  
  <pre><code>def getSyllables(self):
      pronunciation = cmudict.dict() # get the pronunciation dictionary
      syllableThreshold = 1 # we dont care about 1 syllable words right now
      for word in self.tokens:
          for grapheme, phonemes in pronunciation.items():
              if grapheme == word.lower(): # all graphemes are lowercase, we have to word.lower() to match
                  syllableCounter = 0
                  for x in phonemes[0]:
                      for y in x:
                          if y[-1].isdigit(): # an item ending in a number is a stress (syllable)
                              syllableCounter += 1
                  if syllableCounter &gt; syllableThreshold:
                      output = ' '.join([word, "=", str(syllableCounter)])
                      print(output)
                      print(phonemes)
                  else:
                      print(word)
  </code></pre>
  
  <p>Just as an example, my current output is:</p>
  
  <pre><code>Once
  an
  angry = 2
  [['AE1', 'NG', 'G', 'R', 'IY0']]
  man
  </code></pre>
  
  <p>How can I split the word <code>angry</code>, for example, into <code>an - gry</code>?</p>