Skip to main content

You can group similar items (e.g., strings or dict fields) using the fuzzywuzzy package by comparing their similarity scores.

Project description

🔍 groupbyScore – Group Similar Items Using Fuzzy Matching in Python

GitHub

Overview

groupbyScore is a Python function that groups similar records from a list of dictionaries using fuzzy matching. It utilizes the rapidfuzz package for high-performance string similarity scoring.


📦 Dependencies

Install with:

pip install rapidfuzz

🚀 Function Signature

groupbyScore(all_data, score, key)

Parameters

  • all_data (List[dict]): A list of dictionaries to be grouped.
  • key (Callable): A function returning a tuple of values (e.g., lambda x: (x["Notes"], x["page_num"])) used to compare and group.

⚙️ How It Works

  1. Iterates over all item pairs in the dataset.

  2. Compares each field in the key(...) tuple:

    • Strings: fuzz.partial_ratio > 80
    • Integers: must match exactly
  3. Groups items where all key fields match.

  4. Yields:

    • the group key
    • the list of matching items

✅ Example Usage

from groupbyScore.groupbyScore import groupbyScore

data = [
    {"Notes": "Chest Pain", "page_num": 1},
    {"Notes": "chest pain", "page_num": 1},
    {"Notes": "church", "page_num": 1},
    {"Notes": "Fever", "page_num": 2},
    {"Notes": "feverish", "page_num": 2}
]
# TUPLE
for key, group in groupbyScore(data, key=lambda x: (x["Notes"], x["page_num"]),score=90):
    print("Group Key:", key)
    print("Group Items:", group)
# STR
for key, group in groupbyScore(data, key=lambda x: x["Notes"],score=90):
    print("Group Key:", key)
    print("Group Items:", group)

📤 Example Output

#### OUTPUT 1
Group Key: ('Chest Pain', 1)
Group Items: [{'Notes': 'Chest Pain', 'page_num': 1}, {'Notes': 'chest pain', 'page_num': 1}]

Group Key: ('church', 1)
Group Items: [{'Notes': 'church', 'page_num': 1}]

Group Key: ('Fever', 2)
Group Items: [{'Notes': 'Fever', 'page_num': 2}, {'Notes': 'feverish', 'page_num': 2}]

#### OUTPUT 2
Group Key: Chest Pain
Group Items: [{'Notes': 'Chest Pain', 'page_num': 1}, {'Notes': 'chest pain', 'page_num': 1}]

Group Key: church
Group Items: [{'Notes': 'church', 'page_num': 1}]

Group Key: Fever
Group Items: [{'Notes': 'Fever', 'page_num': 2}, {'Notes': 'feverish', 'page_num': 2}]

📎 GitHub

🔗 https://github.com/postboxat18/groupbyScore.git

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

groupbyscore-0.0.5.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

groupbyscore-0.0.5-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file groupbyscore-0.0.5.tar.gz.

File metadata

  • Download URL: groupbyscore-0.0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for groupbyscore-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a4880d690bcf8b0f4056eae11098680e1e39ed700a160e8d6bf09afe690137cb
MD5 9db3ad114cfb84cec2197f8ee108aab4
BLAKE2b-256 550ef742110d83c9bc4858c4c0d813b28b79f6a835259b6a037028e30900296c

See more details on using hashes here.

File details

Details for the file groupbyscore-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: groupbyscore-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for groupbyscore-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9d4b98f0bd696c39991e655473b17d16d708326f585ae3dab9f34b5e29bd5289
MD5 6d6f3a690461b8dbb83bd2f76d9a91b8
BLAKE2b-256 0fcc83bb58d967c33005e8e87098ab159adfb2b9b70db461a4be7ec05b9bd414

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page