views.py
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from rest_framework.decorators import api_view
from rest_framework.response import Response
from webapp.models import Expression, Segment
# wywolanie
#http://localhost:8000/get_expressions/?expression=kot
@api_view(['GET'])
def get_expressions(request):
expressions = Expression.objects.filter(base_text=request.GET['expression'],
main_expression=None,
is_catchword=True)
meanings = [expr.meaning for expr in expressions]
expressions_pks = []
for meaning in meanings:
for expr in meaning.expressions.filter(main_expression=None):
expressions_pks.append(expr.pk)
expressions = Expression.objects.filter(pk__in=expressions_pks).distinct('base_text')
lemmatized_expressions = [expr.base_text for expr in expressions.order_by('base_text')]
#expressions_text_reps = [unicode(expr) for expr in expressions.order_by('text')]
return Response({'expressions': lemmatized_expressions})
# wywolanie
#http://localhost:8000/get_synonyms/?expression=kot
@api_view(['GET'])
def get_synonyms(request):
#segments = Segment.objects.filter(base=request.GET['expression'])
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.filter(main_expression=None):
if expr.segments.count() == 1:
expressions_pks.append(expr.pk)
expressions = Expression.objects.filter(pk__in=expressions_pks).distinct('text')
expressions_text_reps = [expr.lemmatized_form() for expr in expressions.order_by('text')]
return Response({'synonyms': expressions_text_reps})
# wywolanie
#http://localhost:8000/get_synonyms/?expression=kot
@api_view(['GET'])
def get_catchwords(request):
expressions = Expression.objects.filter(base_text=request.GET['expression'],
main_expression=None,
is_catchword=False)
meanings = [expr.meaning for expr in expressions]
expressions_pks = []
for meaning in meanings:
for expr in meaning.expressions.filter(main_expression=None, is_catchword=True):
expressions_pks.append(expr.pk)
expressions = Expression.objects.filter(pk__in=expressions_pks).distinct('base_text')
lemmatized_expressions = [expr.base_text for expr in expressions.order_by('base_text')]
#expressions_text_reps = [unicode(expr) for expr in expressions.order_by('text')]
return Response({'expressions': lemmatized_expressions})