aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/func/similar.py
blob: 3955c03409852b69c628391ae29ce16d58be50c4 (plain)
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
from queue import Queue
from typing import List

from szurubooru import model, search

_search_executor_config = search.configs.PostSearchConfig()
_search_executor = search.Executor(_search_executor_config)


def find_similar_posts(source_post: model.Post, limit: int) -> List[model.Post]:
    results = []
    queue = Queue()  # contains lists of tags to search
    queue.put(source_post.tags)

    while not queue.empty():
        # put follow-up searches on the queue
        last_tags = queue.get()
        if len(last_tags) > 1:
            for removed_tag in last_tags:
                next_search = list(filter(lambda t: t != removed_tag, last_tags))
                queue.put(next_search)

        # prepare the current search, remove known results
        query = ' '.join([t.first_name for t in last_tags])
        query += ' -id:%d' % source_post.post_id
        for r in results:
            query += ' -id:%d' % r.post_id

        # execute
        _, posts = _search_executor.execute(query, 0, limit)

        # update results
        for p in posts:
            results.append(p)
            if len(results) >= limit:
                return results

    return results

© 2015 - 2026 Jakob L. Kreuze