Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare each object with each?

Tags:

python

django

How can I compare each object with each and if ratio() > 0.7 set possible_duplicate=True for both objects?

My try:

from difflib import SequenceMatcher


class Item(models.Model):
   name = models.CharField(max_length=255)
   desc = models.TextField()
   possible_duplicate = models.BooleanField(default=False)



items = Item.objects.all()

for item in items:
    obj = Item.objects.get(pk=item.pk)
    similarity = SequenceMatcher(None, item.desc, obj.desc).ratio()
    if similarity > 0.7:
        item.possible_duplicate = True
        item.save()
        obj.possible_duplicate = True
        obj.save()
like image 870
user4812479812 Avatar asked Mar 30 '16 09:03

user4812479812


2 Answers

You can use itertools.combinations to get comparison combinations:

>>> import itertools
>>> items = [1, 2, 3]
>>> itertools.combinations(items, 2)  # 2 -> yields tuples with 2 items
<itertools.combinations object at 0x7f5e456d5ba8>
>>> list(itertools.combinations(items, 2))
[(1, 2), (1, 3), (2, 3)]

import itertools

items = Item.objects.all()

for item1, item2 in itertools.combinations(items, 2):
    similarity = SequenceMatcher(None, item1.desc, item2.desc).ratio()
    if similarity > 0.7:
        for item in item1, item2:
            item.possible_duplicate = True
            item.save()
like image 163
falsetru Avatar answered Nov 17 '22 13:11

falsetru


In your code you are comparing the object to itself. To compare all objects with each other you can use itertools.combinations

items_list = list(Items.objects.all())

for a,b in itertools.combinations(items_list, 2):
    similarity = SequenceMatcher(None, a.desc, b.desc).ratio()
    if similarity > 0.7:
        a.possible.duplicate = True
        a.save()
        b.possible.duplicate = True
        b.save()
like image 41
ilse2005 Avatar answered Nov 17 '22 12:11

ilse2005