Created
November 12, 2015 19:35
-
-
Save Turnsole/e51845987f898965bc70 to your computer and use it in GitHub Desktop.
Quick and dirty way to see how many words are contained in an Android string resources file. (I needed an estimate to guess how much hiring a translator would cost.)
This file contains hidden or 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
import argparse | |
import re | |
import sys | |
regex = re.compile(r'<string .*>(.*?)<\/string>') | |
def scrape_line(line): | |
matched = re.findall(regex, line) | |
if matched is None or len(matched) == 0: | |
return 0 | |
else: | |
return len(matched[0].split()) | |
def main(sys_args): | |
parser = argparse.ArgumentParser(description='Generate word count for an Android string resource file.') | |
parser.add_argument('pathToFile', metavar='pathToFile', nargs=1, help='path to file (ex: /user/project/res/values/strings.xml)') | |
args = parser.parse_args(sys_args) | |
input_file = open(args.pathToFile[0], 'r') | |
count_lines = 0 | |
count_words = 0 | |
for line in input_file: | |
count_lines += 1 | |
count_words += scrape_line(line) | |
print 'Parsed {} lines and found {:,} words.'.format(count_lines, count_words) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment