#-*- coding:utf-8 -*-

"""Script for getting prepositions usage frequency list."""

from django.core.management.base import BaseCommand

from dictionary.models import *

class Command(BaseCommand):
    'SloVal command for getting preposition frequency list.'
    help = 'Get prepositions frequency use list.'

    def handle(self, **options):
        prep_freq_ls()  

def prep_freq_ls():
    'Get prepositions frequency list.'
    print 'Be patient, it can take a while.'
    prep_ls = []
    for prep in Atribute_Model.objects.get(atr_model_name='PRZYIMEK').atribute_values.all():
        prep_ls.append({'prep' : prep.value,
                        'count': 0})
    lemmas = Lemma.objects.filter(old=False, status__status=u'sprawdzone')
    for lemma in lemmas.all():
        for frame in lemma.frames.all():
            for position in frame.positions.all():
                for arg in position.arguments.all():
                    for attr in arg.atributes.filter(Q(type='PRZYIMEK') | Q(type='PRZYIMEK1')):
                        prep = (item for item in prep_ls if item['prep'] == attr.atribute_value.value).next()
                        prep['count'] += 1
    prep_ls.sort(key=lambda x:x['count'])
    prep_ls.reverse()     
    for prep in prep_ls:
        print smart_str(prep['prep']) + ':  ' + str(prep['count'])             
  