本文整理汇总了Python中django.contrib.postgres.search.TrigramSimilarity方法的典型用法代码示例。如果您正苦于以下问题:Python search.TrigramSimilarity方法的具体用法?Python search.TrigramSimilarity怎么用?Python search.TrigramSimilarity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.search
的用法示例。
在下文中一共展示了search.TrigramSimilarity方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import TrigramSimilarity [as 别名]
def post_search(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = Post.published.annotate(
similarity=TrigramSimilarity('title', query),
).filter(similarity__gt=0.1).order_by('-similarity')
return render(request,
'blog/post/search.html',
{'form': form,
'query': query,
'results': results})
示例2: post_search
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import TrigramSimilarity [as 别名]
def post_search(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = Post.objects.annotate(
similarity=TrigramSimilarity('title', query),
).filter(similarity__gt=0.3).order_by('-similarity')
return render(request,
'blog/post/search.html',
{'form': form,
'query': query,
'results': results})
示例3: _find_keyword_or_split
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import TrigramSimilarity [as 别名]
def _find_keyword_or_split(self, text: str):
"""Attempts to find a keyword for the full string, in case of failure,
attempts to chip off the last word and repeats. Similarity depends on
the length of the string, hence edit (Levenshtein) distance filter was
introduced so that 'amerikkalainen jalkapallo' is matched to one keyword
and 'nuorisotalo jalkapallo' is matched to two.
The function allows 2 edits per every 5 symbols, which helps both with
simple plurals and true typos.
TODO: when a two-word keyword is preceded by something as in
'nuorisotalo amerikkalainen jalkapallo' it is mapped to 'nuorisotalo'
'jalkapallo', and 'amerikkalaiset', should be 'nuorisotalo' and
'amerikkalainen jalkapallo'.
TODO: correct language treatment, so that 'body condition' is searched
for in the English, not the Finnish keywords.
"""
text = text.strip(" ")
match = (
Keyword.objects.annotate(similarity=TrigramSimilarity("name_fi", text))
.order_by("similarity")
.last()
)
if match and distance(match.name_fi, text) / len(text) < 0.2:
return set([match])
if " " not in text:
return set()
split = text.rsplit(" ", 1)
keywords = set()
for i in split:
keywords |= self._find_keyword_or_split(i)
return keywords
示例4: search_taxonomy_node
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import TrigramSimilarity [as 别名]
def search_taxonomy_node(request, short_name):
if not request.user.is_authenticated:
return HttpResponse('Unauthorized', status=401)
dataset = get_object_or_404(Dataset, short_name=short_name)
if not dataset.user_is_maintainer(request.user):
raise HttpResponseNotAllowed
taxonomy = dataset.taxonomy
query = request.GET.get('q', '')
# vector = SearchVector('name', weight='A') + SearchVector('description', weight='C')
# query = SearchQuery(query)
# qs_results = TaxonomyNode.objects.filter(taxonomy__dataset=dataset)\
# .annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.3)\
# .order_by('rank')
qs_results = TaxonomyNode.objects.filter(taxonomy__dataset=dataset)\
.annotate(similarity=TrigramSimilarity('name', query) +
TrigramSimilarity('description', query),)\
.filter(similarity__gte=0.2)\
.order_by('-similarity')
results = [{'name': node.name,
'node_id': node.node_id,
'path': ' > '.join([TaxonomyNode.objects.get(node_id=node_id).name for node_id in path_list]),
'big_id': ','.join(path_list),
'omitted': node.omitted_curation_task}
for node in qs_results
for path_list in taxonomy.get_hierarchy_paths(node.node_id)]
return JsonResponse(results[:10], safe=False)
#############################
# CONTRIBUTE TO DATASET VIEWS
#############################
示例5: test_trigram_similarity
# 需要导入模块: from django.contrib.postgres import search [as 别名]
# 或者: from django.contrib.postgres.search import TrigramSimilarity [as 别名]
def test_trigram_similarity(self):
search = 'Bat sat on cat.'
self.assertQuerysetEqual(
self.Model.objects.filter(
field__trigram_similar=search,
).annotate(similarity=TrigramSimilarity('field', search)).order_by('-similarity'),
[('Cat sat on mat.', 0.625), ('Dog sat on rug.', 0.333333)],
transform=lambda instance: (instance.field, instance.similarity),
ordered=True,
)