From be02fdf1f8ab6431351ccb3ab897de27cc4da837 Mon Sep 17 00:00:00 2001 From: mitri4 Date: Sun, 12 Mar 2017 00:54:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=B1=D1=80=D0=BE=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=20API=20=D0=B4=D0=BB=D1=8F=20landing=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/requirements.txt | 4 ++ trademark/landing/__init__.py | 2 + trademark/landing/api.py | 119 ++++++++++++++++++++++++++++++++++ trademark/urls.py | 3 + 4 files changed, 128 insertions(+) create mode 100644 trademark/landing/__init__.py create mode 100644 trademark/landing/api.py diff --git a/app/requirements.txt b/app/requirements.txt index 46a1603..c72e40b 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -73,3 +73,7 @@ tzlocal==1.2 Unidecode==0.4.18 vobject==0.6.6 YURL==0.13 +# +pymorphy2==0.8 +pymorphy2-dicts==2.4.393442.3710985 +DAWG-Python==0.7.2 \ No newline at end of file diff --git a/trademark/landing/__init__.py b/trademark/landing/__init__.py new file mode 100644 index 0000000..a5682fb --- /dev/null +++ b/trademark/landing/__init__.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- \ No newline at end of file diff --git a/trademark/landing/api.py b/trademark/landing/api.py new file mode 100644 index 0000000..a50e4d8 --- /dev/null +++ b/trademark/landing/api.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import random +import pymorphy2 +from trademark.models import Trademark, Product +from django.http import HttpResponse +import json + + +def _get_random_list(qs): + + _list = list(qs) + random.shuffle(_list) + + return _list + + +def _get_dic_words(words): + + words_dict = {} + + for key in words: + key = key.lower() + if key in words_dict: + value = words_dict[key] + words_dict[key] = value + 1 + else: + words_dict[key] = 1 + + return words_dict + + +def get_notation(list_words, normalize=False): + + if len(list_words) > 1: + notation = list_words[random.randint(0, len(list_words) - 1)] + else: + notation = list_words[0] + + if normalize: + morph = pymorphy2.MorphAnalyzer().parse(notation)[0] + try: + result = morph.inflect({'plur', 'nomn'}).word + except: + result = get_notation(list_words, normalize=True) + else: + result = notation + result = result.capitalize() + + return result + + +def get_lists_for_notation(trademark): + + _words = [] + _generic_words = [] + + morph = pymorphy2.MorphAnalyzer() + + try: + products = trademark.products.all() + for product in products: + if product: + product_phrase = product.title.split(',') + + for phrase in product_phrase: + words = phrase.\ + lower().\ + replace(';', '').\ + replace('.', '').\ + replace('[', '').\ + replace(']', ''). \ + replace('(', ''). \ + replace(')', ''). \ + replace('\n', '') + + if len(words.split()) == 1: + _words.append(words.replace(' ', '')) + + fraction_phrase = words.lower().split(' ') + for word in fraction_phrase: + p = morph.parse(word)[0] + if len(word) >= 3 and p.tag.POS == 'NOUN': + _generic_words.append(p.normal_form) + + except Product.DoesNotExist: + return False, False + + return _words, _generic_words + + +def get_random_list_trademarks(request, status='Expired', count=10): + notation = '' + trademark_list = [] + + expired_trademarks = Trademark.objects.filter(status=status) + random_list = _get_random_list(expired_trademarks)[:count] + + for trademark in random_list: + words, generic_words = get_lists_for_notation(trademark) + + if words: + notation = get_notation(words) + if not words and generic_words: + notation = get_notation(generic_words, normalize=True) + + if notation: + brand = u'{notation} "{title}"'.format(notation=notation, title=trademark) + # print u'%s' % notation + u' "%s"' % trademark + else: + brand = u'{}'.format(trademark) + + trademark_list.append({'name': brand, + 'ico': 'fa' + }) + + response = {'count': len(trademark_list), 'status': 'ok', 'items': trademark_list} + + return HttpResponse(json.dumps(response), content_type="application/json") \ No newline at end of file diff --git a/trademark/urls.py b/trademark/urls.py index d5f72e7..729fdc4 100644 --- a/trademark/urls.py +++ b/trademark/urls.py @@ -3,11 +3,14 @@ from django.conf.urls import patterns, url from . import views +from trademark.landing.api import get_random_list_trademarks + urlpatterns = patterns('', url(r'^$', views.IndexView.as_view(), name='index'), url(r'^results/(?P[\w-]+)/$', views.SearchDetailView.as_view(), name='search-detail'), url(r'^trademark/(?P[\w-]+)/$', views.TrademarkDetailView.as_view(), name='trademark-detail'), + url(r'^random-trademarks/', get_random_list_trademarks, name='random-trademarks'), url(r'^request/(?P[\w-]+)/$', views.Search.as_view(), name='search-detail'), url(r'^request/$', views.Search.as_view(), name='search'), )