def extract_line(words, desired_syllable_count, debug=False): """ attempt to group words from the start of word list to make the desired number of syllables (i.e. get a five syllable line for a haiku) return (line, remaining_words) where line will be an empty string if it fails to match the syllable count """ line = "" remaining = words[:] syllable_count = 0 while (syllable_count < desired_syllable_count) and (len(remaining) > 0): # count syllables in word word = remaining[0] word_syllables = syllables.count_text_syllables(word, debug) syllable_count += word_syllables # if still in range, add word to line and remove word from remaining if syllable_count <= desired_syllable_count: line = "{0} {1}".format(line, word) remaining = remaining[1:] # if the syllable count is good, return the line if syllable_count == desired_syllable_count: return line.strip(), remaining # failed, return empty string return "", remaining
ascii_text = ascii_text.replace(":)", "") ascii_text = ascii_text.replace(";)", "") ascii_text = ascii_text.replace(":P", "") ascii_text = ascii_text.replace(";P", "") ascii_text = ascii_text.replace(":D", "") ascii_text = ascii_text.replace(";D", "") ascii_text = ascii_text.replace("(:", "") # replace "OP" with "O.P." (espeak reads as "op" as in "hop") ascii_text = ascii_text.replace(" OP", " O.P.") # replace "plz" with "please" (espeak reads "P.L.Z") ascii_text = ascii_text.replace(" plz", " please") # broad phase test - see if there are 17 syllables syllable_count = syllables.count_text_syllables(ascii_text, debug) if syllable_count != 17: return "" # all tests passed, return text return ascii_text def get_haiku(text, debug=False): """ return empty string if text is not a haiku else return text formatted as a haiku """ # check suitability