Tag Archives: strings

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

android-string-wc.py 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( cn.data )
            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( cn.data )
        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:] )