def is_valid(self, word, meter_valid, new_syl, num_lines, line_end): """Determines if the word is valid.""" rhyme_valid = True if (num_lines % 4) in [2, 3] and new_syl == SYLLABLES_PER_LINE: rhyme_valid = rhymes(word, line_end[num_lines - 2]) elif num_lines == COUPLET_LINE and new_syl == SYLLABLES_PER_LINE: rhyme_valid = rhymes(word, line_end[num_lines - 1]) return (meter_valid and rhyme_valid and new_syl <= SYLLABLES_PER_LINE)
def set_rhymes(self): self.rhymes = {} positions = self.positions() for index, (position_a, word_a) in enumerate(positions): rhymes_a = utils.rhymes(word_a) print(self.id, position_a, word_a, index, len(positions)) for position_b, word_b in positions[(index + 1):]: if word_b in rhymes_a: key = '%i,%i' % position_a value = '%i,%i' % position_b try: self.rhymes[key].append(value) except KeyError: self.rhymes[key] = [value] self.document['rhymes'] = self.rhymes self.save()
def set_rhymes(self): rhymes = {} positions = self.positions() for index, (position_a, word_a) in enumerate(positions): rhymes_a = utils.rhymes(word_a) for position_b, word_b in positions[(index+1):]: if word_b in rhymes_a: print word_a, word_b, position_a, position_b key = '%i,%i' % position_a value = '%i,%i' % position_b try: rhymes[key].append(value) except KeyError: rhymes[key] = [value] self.document['rhymes'] = rhymes self.save()
for document in collection.find({"_id": poem_ids.STATS}).limit(1): print(document['_id'], file=sys.stderr) n_sentences = len(document['analyzed']) row_list = [] for line_number, sentence in enumerate(document['analyzed'][1:-1], 1): line_number_before = line_number - 1 sentence_before = document['analyzed'][line_number_before] line_number_after = line_number + 1 sentence_after = document['analyzed'][line_number_after] rhyme_words = set() word_list = list(set(w['closest'] for w in sentence_before + sentence + sentence_after)) for index, word_a in enumerate(word_list): rhymes_a = set(utils.rhymes(word_a)) for word_b in word_list[(index + 1):]: if word_b in rhymes_a: rhyme_words.add(word_a) rhyme_words.add(word_b) if line_number_before == 0: row_list.append(make_row(sentence_before, rhyme_words)) row_list.append(make_row(sentence, rhyme_words)) if line_number_after == (n_sentences - 1): row_list.append(make_row(sentence_after, rhyme_words)) diagram = '<div class="diagram container">%s</div>' % ''.join(row_list) with open('rhymes/%s.html' % document['_id'], 'w') as outfile: