init
This commit is contained in:
123
BaseModels/search_funcs.py
Normal file
123
BaseModels/search_funcs.py
Normal file
@@ -0,0 +1,123 @@
|
||||
try:
|
||||
import settings_local
|
||||
|
||||
pg_fts_config = 'pg_catalog.russian' # 'public.mipp_fulltext'
|
||||
except:
|
||||
pg_fts_config = 'pg_catalog.russian'
|
||||
|
||||
from django.db import models
|
||||
from django.contrib.postgres.search import Value, Func
|
||||
import copy
|
||||
|
||||
|
||||
# получаем из списка только слова содержащие цифры
|
||||
def get_list_words_contains_nums(txt):
|
||||
from .inter import numbers
|
||||
|
||||
if type(txt) == str:
|
||||
words = txt.split(' ')
|
||||
else:
|
||||
words = txt
|
||||
|
||||
words_w_nums = []
|
||||
|
||||
# получаем слова с цифрами
|
||||
res_words = []
|
||||
|
||||
for word in words:
|
||||
i = 0
|
||||
|
||||
while i < len(word):
|
||||
if word[i] in numbers:
|
||||
res_words.append(word)
|
||||
break
|
||||
i += 1
|
||||
|
||||
return res_words
|
||||
|
||||
|
||||
# получаем список слов с разделенными цифрами и текстом
|
||||
def get_list_split_words_w_nums(txt):
|
||||
from .inter import numbers
|
||||
|
||||
if type(txt) == str:
|
||||
words = txt.split(' ')
|
||||
else:
|
||||
words = txt
|
||||
|
||||
# words_w_nums = []
|
||||
|
||||
# получаем слова с цифрами
|
||||
words_w_devided_nums = []
|
||||
for word in copy.copy(words):
|
||||
|
||||
i = 0
|
||||
is_number = False
|
||||
cut_piece_compete = False
|
||||
|
||||
while i < len(word):
|
||||
if i == 0:
|
||||
if word[i] in numbers:
|
||||
is_number = True
|
||||
else:
|
||||
is_number = False
|
||||
else:
|
||||
if word[i] in numbers:
|
||||
if not is_number:
|
||||
cut_piece_compete = True
|
||||
else:
|
||||
if is_number:
|
||||
cut_piece_compete = True
|
||||
|
||||
if cut_piece_compete:
|
||||
cut_piece_compete = False
|
||||
words_w_devided_nums.append(word[0:i])
|
||||
|
||||
# if is_number:
|
||||
# words_w_nums.append(word[0:i])
|
||||
|
||||
word = word[i:]
|
||||
i = 0
|
||||
else:
|
||||
i += 1
|
||||
|
||||
if i > 0:
|
||||
words_w_devided_nums.append(word[0:i])
|
||||
# if is_number:
|
||||
# words_w_nums.append(word[0:i])
|
||||
|
||||
return words_w_devided_nums
|
||||
|
||||
|
||||
class Headline(Func):
|
||||
function = 'ts_headline'
|
||||
|
||||
def __init__(self, field, query, config=None, options=None, **extra):
|
||||
expressions = [field, query]
|
||||
if config:
|
||||
expressions.insert(0, Value(config))
|
||||
if options:
|
||||
expressions.append(Value(options))
|
||||
extra.setdefault('output_field', models.TextField())
|
||||
super(Headline, self).__init__(*expressions, **extra)
|
||||
|
||||
|
||||
def get_search_lexems_list(search_phrase):
|
||||
from django.db import connection
|
||||
search_lexems_list = None
|
||||
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SET NAMES 'UTF8';")
|
||||
# cursor.execute(u"SET CHARACTER SET 'utf8';")
|
||||
# cursor.execute(u"SET character_set_connection='utf8';")
|
||||
cursor.execute("SELECT plainto_tsquery('{1}', '{0}');".format(search_phrase, pg_fts_config))
|
||||
search_lexems = cursor.fetchone()
|
||||
s = search_lexems[0] # .decode('utf8')
|
||||
|
||||
if search_lexems:
|
||||
search_lexems = s.replace('\\', '')
|
||||
search_lexems = search_lexems.replace("'", '')
|
||||
search_lexems = search_lexems.replace(" ", '')
|
||||
search_lexems_list = search_lexems.split('&')
|
||||
|
||||
return search_lexems_list
|
||||
Reference in New Issue
Block a user