Core Java

Capitalize English Titles With Java, Quarkus & GraalVM

Over the years, I’ve been writing a lot in English, which isn’t my mother tongue, and for writing titles (headlines, article titles, etc.) I always have to think which words to capitalize. Thus, I’ve created a small tool, of course written in Java, that I use as command line tool, powered by Quarkus, Picocli, and GraalVM.

What I want to have is a method that takes a single line or multiple lines and capitalizes them. For my writing, I use the APA style. You can check out different ways of capitalization in online tools such as this one.

I’ve created a small Java project that uses Quarkus, Picocli, and GraalVM to run the code as standalone application on the command line. Especially with native executables built by GraalVM, we can speed up the time the command line program takes. While the JAR/JVM version takes a few hundred milliseconds to complete, the native executable runs pretty much instant. For command line tools, you will notice a difference.

You can find the code on GitHub.

The CapitalizeCommand class can run the whole thing as command line program. If you want to build the project yourself, you simply need to clone and execute mvn package -P native and copy the resulting target/captalize-title-runner executable into your $PATH.

The produced executable works as follows:

You can see some of the particularities when capitalizing titles, with regards to prepositions, hyphenated words, etc. It’s also important to mention that a simple software solution that doesn’t perform any Grammar analysis won’t always produce correct results since some words might occur differently as prepositions, conjunctions, adjective, or adverbs (e.g. yet, out, so, etc). But for my purposes, this solution is good enough ™.

It’s also very nice to include the command in my usual writing editor, Vim in my case. The command ignores trailing special characters, which makes it easy to invoke it in formats such as AsciiDoc:

For Vim, you can trigger the tool as follows:

" in your .vimrc
nmap <leader>c :.!capitalize-title -<CR>
vnoremap <leader>c :.!capitalize-title -<CR>

You can check out the code on GitHub: Capitalize Titles

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Capitalize English Titles With Java, Quarkus & GraalVM

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button