Writing stories with code, part 2: conditional leads from trends

See part one here. Get the code, such as it is, here.

When we last left off, we had a script that would loop through a list of data and write a news lead out of it. All that the script did was look at two numbers and decide if the crime rate went up or down and then wrote an appropriate sentence. Something like this:

Lexington police reported more violent crime in 2010 compared to 2009, according to federal statistics.

But, sometimes, just one year isn’t enough. Sometimes a city gets on a roll, crime goes down for several years in a row, and that’s noteworthy enough to change the lead. So, lets do that. How?

Well, not to continue a trend here, but’s it’s really simple. 

Here’s the code:

    # determine the duration of the trend

    if city[4] > city[3] > city[2]:
        trend_length_clause = ", the second year in a row crime has increased"
    elif city[4] < city[3] < city[2]:
        trend_length_clause = ", the second year in a row crime has declined"
    else:
        trend_length_clause = ""

What does that say? It’s simple greater than, less than logic. If 2010 (or city[4] in our loop of data) is greater than 2009, which is greater than 2008, then crime has gone up two straight years. If you flip the sign, you get that crime has declined two straight years. So, if those conditions are true, then lets set a variable called trend_length_clause to some words that work in our lead. Then, add that to our lead generating code:

    lead = "%s police reported %s violent crime in 2010 compared to 2009%s, according to federal statistics." % (clean_city, direction, trend_length_clause)

Our lead now says insert the city where the first %s is, the direction of the trend where the next %s is and finally jam that clause in. Since we set it to blank if crime didn’t go up or down for two consecutive years, jamming it in there will do nothing if those trends don’t exist. When you run the script, now you see leads like this:

Lincoln police reported more violent crime in 2010 compared to 2009, according to federal statistics.

Norfolk police reported more violent crime in 2010 compared to 2009, the second year in a row crime has increased, according to federal statistics.

That’s a little better. Still not award winning, but at least it varies it up based on the news. 

Let’s call it a lead for now. We need a second paragraph, one that starts putting some numbers to this trend. So that’s what we’ll do in the next post. We’ll write a second paragraph that does some percent change math and spells out the data a little further.