Count words in Android string resource XML files

I put together a simple Python script that counts the words in one or more Android string resource XML file, and thought other people might find use for it as well.

The script is written for Python 2.7, but may work in other versions.

You run the script from the commandline and pass as arguments the string resource file(s) you want to count words in, for example MyApp/res/values/strings.xml MyApp/res/values-es/strings.xml

And here is the source code for the script:

from xml.dom.minidom import parse
import sys
import os
import codecs
import re

def count( files ):
    for f in files:
        if not os.path.exists( f ):
            print "File %s does not exist" % f
            sys.exit( -1 )
    total_count = {}
    for filename in files:
        count = 0
        with open( filename, 'r' ) as f:
            xml = parse( f )
            for item in xml.getElementsByTagName( "string" ):
                for cn in item.childNodes:
                    if cn.nodeType == cn.TEXT_NODE:
                        count += count_words( )
            for plural in xml.getElementsByTagName( "plurals" ):
                for item in plural.childNodes:
                    for cn in item.childNodes:
                        if cn.nodeType == cn.TEXT_NODE:
                            count += count_words( )
        total_count[ filename ] = count
    print total_count
def count_words( str ):
    words = re.findall(ur'\w+', str)
    return len( words )
if __name__ == "__main__":
    if len( sys.argv ) <= 1:
        print "Enter string resource file(s) as arguments to this script."
        sys.exit( -1 )
    count( sys.argv[1:] )

About the author

Jesper Borgstrup Jesper is a Masters student of computer science at the University of Copenhagen with many years of experience in writing applications for Android.

2 thoughts on “Count words in Android string resource XML files

    1. Jesper Borgstrup Post author

      Hi, it didn’t work due to a bug in how I implemented the SSL redirect (Buzzing Android now runs entirely on HTTPS), but it is working now.

      Thank you for reporting this and I am glad that you like the layout finder :-)


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>