osc_homonyms.py 1 KB
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.db import transaction

from accounts.util import bot_history
from dictionary.models import Lexeme, Vocabulary


class Command(BaseCommand):
    help = 'fixes homonym numbers'

    @transaction.atomic
    def handle(self, **options):
        bot_history()
        vocab = Vocabulary.objects.get(id='antyMorfeusz')
        osc_entries = Lexeme.filter_reader(
            Lexeme.objects.filter(part_of_speech='osc'))\
            .values_list('entry', flat=True)
        osc_distinct = list(osc_entries.distinct())
        subst_osc = Lexeme.filter_reader(Lexeme.objects.filter(
            part_of_speech='subst', entry__in=osc_distinct))
        subst_osc_entries = list(
            subst_osc.values_list('entry', flat=True).distinct())
        redundant_osc = Lexeme.filter_reader(Lexeme.objects.filter(
            part_of_speech='osc', entry__in=subst_osc_entries))
        for osc in redundant_osc:
            vocab.add_lexeme(osc)