Created
August 4, 2020 19:16
-
-
Save svenoaks/4632bc745e4cc152d4790fd04aa3f02b to your computer and use it in GitHub Desktop.
Count words in Android strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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