Script to run a transifex yml through aspell to correct spelling mistakes


(foobar) #1

I’m sharing this script that runs a transifex yml through aspell. I thought I’d share it in case it is useful to this community. It was to me.

import yaml
import sys
import os


LANG = 'ca'

stream = open(sys.argv[1], 'r')
dic = yaml.load(stream)
stream.close()
save_counter = 0


def save(force=False):
    global save_counter
    save_counter += 1
    if not force and save_counter % 10 != 0:
        return

    f = open(sys.argv[1] + '-out', 'w')
    print >>f, yaml.dump(dic, allow_unicode=True)
    f.close()


def fix(dic):
    if dic is None: return
    for k, v in dic.iteritems():
        if isinstance(v, str) or isinstance(v, unicode):
            fix_string(dic, k, v)
        else:
            fix(v)


def fix_string(dic, key, s):
    f = open('tmp.txt', 'w')
    print >>f, s.encode('utf-8'),
    f.close()

    os.system('aspell -l ' + LANG + ' -c tmp.txt --encoding=utf-8')

    f = open('tmp.txt', 'r')
    value = f.read()
    f.close()

    value = value.decode('utf-8')
    dic[key] = value
    save()

fix(dic)
save(force=True)