Android Word Count
I was asked to add a word and character count to my Editor app. So I looked it up and found a Gist that uses BreakIterator. This relies on an embedded function that determines what is considered a word.
So I tested it and found that it gave different results to the linux
utility wc. However, the app already makes great use of regular
expressions, which include an expression for a word character \w
.
So I wrote a very simple implementation using the regular expression
\w+
, which means one or more word characters.
public final static Pattern WORD_PATTERN = Pattern.compile
("\\w+", Pattern.MULTILINE);
// wordcountText
private void wordcountText()
{
int words = 0;
Matcher matcher = WORD_PATTERN.matcher(textView.getText());
while (matcher.find())
{
words++;
}
if (customView != null)
{
String string = String.format(Locale.getDefault(), "%d\n%d",
words, textView.length());
customView.setText(string);
}
}
This gives identical results to the utility wc for text files, but differs for source code files, which is probably due to differing definitions of what constitutes a word. It would be possible to add the line count as well as the character count, but there isn’t room in the toolbar of an android app for three lines of text.
To trigger a word count when the text changes in the editor I added
some code to the onCreate()
function and the text changed listener.
updateWordCount = () -> wordcountText();
if (updateWordCount != null)
{
textView.removeCallbacks(updateWordCount);
textView.postDelayed(updateWordCount, UPDATE_DELAY);
}
This updates the word count in the background after a small delay from the last text change, so it is not running until necessary.