summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunternif <hunternif@gmail.com>2021-09-01 22:38:03 +0100
committerHunternif <hunternif@gmail.com>2021-09-01 22:38:03 +0100
commitfff705a6f6e46ccc1590668a67af9df6fee2a257 (patch)
treebba015c7911e8ae11bd5e0cdd08a45c884dd4e0a
parent2a149935d14936a158e5ac022fba10b65c9c02dc (diff)
server: update similar post logic to yield more results
-rw-r--r--server/szurubooru/func/similar.py2
-rw-r--r--server/szurubooru/tests/func/test_similar.py39
2 files changed, 39 insertions, 2 deletions
diff --git a/server/szurubooru/func/similar.py b/server/szurubooru/func/similar.py
index 77d7805..37f3f0d 100644
--- a/server/szurubooru/func/similar.py
+++ b/server/szurubooru/func/similar.py
@@ -14,7 +14,7 @@ def find_similar_posts(source_post: model.Post, limit: int) -> List[model.Post]:
# This will help yield results quicker
source_tags = sorted(source_post.tags, key=lambda t: t.post_count)
source_tag_count = len(source_tags)
- max_removals = ceil(source_tag_count / 2) # remove at most 50% of tags
+ max_removals = source_tag_count - 1
tags = source_tags
for x in range(max_removals + 1):
diff --git a/server/szurubooru/tests/func/test_similar.py b/server/szurubooru/tests/func/test_similar.py
index 9200ff1..486f67b 100644
--- a/server/szurubooru/tests/func/test_similar.py
+++ b/server/szurubooru/tests/func/test_similar.py
@@ -9,9 +9,11 @@ def verify_posts():
actual_post_ids = list([p.post_id for p in actual_posts])
expected_post_ids = list([p.post_id for p in expected_posts])
assert actual_post_ids == expected_post_ids
+
return verify
+# I'd like my behavior to be like this
def test_find_similar_posts(post_factory, tag_factory, verify_posts):
tagA = tag_factory(names=["a"])
tagB = tag_factory(names=["b"])
@@ -43,6 +45,38 @@ def test_find_similar_posts(post_factory, tag_factory, verify_posts):
verify_posts(results, [postABC, postBC, postAB, postA])
+# but it's actually like this for performance reasons
+def test_find_similar_posts_naive(post_factory, tag_factory, verify_posts):
+ tagA = tag_factory(names=["a"]) # count=4
+ tagB = tag_factory(names=["b"]) # count=3
+ tagC = tag_factory(names=["c"]) # count=3
+ postA = post_factory(id=1, tags=[tagA])
+ postAB = post_factory(id=2, tags=[tagA, tagB])
+ postAC = post_factory(id=3, tags=[tagA, tagC])
+ postABC = post_factory(id=4, tags=[tagA, tagB, tagC])
+ postBC = post_factory(id=5, tags=[tagB, tagC])
+ db.session.add_all([tagA, tagB, tagC, postA, postAB, postAC, postABC, postBC])
+ db.session.flush()
+
+ results = similar.find_similar_posts(postBC, 10)
+ verify_posts(results, [postABC, postAC])
+
+ results = similar.find_similar_posts(postBC, 2)
+ verify_posts(results, [postABC, postAC])
+
+ results = similar.find_similar_posts(postABC, 10)
+ verify_posts(results, [postAC, postAB, postA])
+
+ results = similar.find_similar_posts(postA, 10)
+ verify_posts(results, [postABC, postAC, postAB]) # sorted by id
+
+ results = similar.find_similar_posts(postAB, 10)
+ verify_posts(results, [postABC, postAC, postA])
+
+ results = similar.find_similar_posts(postAC, 10)
+ verify_posts(results, [postABC, postAB, postA])
+
+
def test_find_similar_posts_with_limit(post_factory, tag_factory, verify_posts):
tagA = tag_factory(names=["a"])
tagB = tag_factory(names=["b"])
@@ -56,4 +90,7 @@ def test_find_similar_posts_with_limit(post_factory, tag_factory, verify_posts):
db.session.flush()
results = similar.find_similar_posts(postABCDE, 10)
- verify_posts(results, [postAB])
+ # I'd like it to be like this:
+ # verify_posts(results, [postAB])
+ # but it's like this for performance reasons:
+ verify_posts(results, [postAB, postA])