Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Management command to merge tags #6339

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions kitsune/sumo/management/commands/merge_similar_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.core.management.base import BaseCommand
from fuzzywuzzy import fuzz
from taggit.models import Tag, TaggedItem

SIMILARITY_THRESHOLD = 75


class Command(BaseCommand):
help = "Merge similar tags"

def handle(self, *args, **kwargs):
def recursively_merge_tags(tag_ids):
merged_any = False
deleted_tags = set()

for i, primary_tag_id in enumerate(tag_ids):
if primary_tag_id in deleted_tags:
continue

primary_tag = Tag.objects.get(id=primary_tag_id)

for secondary_tag_id in tag_ids[i + 1 :]:
if secondary_tag_id in deleted_tags:
continue

secondary_tag = Tag.objects.get(id=secondary_tag_id)
similarity = fuzz.ratio(primary_tag.name, secondary_tag.name)
if similarity >= SIMILARITY_THRESHOLD:
duplicate_conflicts = TaggedItem.objects.filter(
tag=secondary_tag,
object_id__in=TaggedItem.objects.filter(tag=primary_tag).values_list(
"object_id", flat=True
),
)
duplicate_conflicts.delete()

TaggedItem.objects.filter(tag=secondary_tag).update(tag=primary_tag)

secondary_tag.delete()
deleted_tags.add(secondary_tag_id)

print(f"Merged '{secondary_tag.name}' into '{primary_tag.name}'")
merged_any = True
break # start over

if merged_any:
remaining_tag_ids = (
Tag.objects.exclude(id__in=deleted_tags)
.order_by("-id")
.values_list("id", flat=True)
)
return recursively_merge_tags(list(remaining_tag_ids))

tag_ids = Tag.objects.all().order_by("-id").values_list("id", flat=True)
recursively_merge_tags(list(tag_ids))
18 changes: 17 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ google-analytics-data = "0.18.7"
pyparsing = "3.1.2"
django-silk = "^5.1.0"
requests = "^2.32.3"
fuzzywuzzy = "^0.18.0"

[tool.poetry.group.dev.dependencies]
ipdb = "^0.13.11"
Expand Down