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
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
- Python 3.6+
rapidfuzz
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
-
Iterates over all item pairs in the dataset.
-
Compares each field in the
key(...)tuple:- Strings:
fuzz.partial_ratio> 80 - Integers: must match exactly
- Strings:
-
Groups items where all key fields match.
-
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file groupbyscore-0.0.6.tar.gz.
File metadata
- Download URL: groupbyscore-0.0.6.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f0b0621cb04e4f37f6de7d61126b11602ba67428b8eeadd8f387bb4ca307843
|
|
| MD5 |
b6291c461ce17c7d9babb3fe2b934471
|
|
| BLAKE2b-256 |
d7b6212ac71c55a3a4fb8939b4a6f4c700be79fc81b595817e4838711f50eb15
|
File details
Details for the file groupbyscore-0.0.6-py3-none-any.whl.
File metadata
- Download URL: groupbyscore-0.0.6-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d063a20e95bee885d4a240f41a665abc559870ed40f45cc2c4df2f7e5643e9d
|
|
| MD5 |
b28bc44f61e63ebeedc393d1aa228f1d
|
|
| BLAKE2b-256 |
c56dfe7711dac4107b048263f37ac3e40dcbf2cf2ca38d07845cfdc890d1c97b
|