From b75cd39163a3aaf996d6d69a22686fe00b13ad42 Mon Sep 17 00:00:00 2001 From: Alexander Kondratyev Date: Sat, 25 Mar 2017 15:35:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BB=D1=83=D1=87=D0=B0=D0=B9=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BF=D1=80=D0=BE=D1=81=D1=80=D0=BE=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B7=D0=BD=D0=B0=D0=BA=D0=B8,=20=D1=83=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=89=D1=91=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trademark/landing/__init__.py | 2 - trademark/landing/api.py | 147 ---------------------------------- trademark/views.py | 19 +++-- 3 files changed, 13 insertions(+), 155 deletions(-) delete mode 100644 trademark/landing/__init__.py delete mode 100644 trademark/landing/api.py diff --git a/trademark/landing/__init__.py b/trademark/landing/__init__.py deleted file mode 100644 index a5682fb..0000000 --- a/trademark/landing/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -#!/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 deleted file mode 100644 index 4b85982..0000000 --- a/trademark/landing/api.py +++ /dev/null @@ -1,147 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import random -import pymorphy2 -from trademark.models import Trademark, Product, Nice -from django.http import HttpResponse -import json -from django.views.decorators.cache import never_cache - - -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, t=0): - - if t: - print 't', list_words - - if len(list_words) > 1: - notation = list_words[random.randint(0, len(list_words) - 1)] - else: - notation = list_words[0] - - if normalize: - # print 'normalize 44', notation, notation[0], notation[1] - - morph = pymorphy2.MorphAnalyzer().parse(notation[0])[0] - try: - result = morph.inflect({'sing', 'nomn'}).word, notation[1] - except: - result = get_notation(list_words, normalize=True, t=1) - else: - # print notation - result = notation[0], notation[1] - - result = result[0].capitalize(), result[1] - - 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: - nice_id = product.nice_id - 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(' ', ''), nice_id)) - - 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, nice_id)) - - except Product.DoesNotExist: - return False, False - return _words, _generic_words - - -@never_cache -def get_random_list_trademarks(request, status='Expired', count=10): - ico = '' - notation = '' - trademark_list = [] - - expired_trademarks = Trademark.objects.filter(status=status).order_by('?')[:50] - random_list = _get_random_list(expired_trademarks)[:count*2] - - for trademark in random_list: - words, generic_words = get_lists_for_notation(trademark) - - if len(trademark.title.split(' ')) > 3: - continue - - if words: - notation = get_notation(words) - # print 'words', notation, notation[1] - if not words and generic_words: - # print generic_words - notation = get_notation(generic_words, normalize=True) - # print 'generic_words', notation, notation[0], notation[1] - if notation: - try: - pass - # print notation[1] - # print notation - glyph = Nice.objects.get(nice_id=notation[1]).glyph or 'fa-snowflake-o' - except Nice.DoesNotExist: - glyph = 'fa-snowflake-o' - else: - continue - # brand = u'{}'.format(trademark) - - trademark_list.append({ - 'product': notation[0], - 'name': trademark.title.title(), - 'trademark': trademark.id, - 'glyph': glyph - }) - - response = { - 'count': len(trademark_list), - 'status': 'ok', - 'items': trademark_list[:count] - } - - return HttpResponse(json.dumps(response), content_type="application/json") diff --git a/trademark/views.py b/trademark/views.py index 39fa6c2..e95cef0 100644 --- a/trademark/views.py +++ b/trademark/views.py @@ -231,29 +231,36 @@ def get_random_list_trademarks(request, status='Expired', count=10): if len(trademark.title.split(' ')) > 3: continue + nice_id = None + normalize_title = None for product in trademark.products.all(): words = product.title.split(' ') - normalize_title = [] + normalize_title = None if len(words) <= 3: first_word = morph.parse(words[0])[0] + if first_word.tag.POS == 'NOUN': - for word in words: - word = morph.parse(word)[0] + normalize_title = product.title + if len(words) == 1: try: - normalize_title.append(word.inflect({'sing', 'nomn'}).word) + normalize_title = word.inflect({'sing', 'nomn'}).word except: continue + nice_id = product.nice_id break + if not normalize_title: + continue + try: - glyph = Nice.objects.get(nice_id=notation[1]).glyph or 'fa-snowflake-o' + glyph = Nice.objects.get(nice_id=nice_id).glyph or 'fa-snowflake-o' except Nice.DoesNotExist: # Быть такого не должно glyph = 'fa-snowflake-o' trademark_list.append({ - 'product': ' '.join(normalize_title).capitalize(), + 'product': normalize_title.capitalize(), 'name': trademark.title.title(), 'trademark': trademark.id, 'glyph': glyph