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

from django.core.management.base import BaseCommand

from dictionary.models import Lemma
from semantics.models import LexicalUnitExamples

class Command(BaseCommand):
    args = 'none'
    help = ""

    def handle(self, **options):
        find_hanging_examples()
        
def find_hanging_examples():
    lemmas = Lemma.objects.filter(old=False).order_by('entry_obj__name')
    for lemma in lemmas:
        print_hanging_examples(lemma)
    
def print_hanging_examples(lemma):
    lex_units = lemma.entry_obj.meanings.all()
    for lu in lex_units:
        lu_examples = LexicalUnitExamples.objects.filter(lexical_unit=lu)
        for lu_ex in lu_examples:
            example = lu_ex.example
            if not lemma.nkjp_examples.filter(id=example.id).exists():
                print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
                print 'hanging example --> lemma: %s\tlu_ex_id: %d\texample: %s\tex_id: %d' % (lemma.entry_obj.name,
                                                                                               lu_ex.id,
                                                                                               example.sentence,
                                                                                               example.id)
            same_lu_examples = LexicalUnitExamples.objects.filter(lexical_unit=lu, example=example)
            if same_lu_examples.count() > 1:
                print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
                print 'multiple pointers --> lemma: %s\tlu_ex_id: %d\texample: %s\tex_id: %d' % (lemma.entry_obj.name,
                                                                                                 lu_ex.id,
                                                                                                 example.sentence,
                                                                                                 example.id)
                for ex in same_lu_examples.all():
                    print 'lu_ex_id: %d' % ex.id
                
                