
import string
import gnoetics
import syllables

class Word:

    def __init__(self, word_str):
        self.__word  = word_str
        self.__glue = 0
        if self.__word[0] == "|":
            self.__glue = 1
            self.__word = self.__word[1:]
        self.__syllables = -1
        self.__decomp = gnoetics.dictionary_get_word(self.__word)
            
    def to_string(self, prefix=""):
        word_str = self.__word
        if word_str[0] == "<":
            word_str = ""
        pad = (prefix and prefix[-1] != "\n" and word_str and not self.__glue and " ") or ""
        return prefix + pad + word_str

    def syllables(self):
        
        if self.__syllables <= 0:
            if self.__word[0] == "<":
                self.__syllables = 0
            elif self.__decomp:
                self.__syllables = syllables.count_decomp(self.__decomp)
            else:
                word_str = self.__word
                for c in "!@#$%^&*()-_=+[{]}\\|;:\'\",./<>?":
                    word_str = word_str.replace(c, " ")
                self.__syllables = 0
                for w in word_str.split():
                    decomp = gnoetics.dictionary_get_word(w)
                    if decomp:
                        syl = syllables.count_decomp(decomp)
                    else:
                        syl = syllables.count_word_fallback(w)
                    self.__syllables += syl
                
        return self.__syllables

        

__words = {}
def get_word(word_str):
    w = __words.get(word_str)
    if not w:
        __words[word_str] = w = Word(word_str)
    return w
        
        
