SEO Egghead by Jaimie Sirovich: A blog about SEO, written for nerds, by a nerd.

Choose a Topic:

» Suggest a topic or buzz to cover; if I write about it, you'll get credit with a link in the post!

Mon
11
Sep '06

Using Proper Grammar Programmatically: Part I

One of the things that irks me most about programmers getting involved with marketing is that most of them just don't know grammar.  SEO Black Hat mentioned "Google are," I mentioned "then vs. than," and now I'll mention one more thing -- "a vs. an."  This one is a little different.  Most people know this rule intuitively.  But programmers frequently get lazy and do not look programmatically at the following word to decide which indefinite article to use.  Being a programmer makes me slightly more forgiving, but this still looks bad.  After I release this code, there are no more excuses:

<?

function aOrAn($next_word)
{
    
$_an = array('hour''honest''heir''heirloom');
    
$_a = array('use''useless''user');
    
$_vowels = array('a','e','i','o','u');
    
    
$_endings = array('ly''ness''less''lessly''ing''ally''ially');
    
$_endings_regex implode('|'$_endings);
    
    
$tmp preg_match('#(.*?)(-| |$)#'$next_word$captures);
    
$the_word = trim($captures[1]);
    
//$the_word = Format::trimString(Utils::pregGet('#(.*?)(-| |$)#', $next_word, 1));
    
    
$_an_regex implode('|'$_an);
    if (
preg_match("#($_an_regex)($_endings_regex)#i"$the_word)) {
        return 
'an';
    }
    
    
$_a_regex implode('|'$_a);
    if (
preg_match("#($_a_regex)($_endings_regex)#i"$the_word)) {
        return 
'a';
    }
    
    if (
in_array(strtolower($the_word{0}), $_vowels)) {
        return 
'an';    
    }
    
    return 
'a';
}

?>

This code may not be bulletproof, but it nails most of the major rules and exceptions.  So if I see one more lazy PHP programmer making this stupid mistake, I'm just closing the window, OK? :) 

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Furl
  • Reddit
  E-Mail This Post/Page

2 Responses to “Using Proper Grammar Programmatically: Part I”

  1. Gilbert Says:

    That is great stuff. ;)
    --G

  2. Adrian Says:

    Some of the a/an rules are a bit more complex and can't be embedded in code because they depend on how you pronounce words. For example its "a hotel" and "an 'otel" if you drop your aitches.

    Btw, can you provide code to help programmers with the its/it's problems too?

    Cheers,

    Adrian

Leave a Reply