export_as_csv.py 1.46 KB
# -*- coding:utf-8 -*-

import codecs
import datetime
import os

from django.core.management.base import BaseCommand

from normalization import normalize
from settings import PROJECT_PATH
from webapp.models import Meaning

NORMALIZE = True
SOURCE = 'sjp'

CSV_PATH = os.path.join(PROJECT_PATH, 'data', '%s_%s.csv' % (SOURCE,
                                                             datetime.datetime.now().strftime('%Y%m%d')))

class Command(BaseCommand):
    help = 'Get database as csv.'

    def handle(self, *args, **options):
        export_csv()


def export_csv():
    try:
        csv_file = codecs.open(CSV_PATH, 'wt', 'utf-8')
        meanings = Meaning.objects.filter(expressions__link__source__key=SOURCE).distinct()
        for meaning in meanings.order_by('id'):
            print meaning.id
            if NORMALIZE:
                mng_expressions = meaning.expressions.all()
                orth_expressions = [expr.orth_text for expr in mng_expressions]
                orth_expressions.extend(normalize.expressions(mng_expressions))
                csv_file.write(u'%d\t%s\n' % (meaning.id,
                                              u'\t'.join([expr for expr in orth_expressions])))
            else:
                csv_file.write(u'%d\t%s\n' % (meaning.id,
                                              u'\t'.join([expr.orth_text for expr in
                                                          meaning.expressions.all()])))
    finally:
        csv_file.close()