@grammer_man who the fuck is this nigga and why u comin at me like that #Hoeassnigga


Posted: 2012-01-09 20:06   |  More posts about code computers funny idiots internet oddities

Had a spare hour last Thursday and decided to write a little twitter bot. There he is above. His name is Grammer_Man and he corrects other twitter users' misspellings, using data scraped from these Wikipedia pages.

Responses have been pouring in already, some agitated, some confused, but most positive -- which was a pleasant surprise. In any event, the minimal amount of effort in coding has paid off many times over in entertainment.

You can see who's responding at the moment by searching for @grammer_man, and also by checking his list of favourites.

Here is the (somewhat slapdash) code that powers our fearless spelling Nazi:

grabber.py

This module grabs the spelling data from Wikipedia.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pickle

import requests
from BeautifulSoup import BeautifulSoup

def grab(letter):
    '''
    Grabs spellings from wikipedia
    '''
    url = 'http://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/%s' % letter
    html = requests.get(url).content
    soup = BeautifulSoup(html)
    bullets = soup.findAll('li')
    retval = {}
    for bullet in bullets:
        if 'plainlinks' in repr(bullet):
            values = bullet.text.split('(')
            if len(values) == 2:
                retval[values[0]] = values[1][:-1] # shave off the ) at end
    return retval

def get_spellings():
    '''
    Returns a dictionary of {false: correct} spellings
    '''
    if not os.path.exists('words.pkl'):
        retval = {}
        for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
            print 'Getting typos - %s' % c
            retval.update(grab(c))
        print 'Dumping...'
        f = open('words.pkl', 'w')
        pickle.dump(retval, f)
        f.close()
        return retval
    else:
        f = open('words.pkl', 'r')
        retval = pickle.load(f)
        f.close()
        return retval

if __name__ == '__main__':
    get_spellings()

bot.py

The bot. Selects misspellings at random, searches for them, responds to them, while also taking breaks between tweets and longer breaks every few hours.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import random
import time
import pickle

import twitter

from grabber import get_spellings

API = twitter.Api()

MESSAGES = u'''
Hey $USERNAME, didn't you mean $CORRECT there?
#
# All messages stored in here, one per line.
#
'''.split('\n')

def compose_message(twitter_post, mistake, correct):
    '''
    Choose a message from MESSAGES at random, substitute fields to personalise it and 
    check if it exceeds the twitter message limit. Try this 100 times before failing.
    '''
    retries = 0
    while retries < 100:
        message = MESSAGES[random.randint(0, len(MESSAGES) - 1)]
        message = message.replace('$USERNAME', '@%s' % twitter_post.user.screen_name)
        message = message.replace('$MISTAKE', '"%s"' % mistake).replace('$CORRECT', '"%s"' % correct)
        if message and len(message) < 141:
            return message
    return None

def correct_spelling(twitter_post, mistake, correct):
    '''
    Correct someone's spelling in a twitter_post
    '''
    print u'Correcting @%s for using %s...' %(twitter_post.user.screen_name, 
                                            mistake)
    message = compose_message(twitter_post, mistake, correct)
    if not message:
        print u'All messages were too long... Aborting...'
        return False
    else:
        failures = 0
        try:
            API.PostUpdate(message, in_reply_to_status_id=twitter_post.id)
        except Exception, e:
            print 'Failed to submit tweet (%s).'
            return False
        return True

def search(word):
    '''
    Search twitter for uses of a word, return one if it's been used recently.
    Otherwise return None.

    TODO: Add time awareness.
    '''
    print 'Searching for uses of %s...' % word
    results = API.GetSearch(word)
    if results:
        for result in results:
            if not check_if_done(result.id) and\
                not result.user.screen_name == 'grammer_man' and word in result.text:
                return result
    return None

def check_if_done(id):
    '''
    Checks if a tweet has already been responded to
    '''
    if os.path.exists('done.pkl'):
        f = open('done.pkl', 'r')
        done = pickle.load(f)
        f.close()
        if id in done:
            return True
    return False

def update_done(id):
    '''
    Updates a list of tweets that've been replied to
    '''
    if os.path.exists('done.pkl'):
        f = open('done.pkl', 'r')
        done = pickle.load(f)
        f.close()
    else:
        done = []

    done.append(id)

    f = open('done.pkl', 'w')
    pickle.dump(done, f)
    f.close()

def main():
    '''
    Main program flow
    '''
    words = get_spellings()
    counter = 0 
    while True:
        word = random.choice(words.keys())
        post = search(word)
        if counter > 100:
            rand_time = random.randint(120*60, 240*60)
            print 'Done %s tweets, sleeping for %s minutes' % (counter, rand_time/60)
            time.sleep(rand_time)
            counter = 0
        # TODO: PROPERLY PRUNE THE MISTAKES/CORRECTIONS FROM WIKIPEDIA AND REMOVE THIS:
        if not u',' in word + words[word] and not u';' in word + words[word]:
            if post:
                result = correct_spelling(post, word, words[word])
                if result:
                    counter += 1
                    print '#%s Done' % counter
                    update_done(post.id)
                    time.sleep(random.randint(300,500))

if __name__ == '__main__':
    main()

Grammer_Man uses the following libraries:

Comments


The Chaos


Posted: 2012-01-06 15:02   |  More posts about art funny linguistics poetry words

A poem by Gerard Nolst Trenité demonstrating the abundant irregularities of English spelling and pronunciation. More info here. 

Read more...

Comments


Cowboys and Anthrax


Posted: 2011-12-12 21:56   |  More posts about film funny politics

Great stuff here from the Bad Lip Reading Youtube channel. Check it out for plenty more.

Thanks to Hugh for bringing this to my attention.

Comments


Comb-me quickly; don't put me so much pomatum. What news tell me? all hairs dresser are newsmonger.


Posted: 2011-04-17 21:33   |  More posts about funny oddities words

The title of this blogpost is taken from English As She Is Spoke, a 19th century Portugese-English phrase book which I've spent the last hour reading on the train. It's living up to all expectations. You can enjoy the entire book here.

Comments


Cáca Milis


Posted: 2011-02-12 18:32   |  More posts about art film funny ireland irish language short film

A short Irish-language film starring Brendan Gleeson, not entirely unlike the wonderful Six Shooter.

Comments


But what does it mean?


Posted: 2010-10-22 15:15   |  More posts about art funny internet oddities photography

500 / 200.

From a collective of people including the man behind King Lud's Revenge.

Comments


Welcome to Reykjavík


Posted: 2010-08-16 22:25   |  More posts about art funny oddities politics words

An eccentric little piece from Reykjavík's Mayor. Click to enlarge.

Thanks to Hugh for bringing this to my attention.

Comments


An anecdote


Posted: 2009-09-30 21:28   |  More posts about censorship football funny oddities politics religion sports

A few weeks ago, I was asked to act as a proxy and present a paper on language evolution at a conference on artificial intelligence and life taking place in Budapest. The presentation went well, considering I'd only had a week or so to read up on what is an enormous subject I'd never studied before.

Later on, in the evening, I had been walking about the town, looking for a suitable place to have dinner when I came across an Irish pub which I decided to have a few drinks in later that night, after having eaten. A match was on that night between Manchester Utd and Besiktas for which they had the projector screen out and all. Upon entering, I attempted to take a seat at the bar, since I had absolutely no interest in the match, only to be chaperoned to the pub audience and told I must be seated with everyone else, in front of the projector screen.

It quickly became apparant that I was the only Irishman in the building: the staff were all Hungarian, there was a group of Americans closest to the screen, then directly in front of me a group of about four Englishmen, and to my left a group of about eight Turks, men and women, who were occasionally chatting to three Danes seated beside them, having dinner. Those Turks immediately to my left were rather friendly and chatty, and after a while we had exchanged pleasantries and stories explaining why and how we had wound up in an Irish pub in Budapest of all places.

One hour and many beers later, and not a goal had been scored. I grew more and more impatient, and the Turks (for whom this game seemed to mean an awful lot) grew more and more raucous. Then, out of nowhere, a shot on-target rebounded off the goal-posts. As it seems, the drink had affected my prior apathy towards the whole event, and I let an annoyed roar of "JESUS!" out of me. One of the Turks turned to me and said with a smile, "Don't you mean Mohammad?" I responded, "Ah yeah, he's pretty good too, just don't draw any funny pictures of him, ye?"

The Danes exploded in laughter.

The Turks went completely silent, staring straight ahead at the projector screen.

Comments


OMG DID YOU HEAR MJ DIED


Posted: 2009-06-30 00:34   |  More posts about funny media news

Michael Jackson died and Charlie Brooker wrote a great blog post about the death and how the media's handled it:

I was at Glastonbury when Jacko died. That's not a factual statement, but a T-shirt slogan. The day after his death, souvenir tops with "I was at Glasto 09 when Jacko died" printed on them were already on sale around the site. In fact, when Jacko died, I was at home playing Grand Theft Auto: Chinatown Wars on a Nintendo DSi. I am 38 years old.

...

The next day he was still dead, but somehow deader than the day before. He was all over the radio and papers. The TV had clips of Thriller on heavy rotation, which seemed a tad inappropriate, what with him playing a decomposing corpse in it. If Bruce Willis died falling from a skyscraper, I doubt they'd illustrate his life story by repeatedly showing that bit from Die Hard where he ties a firehose round his waist and jumps off the building.

Across all the networks, a million talking heads shared their thoughts and feelings on his death. They had rung everyone in the universe and invited them on the show. On This Morning, a Coronation Street actor revealed he had once had tickets for a Michael Jackson concert but couldn't go because of the traffic. It was a sad day indeed. At 3pm, his death was still "BREAKING NEWS" according to Sky, which has to be some kind of record. Even 9/11 didn't "break" that long.

He ends with a well-expressed sentiment I share completely:

But the news is not the place to "celebrate" Jackson's music. The Glastonbury stage, the pub, the club, the office stereo, the arts documentary: that's the place. The news should report his death, then piss off out of the way, leaving people to moonwalk and raise a toast in peace.

Comments


The idea behind reCAPTCHA


Posted: 2009-06-13 19:50   |  More posts about computer science computers funny internet words

An entertaining talk from Luis von Ahn, the guy behind CAPTCHAs, about the reinvention of the idea in a way to benefit mankind. Some pretty incredible statistics throughout, especially towards the end.

Comments