find_hanging_examples.py 3 KB
#-*- 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 '!!!!!!!!!!!!!! hanging example !!!!!!!!!!!!!!!!!'
                print '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 '!!!!!!!!!!!!!! multiple pointers !!!!!!!!!!!!!!!!!'
                print '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
    for example in lemma.nkjp_examples.all():
        same_lu_examples = LexicalUnitExamples.objects.filter(example=example, lexical_unit__in=lex_units)
        if same_lu_examples.count() > 1:
            print '!!!!!!!!!!!! two lus pinned to same example !!!!!!!!!!!!!!!!!!!'
            for lu_ex in same_lu_examples:
                print 'lemma: %s\tlu_ex_id: %d\texample: %s\tex_id: %d' % (lemma.entry_obj.name,
                                                                           lu_ex.id,
                                                                           example.sentence,
                                                                           example.id)

        if not lemma.frames.filter(pk=example.frame.pk).exists():
            print '!!!!!!!!!!!! example without schema !!!!!!!!!!!!!!!!!!!'
            print 'lemma: %s\tschema: %d\texample: %s\tex_id: %d' % (lemma.entry_obj.name,
                                                                     example.frame.id,
                                                                     example.sentence,
                                                                     example.id)