<commit_before># coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals

from collections import defaultdict

from . import rhyme
from .syllables import count_syllables


class Rhymer(object):
    """
    finds pairs of rhymes in input
    our only basic worry is quality checking our rhymes?
    """
    def __init__(self, rhyme_count=2):
        super(Rhymer, self).__init__()
        self.endings = defaultdict(list)
        self.rhyme_count = rhyme_count

    def add_line(self, line):
        # get our end sound
        end_word = rhyme.rhyme_word_if_appropriate(line)

        if end_word:
            end_sound = rhyme.sound_for_word(end_word)

            if self.not_homophonic(line, end_sound):
                self.endings[end_sound].append(line)
                if len(self.endings[end_sound]) == self.rhyme_count:
                    to_return = tuple(self.endings[end_sound])
                    self.endings[end_sound] = list()
                    return to_return

            # rhyme_line = self.endings.get(end_sound)
            # if rhyme_line and rhyme.lines_rhyme(line, rhyme_line):

                # print('\n', line, rhyme, sep='\n')
            #     self.endings[end_sound] = None
            #     return (line, rhyme_line)
            # else:
            #     self.endings[end_sound] = line

    def not_homophonic(self, line, end_sound):
        for other_line in self.endings[end_sound]:
            if not rhyme.lines_rhyme(line, other_line):
                # print('homophones:\n%s\n%s' % (line, other_line))
                return False

        return True



    def debug(self):
        print(self.endings)



class Coupler(object):
    """finds rhyming couplets"""
    def __init__(self):
        super(Coupler, self).__init__()
        self.rhymers = defaultdict(Rhymer)

    def add_line(self, line):
        syllable_count = count_syllables(line)
        return self.rhymers[syllable_count].add_line(line)

    def generate_from_source(self, source):
        for line in source:
            couplet = self.add_line(line)
            if couplet:
                yield couplet


class Limericker(object):
    """finds limericks"""
    def __init__(self):
        super(Limericker, self).__init__()
        self.lines = defaultdict(list)
        self.nines = Rhymer(rhyme_count=3)
        self.sixes = Rhymer()


    def add_line(self, line):
        syllable_count = count_syllables(line)
        new_rhyme = None
        lines = None
        if syllable_count == 6:
            lines, new_rhyme = 6, self.sixes.add_line(line)
        elif syllable_count == 9:
            lines, new_rhyme = 9, self.nines.add_line(line)

        if not new_rhyme:
            return None

        self.lines[lines].append(new_rhyme)
        return self.check_for_art()


    def check_for_art(self):
        if (len(self.lines[9]) and len(self.lines[6]) and 
            not rhyme.lines_rhyme(self.lines[9][0][0], self.lines[6][0][0])):

            nines = self.lines[9].pop()
            sixes = self.lines[6].pop()

            poem = (nines[0], nines[1], sixes[0], sixes[1], nines[2])
            return poem

    def generate_from_source(self, source):
        for line in source:
            poem = self.add_line(line)
            if poem:
                yield poem

    def prettify(self, poem):<commit_msg>cleaned up limerick code<commit_after># coding: utf-8
from __future__ import print_function
from __future__ import unicode_literals

from collections import defaultdict

from . import rhyme
from .syllables import count_syllables


class Rhymer(object):
    """
    finds pairs of rhymes in input
    our only basic worry is quality checking our rhymes?
    """
    def __init__(self, rhyme_count=2):
        super(Rhymer, self).__init__()
        self.endings = defaultdict(list)
        self.rhyme_count = rhyme_count

    def add_line(self, line):
        # get our end sound
        end_word = rhyme.rhyme_word_if_appropriate(line)

        if end_word:
            end_sound = rhyme.sound_for_word(end_word)

            if self.not_homophonic(line, end_sound):
                self.endings[end_sound].append(line)
                if len(self.endings[end_sound]) == self.rhyme_count:
                    to_return = tuple(self.endings[end_sound])
                    self.endings[end_sound] = list()
                    return to_return

            # rhyme_line = self.endings.get(end_sound)
            # if rhyme_line and rhyme.lines_rhyme(line, rhyme_line):

                # print('\n', line, rhyme, sep='\n')
            #     self.endings[end_sound] = None
            #     return (line, rhyme_line)
            # else:
            #     self.endings[end_sound] = line

    def not_homophonic(self, line, end_sound):
        for other_line in self.endings[end_sound]:
            if not rhyme.lines_rhyme(line, other_line):
                # print('homophones:\n%s\n%s' % (line, other_line))
                return False

        return True



    def debug(self):
        print(self.endings)



class Coupler(object):
    """finds rhyming couplets"""
    def __init__(self):
        super(Coupler, self).__init__()
        self.rhymers = defaultdict(Rhymer)

    def add_line(self, line):
        syllable_count = count_syllables(line)
        return self.rhymers[syllable_count].add_line(line)

    def generate_from_source(self, source):
        for line in source:
            couplet = self.add_line(line)
            if couplet:
                yield couplet


class Limericker(object):
    """finds limericks"""
    def __init__(self):
        super(Limericker, self).__init__()
        self.lines = defaultdict(list)
        self.rhymers = {9: Rhymer(rhyme_count=3), 6: Rhymer()} 

    def add_line(self, line):
        syllable_count = count_syllables(line)
        new_rhyme = None
        if syllable_count == 6 or syllable_count == 9:
            new_rhyme = self.rhymers[syllable_count].add_line(line)

        if new_rhyme:
            self.lines[syllable_count].append(new_rhyme)
            return self.check_for_art()


    def check_for_art(self):
        if (len(self.lines[9]) and len(self.lines[6]) and 
            not rhyme.lines_rhyme(self.lines[9][0][0], self.lines[6][0][0])):

            nines = self.lines[9].pop()
            sixes = self.lines[6].pop()

            poem = (nines[0], nines[1], sixes[0], sixes[1], nines[2])
            return poem

    def generate_from_source(self, source):
        for line in source:
            poem = self.add_line(line)
            if poem:
                yield poem

    def prettify(self, poem):
{'fraction_non_alphanumeric': 0.1041885602762348, 'fraction_numerical': 0.006605614772556673, 'mean_word_length': 2.146905999055267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '479374', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2106, 'n_words': 450}