28 September 2009

All that is the case

I'm working on a short piece of code to capitalize all the initials in a given sentence (headlines for news stories from a wire service, to be specific), in order to match house style. At first, I was a little surprised that I couldn't find a Java class to do most of the work for me. But once I waded into the actual sentences that I had to process, with their variations and exceptions, I came to the conclusion that some degree of RYO was called for. Here's my current draft:



import java.util.regex.*;

* * *

private String capitalizeAllInitials(String text)
{
//capitalize the first letter of every word in the passed text
//(including little words like "a," "the," "and," "to")
//also capitalize words in quoted and hyphenated phrases

//NOTES: The pattern requires a leading space; I have found that
//ingested stories already capitalize the first word of the title.

Pattern p = Pattern.compile("(-|( (`|\\\"|\\\')?))([a-z])");
Matcher m = p.matcher(text);

StringBuffer sb = new StringBuffer();
while (m.find())
{
m.appendReplacement(sb, m.group(1) + m.group(4).toUpperCase());
}
m.appendTail(sb);

return sb.toString();
}



As the comments note, this code will handle ordinary words (The quick brown fox jumps over the lazy dog becomes The Quick Brown Fox Jumps Over The Lazy Dog), hyphenated phrases (Senator proposes pay-as-you-go plan becomes Senator Proposes Pay-As-You-Go Plan), and quoted phrases (Accused confesses, "we did it" becomes Accused Confesses, "We Did It") with single, double, or backquotes. The code assumes that the first word is already capitalized, so if that's not the case with you, you would need to add a ^ to the regular expression.

This code also doesn't handle the common forms of title casing, whereby articles, prepositions, and other small words are not capitalized. Also, this code capitalizes proper names and trademarks indiscriminately.

20 September 2009

Respect the pomodoro

With Patricia R. Olsen, Jim Remsik writes a good first-person account of pair programming as it is practiced at Hashrocket:
When senior and junior developers are paired, junior programmers might feel intimidated. If this happens, the junior programmer might be asked to start as the driver, which may encourage the senior person to become a better teacher. It’s also a way to bring junior programmers up to speed quickly, because they benefit from the more senior employee’s knowledge.

15 September 2009

We used to call that a dirt road

Via RISKS Digest: complaining of poor bandwidth through Telekom (South Afica's leading ISP), an information technology company strapped a data card to the leg of a carrier pigeon, sent it flying 80 kilometers, and exceeded internet throughput by a factor of 50.

27 August 2009

Eval of jQuery data grid plugins

[Adapted from a wiki entry I made for my current project:]

Starting from a brief review article, I looked at five tools:



Flexigrid and jTPS have very little documentation and have not been updated for nearly a year, and so I did not try to get these tools working. I downloaded jqGrid, Ingrid, and FireScope Grid and assembled test harnesses for all three.

Of these three, all use Ajax to populate the grid. All of them accomplish sorting by an Ajax call back to the server, passing a parm to specify the sort column and direction. So we would accomplish sorting of dates (or any other data types with unusual sort requirements) with server-side logic.

All three tools provide paging navigators, but all require some paging logic to be performed on the server. I didn't fully eval this area, because I used a static file of data to populate the grid.

Ingrid and FireScope Grid only support HTML data. jqGrid, according to its wiki entry, provides these options for its datatype parameter:


  • xml
  • json
  • local or clientSide
  • xmlstring
  • jsonstring
  • script
  • function (…)


I tested the tool with XML, but JSON support would appear to be consistent with other technologies we're using on the project.

jqGrid is not bug-free: in particular, I'm seeing a one-off issue with specifying the number of rows to display, and there are some formatting weirdnesses with the navigation bar. But it is the only tool of the five with a wiki for documentation (albeit the wiki's spelling and grammar are a little wonky), and it is the most fully-featured tool. Indeed, the download page offers a choice of optional components to download.

I will proceed with building out a prototype of the new search results page with jqGrid.

26 August 2009

From Babbage to EDVAC

A tidy history of early mechanical, analog, and electronic computing, fully referenced, by B. Jack Copeland for the Stanford Encyclopedia of Philosophy—with a fairly clear explanation of how mercury delay line memories worked.