Skip to content

Instantly share code, notes, and snippets.

@svenoaks
Created August 4, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenoaks/4632bc745e4cc152d4790fd04aa3f02b to your computer and use it in GitHub Desktop.
Save svenoaks/4632bc745e4cc152d4790fd04aa3f02b to your computer and use it in GitHub Desktop.
Count words in Android strings.xml
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:] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment