Skip to main content

Duplicate-Recognition

This Project uses the calculation of similarities scores of a set of entities in an edge list. To allow for versatile usage, it uses dependency injection to implement it into any application.

Usage

You need to implement all the read/write methods, to keep the project versatile.

For an example on how to use it, see the example, or the following code block:

"""
This is an example implementation of the DuplicateRecognition class.
It won't work. It's just to show, how it could be used.
"""

import logging
import os
from collections import defaultdict
from typing import Dict, Set
from typing import Generator, Tuple, Any
from itertools import chain, islice

from mysql.connector import connect

from duplicate_recognition import DuplicateRecognition, Algorithm, Comparison

logging.basicConfig(level=logging.DEBUG)


def chunks(iterable, size=1000):
    # https://stackoverflow.com/a/24527424
    iterator = iter(iterable)
    for first in iterator:
        yield chain([first], islice(iterator, size - 1))


class Entity(DuplicateRecognition):
    ID_COLUMN: str = "id"
    F_SCORES: Dict[str, float] = defaultdict(lambda: 0, {
        "id": DuplicateRecognition.F_SCORE_FOR_EXACT_MATCH,
        "company": 1,
        "postal_code": 1,
        "country": 0.5,
    })
    MATCHING_ALGORITHM: Dict[str, Algorithm] = defaultdict(lambda: Algorithm.EQUALITY, {
        "id": Algorithm.EQUALITY,
        "company": Algorithm.PHONETIC_DISTANCE,
        "postal_code": Algorithm.EQUALITY,
        "country": Algorithm.COUNTRY,
    })
    THRESHOLDS: Dict[str, float] = defaultdict(lambda: 0, {
        "country": 1,
    })
    NEGATIVE_FIELDS: Set[str] = {"country"}

    def __init__(self):
        self.connection = connect(
            host=os.getenv("MYSQL_HOST"),
            port=os.getenv("MYSQL_PORT"),
            user=os.getenv("MYSQL_USER"),
            password=os.getenv("MYSQL_PASSWORD"),
            database="foo",
        )
        super().__init__()

    def get_relevant_entities(self) -> Generator[Dict[str, Any], None, None]:
        cursor = self.connection.cursor(dictionary=True)

        cursor.execute("""
            SELECT DISTINCT * FROM entity    
            ORDER BY entity.id ASC
            """)
        return cursor

    def get_refresh_pairs(self) -> Generator[Tuple[int, int], None, None]:
        cursor = self.connection.cursor(buffered=True)
        cursor.execute("""
            SELECT entity_edge_list.a, entity_edge_list.b
            FROM entity_edge_list
            
            INNER JOIN entity
                ON entity.id = entity_edge_list.a OR entity.id = entity_edge_list.b
            
            WHERE entity.change_date > entity_edge_list.change_date
            ORDER BY entity_edge_list.a, entity_edge_list.b ASC
            """)
        return cursor

    def get_compared(self) -> Generator[int, None, None]:
        cursor = self.connection.cursor(buffered=True)
        cursor.execute("SELECT DISTINCT a FROM entity_edge_list")
        for row in cursor:
            yield row[0]

    def get_uncompared(self) -> Generator[int, None, None]:
        cursor = self.connection.cursor(buffered=True)

        cursor.execute("""
            SELECT DISTINCT entity.id
            FROM entity
            LEFT JOIN entity_edge_list
                ON entity.id = entity_edge_list.a
        
            WHERE entity_edge_list.a IS NULL
            ORDER BY entity.id ASC
            """)
        for row in cursor:
            yield row[0]

    def write_comparisons(self, comparisons: Generator[Comparison, None, None]):
        cursor = self.connection.cursor()

        query = f"""
        INSERT INTO entity_edge_list (a, b, score, count, f_score_sum, change_date) VALUE (%s, %s, %s, %s, %s, NOW())
        ON DUPLICATE KEY UPDATE score=VALUES(score), count=VALUES(count), f_score_sum=VALUES(f_score_sum), change_date=NOW();
        """

        # execute in batches of 1000
        for chunk in chunks(comparisons, size=1000):
            cursor.executemany(query, [
                (c.entity[self.ID_COLUMN], c.other_entity[self.ID_COLUMN], c.score, c.count, c.f_score_sum)
                for c in chunk
            ])
        self.connection.commit()


if __name__ == "__main__":
    Entity().execute(limit=None)

Release files for duplicate-recognition 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for duplicate-recognition 0.1.0
File Interpreter ABI Platform
duplicate_recognition-0.1.0-py3-none-any.whl Python 3 none any Details

Release files / duplicate_recognition-0.1.0-py3-none-any.whl

Download URL duplicate_recognition-0.1.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef1fe9bd552e6920c527b924fb8ae0ced965f96f290a05ea6c1204824615c08f
BLAKE2b-256 checksum
How to use checksums
9123ed1d0c97a638dc813874c5827ac73fbf34ad3a529f302fa5de783d054618
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

0.0.12

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page