Skip to main content

`fuzzy-multi-dict` is a module that provides a hight-flexible structure for storing and accessing information by a string key.

Project description

fuzzy-multi-dict

Coverage Status Coverage Status

Coverage Status Coverage Status

fuzzy-multi-dict is a module that provides a hight-flexible structure for storing and accessing information by a string key.

Fuzzy: access by key is carried out even if there are mistakes (missing/extra/incorrect character) in the string representation of the key.

Multi: flexible functionality for updating data on an existing key.

Installation

pip install fuzzy_multi_dict

Quickstart

Module can be used as a fast enough (due to the tree structure of data storage) spell-checker.

import re
from fuzzy_multi_dict import FuzzyMultiDict

with open('big_text.txt', 'r') as f:
    words = list(set(re.findall(r'[a-z]+', f.read().lower())))
    
vocab = FuzzyMultiDict(max_corrections=3)
for word in words:
    vocab[word] = word
    
vocab['responsibilities']
# 'responsibilities'

vocab['espansibillities']
# 'responsibilities'

vocab.get('espansibillities')
# [{'value': 'responsibilities',
#   'key': 'responsibilities',
#   'mistakes': [{'mistake_type': 'missing symbol "r"', 'position': 0},
#    {'mistake_type': 'wrong symbol "a": replaced on "o"', 'position': 3},
#    {'mistake_type': 'extra symbol "l"', 'position': 10}]}]

It can also be used as a flexible structure to store and access semi-structured data.

from fuzzy_multi_dict import FuzzyMultiDict

def update_value(x, y):
    
    if x is None: return y
    
    if not isinstance(x, dict) or not isinstance(y, dict):
        raise TypeError(f'Invalid value type; expect dict; got {type(x)} and {type(y)}')
        
    for k, v in y.items():
        if x.get(k) is None: x[k] = v
        elif isinstance(x[k], list):
            if v not in x[k]: x[k].append(v)
        elif x[k] != v: x[k] = [x[k], v]
            
    return x

phone_book = FuzzyMultiDict(
    max_corrections=3, 
    update_value_func=update_value
)

phone_book['Mom'] = {'phone': '123-4567', 'organization': 'family'}
phone_book['Adam'] = {'phone': '890-1234', 'organization': 'work'}
phone_book['Lisa'] = {'phone': '567-8901', 'organization': 'family'}
phone_book['Adam'] = {'address': 'baker street 221b'}
phone_book['Adam'] = {'phone': '234-5678', 'organization': 'work'}

phone_book['Adam']
# {'phone': ['890-1234', '234-5678'],
#  'organization': 'work',
#  'address': 'baker street 221b'}

It can also be used for indexing data and fuzzy-search.

from fuzzy_multi_dict import FuzzyMultiDict

d = FuzzyMultiDict()

d["apple"] = "apple"
d["apple red delicious"] = "apple red delicious"
d["apple fuji"] = "apple fuji"
d["apple granny smith"] = "apple granny smith"
d["apple honeycrisp"] = "apple honeycrisp"
d["apple golden delicious"] = "apple golden delicious"
d["apple pink lady"] = "apple pink lady"

d.get("apple") 
# [{'value': 'apple', 'key': 'apple', 'correction': [], 'leaves': []}]

d.search("apple") 
# ['apple', 'apple red delicious', 'apple fuji', 'apple granny smith',
#  'apple golden delicious', 'apple honeycrisp', 'apple pink lady']

d.search("apl") 
# ['apple', 'apple red delicious', 'apple fuji', 'apple granny smith', 
#  'apple golden delicious', 'apple honeycrisp', 'apple pink lady']

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

fuzzy_multi_dict-0.0.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

fuzzy_multi_dict-0.0.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fuzzy_multi_dict-0.0.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for fuzzy_multi_dict-0.0.5.tar.gz
Algorithm Hash digest
SHA256 61bd5e96a0801dfd864b8ac58e418180f0b8f2bc0c9e83f6a59884e18c2deb54
MD5 69dafeeca6c6661effe5bf09b0a1ce65
BLAKE2b-256 15ca0e7e600eceb5c5bbffd534fbc7b24c5905bf38d9d24c56ae292eba546459

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fuzzy_multi_dict-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3a3f526b4718f22de7ab0f3eb1b227e7196fb592187b5d0821d23993fc2412
MD5 9e8b950490d70ecc6211ad23e5aeca46
BLAKE2b-256 bf6e14b9f22cf81c71892b6ea47c182123115e1771fc9250fb6852a25c1719cb

See more details on using hashes here.

Supported by

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