Пример #1
0
    def get_lines(sylla_count):
        ''' Returns all tweets with the given number of syllables '''

        q = text('''SELECT * FROM tweet LEFT JOIN tweets ON tweet.id = tweets.tweet_id 
            WHERE tweets.tweet_id IS NULL AND tweet.hashtag = :h AND tweet.syllables = :s''')
        tweets = engine.connect().execute(q, h=hashtag, s=sylla_count).fetchall()
        print "Haiku: query", q, ": ", str(len(tweets))
        print "hashtag:", hashtag
        return tweets
Пример #2
0
def couplet(hashtag):
    ''' Generates a Couplet poem given a list of (text, syllable count, rhyme) tuples '''

    q = text(
        '''SELECT * FROM tweet LEFT JOIN tweets ON tweet.id = tweets.tweet_id 
            WHERE tweets.tweet_id IS NULL AND tweet.hashtag = :h''')
    tweets = engine.connect().execute(q, h=hashtag).fetchall()
    corpus = [{'line':t.text, 'syllables':t.syllables, 'phone':t.phone, 'id':t.id, 'last_word':t.last_word} \
                for t in tweets]

    def get_lines(phone, word, syllables):
        return [
            line for line in corpus if line != {} and line['phone'] == phone
            and word.lower() != line['last_word'].lower()
            and abs(line['syllables'] - syllables) <= 1
        ]

    def get_lines2():
        return [
            line for line in corpus if line != {} and line['phone'] != None and
            len(get_lines(line['phone'], line['last_word'],
                          line['syllables'])) > 0
        ]

    try:
        # Get random sample from 5 or 7 syllable tweets
        first = sample(get_lines2(), 1)
        phone = first[0]
        phone = phone['phone']
        second = first
        word = first[0]['last_word']
        syl = first[0]['syllables']
        if len(get_lines(phone, word, syl)) > 0:
            while word == second[0]['last_word']:
                second = sample(get_lines(phone, word, syl), 1)
        else:
            raise ValueError(
                'Could not construct couplet - not enough tweets found')
    except ValueError as e:
        raise ValueError(
            'Could not construct couplet - not enough tweets found')
    first = first[0]
    second = second[0]

    tweets = [first, second]
    tweets = [db.session.query(Tweet).get(t['id']) for t in tweets]

    poem = Poem(tweets, hashtag, 'couplet')

    return poem
Пример #3
0
def couplet(hashtag):
    ''' Generates a Couplet poem given a list of (text, syllable count, rhyme) tuples ''' 

    q = text('''SELECT * FROM tweet LEFT JOIN tweets ON tweet.id = tweets.tweet_id 
            WHERE tweets.tweet_id IS NULL AND tweet.hashtag = :h''')
    tweets = engine.connect().execute(q, h=hashtag).fetchall()
    corpus = [{'line':t.text, 'syllables':t.syllables, 'phone':t.phone, 'id':t.id, 'last_word':t.last_word} \
                for t in tweets]

    def get_lines(phone, word, syllables):
        return [line for line in corpus if line != {} and line['phone'] == phone and word.lower() != line['last_word'].lower() and abs(line['syllables'] - syllables)<=1]
    def get_lines2():
        return [line for line in corpus if line != {} and line['phone'] != None and len(get_lines(line['phone'], line['last_word'], line['syllables']))>0]  
        
    try:
        # Get random sample from 5 or 7 syllable tweets
        first = sample(get_lines2(), 1)
        phone = first[0]
        phone = phone['phone']
        second = first
        word = first[0]['last_word']
        syl = first[0]['syllables']
        if len(get_lines(phone, word, syl)) > 0:
            while word==second[0]['last_word']:
                second = sample(get_lines(phone, word, syl), 1)
        else:
            raise ValueError('Could not construct couplet - not enough tweets found')
    except ValueError as e:
        raise ValueError('Could not construct couplet - not enough tweets found') 
    first = first[0]
    second = second[0]
    
    tweets = [first, second]
    tweets = [db.session.query(Tweet).get(t['id']) for t in tweets]
    
    poem = Poem(tweets, hashtag, 'couplet')
    
    return poem
Пример #4
0
def limerick(hashtag):
    ''' Generates a limerick given a dictionary of line/syllables/phone '''

    q = text(
        '''SELECT * FROM tweet LEFT JOIN tweets ON tweet.id = tweets.tweet_id 
            WHERE tweets.tweet_id IS NULL AND tweet.hashtag = :h''')
    tweets = engine.connect().execute(q, h=hashtag).fetchall()
    corpus = [{'line':t.text, 'syllables':t.syllables, 'phone':t.phone, 'id':t.id, 'last_word':t.last_word} \
                for t in tweets]
    print "-*" * 10
    print len(corpus)
    print "-*" * 10

    def get_lines(sylla_min, sylla_max, n):
        ''' Returns dic of at least n rhyming tweets within the given syllable range
            ex. {'ha1':['some tweet text', 'some other text']} '''

        lines = [
            line for line in corpus if line['syllables'] >= sylla_min
            and line['syllables'] <= sylla_max
        ]
        dic = {}
        # dic is a dictionary of phone strings to line dictionaries
        for line in lines:
            phone = line['phone']
            if phone is None:
                continue
            elif phone in dic:
                # Avoid repeated last words
                if line['last_word'] not in [
                        l['last_word'] for l in dic[phone]
                ]:
                    dic[phone].append(line)
            else:
                dic[phone] = [line]
        # Pick the least syllabically variant n lines
        return_lines = ''
        least_variance = 999
        for key in dic:
            lines = dic[key]
            if len(lines) >= n:
                # Check variance, we want lines with similar syllable counts
                variance = max(lines, key=lambda line:line['syllables'])['syllables'] - \
                           min(lines, key=lambda line:line['syllables'])['syllables']

                if len(return_lines) == 0 or least_variance > variance:
                    return_lines = [line for line in lines]
                    least_variance = variance
                    logging.info("best lines: ")
                    logging.info(return_lines)
        if len(return_lines) != 0:
            return return_lines
        else:
            raise ValueError

    '''
    Limerick format is as follows:
    6-12A
    6-12A
    3-6B
    3-6B
    6-10A
    '''
    try:
        a = get_lines(6, 12, 3)  # Get 3 6-12 syllable lines that rhyme
        b = get_lines(3, 7, 2)  # Get 2 3-6 syllable lines that rhyme
    except ValueError as e:
        raise ValueError('Could not construct limerick - not enough tweets')

    tweets = [a[0], a[1], b[0], b[1], a[2]]
    tweets = [db.session.query(Tweet).get(t['id']) for t in tweets]

    poem = Poem(tweets, hashtag, 'limerick')

    return poem
Пример #5
0
def limerick(hashtag):
    ''' Generates a limerick given a dictionary of line/syllables/phone ''' 

    q = text('''SELECT * FROM tweet LEFT JOIN tweets ON tweet.id = tweets.tweet_id 
            WHERE tweets.tweet_id IS NULL AND tweet.hashtag = :h''')
    tweets = engine.connect().execute(q, h=hashtag).fetchall()
    corpus = [{'line':t.text, 'syllables':t.syllables, 'phone':t.phone, 'id':t.id, 'last_word':t.last_word} \
                for t in tweets]
    print "-*"*10
    print len(corpus)
    print "-*"*10

    def get_lines(sylla_min, sylla_max, n):
        ''' Returns dic of at least n rhyming tweets within the given syllable range
            ex. {'ha1':['some tweet text', 'some other text']} '''

        lines = [line for line in corpus if line['syllables'] >= sylla_min and line['syllables'] <= sylla_max] 
        dic = {}
        # dic is a dictionary of phone strings to line dictionaries
        for line in lines:
            phone = line['phone']
            if phone is None:
                continue
            elif phone in dic:
                # Avoid repeated last words
                if line['last_word'] not in [l['last_word'] for l in dic[phone]]:
                    dic[phone].append(line)
            else:
                dic[phone] = [line]
        # Pick the least syllabically variant n lines
        return_lines = ''
        least_variance = 999
        for key in dic:
            lines = dic[key]
            if len(lines) >= n:
                # Check variance, we want lines with similar syllable counts
                variance = max(lines, key=lambda line:line['syllables'])['syllables'] - \
                           min(lines, key=lambda line:line['syllables'])['syllables']

                if len(return_lines) == 0 or least_variance > variance:
                    return_lines = [line for line in lines]
                    least_variance = variance
                    logging.info("best lines: ")
                    logging.info(return_lines)
        if len(return_lines) != 0:
            return return_lines
        else:
            raise ValueError

    '''
    Limerick format is as follows:
    6-12A
    6-12A
    3-6B
    3-6B
    6-10A
    '''
    try:
        a = get_lines(6, 12, 3) # Get 3 6-12 syllable lines that rhyme
        b = get_lines(3, 7, 2)  # Get 2 3-6 syllable lines that rhyme
    except ValueError as e:
        raise ValueError('Could not construct limerick - not enough tweets') 
    
    tweets = [a[0], a[1], b[0], b[1], a[2]]
    tweets = [db.session.query(Tweet).get(t['id']) for t in tweets]
    
    poem = Poem(tweets, hashtag, 'limerick')
    
    return poem