def syllabify(language, word):
    """Syllabifies the word, given a language configuration loaded with loadLanguage.
	   word is either a string of phonemes from the CMU pronouncing dictionary set
	   (with optional stress numbers after vowels), or a Python list of phonemes,
	   e.g. "B AE1 T" or ["B", "AE1", "T"]"""
    if type(word) == str:
        word = word.split()
    syllables = []
    internuclei = []
    for phoneme in word:
        phoneme = phoneme.strip()
        if phoneme == '':
            continue
        stress = None
        if phoneme[-1].isdigit():
            stress = int(phoneme[-1])
            phoneme = phoneme[0:-1]
        if phoneme in language['vowels']:
            coda = None
            onset = None
            if '.' in internuclei:
                period = internuclei.index('.')
                coda = internuclei[:period]
                onset = internuclei[period + 1:]
            else:
                for split in range(0, len(internuclei) + 1):
                    coda = internuclei[:split]
                    onset = internuclei[split:]
                    if ' '.join(onset) in language['onsets'] or len(syllables
                        ) == 0 or len(onset) == 0:
                        break
            if len(syllables) > 0:
                syllables[-1][3].extend(coda)
            syllables.append((stress, onset, [phoneme], []))
            internuclei = []
        elif not phoneme in language['consonants'] and phoneme != '.':
            raise ValueError('Invalid phoneme: ' + phoneme)
        else:
            internuclei.append(phoneme)
    if len(internuclei) > 0:
        if len(syllables) == 0:
            syllables.append((None, internuclei, [], []))
        else:
            syllables[-1][3].extend(internuclei)
    return syllables
