views.py 2.27 KB
from rest_framework.decorators import api_view
from rest_framework.response import Response

from normalization import normalize
from webapp.models import Entry, Expression

# wywolanie
#http://localhost:8000/get_expressions/?expression=kot
@api_view(['GET'])
def get_expressions(request):
    entry = Entry.objects.get(name=request.GET['expression'].upper())

    expressions_pks = []
    for meaning in entry.meanings.all():
        for expr in meaning.expressions.filter():
            expressions_pks.append(expr.pk)
    expressions = Expression.objects.filter(pk__in=expressions_pks, link__source__protected=False).distinct('text')

    orth_expressions = [expr.orth_text for expr in expressions]
    orth_expressions.extend(normalize.expressions(expressions))

    return Response({'expressions': orth_expressions})


# wywolanie
#http://localhost:8000/get_synonyms/?expression=kot
@api_view(['GET'])
def get_synonyms(request):
    expressions = Expression.objects.filter(text=request.GET['expression'].upper())

    meanings = [expr.meaning for expr in expressions if expr.segments.count() == 1]
    expressions_pks = []
    for meaning in meanings:
        for expr in meaning.expressions.all():
            if expr.segments.count() == 1:
                expressions_pks.append(expr.pk)
    expressions = Expression.objects.filter(pk__in=expressions_pks, link__source__protected=False).distinct('base_text')
    expressions_text_reps = [expr.base_text for expr in expressions.order_by('base_text')]

    return Response({'synonyms': expressions_text_reps})


# wywolanie
#http://localhost:8000/get_catchwords/?expression=kot
@api_view(['GET'])
def get_catchwords(request):
    expressions = Expression.objects.filter(text=request.GET['expression'].upper(),
                                            is_catchword=False)
    meanings = [expr.meaning for expr in expressions]
    expressions_pks = []
    for meaning in meanings:
        for expr in meaning.expressions.filter(is_catchword=True):
            expressions_pks.append(expr.pk)
    expressions = Expression.objects.filter(pk__in=expressions_pks, link__source__protected=False).distinct('base_text')
    lemmatized_expressions = [expr.base_text for expr in expressions.order_by('base_text')]

    return Response({'expressions': lemmatized_expressions})