Skip to main content

A class that allows retrieval of a given object by any of its synonyms

Project description

synonym_dict

A class that allows retrieval of a given object by any of its synonyms.

Build StatusCoverage Status

Overview

There are many situations in which an object may be known by several names. synonym_dict provides a way to:

  1. Retrieve an object by its name or any synonyms
  2. Ensure that synonyms are distinct and non-overlapping
  3. Support case-insensitive tests

Installation

$ pip install synonym_dict

The package has no dependencies.

Testing

$ python -m unittest

Or, on python2:

$ python -m unittest discover

Code Design

SynonymSet

A SynonymSet a set of synonyms called "terms" in a hashable collection. Its "name" is canonically its first term, but can be set to any term in the collection. It can also have child objects, all of whose terms are taken to be synonyms.

# from TestSynonymSet.test_name()
s = SynonymSet('hello', 'aloha', 'Ni hao')
assert str(s) == 'hello'
assert s.object == 'hello'
s.set_name('aloha')
assert s.object == 'aloha'

Each synonym set can represent a particular object, such that the terms are synonymous names for that object. The object for the base SynonymSet is simply the name of the set, but subclasses can override this.

SynonymDict

# from TestSynonymDict.test_explicit_merge()
g = SynonymDict(ignore_case=False)  # default
g.new_entry('hello', 'hola', 'hi', 'aloha')
g.new_entry('Hello', 'HELLO', 'Hi', 'HI')
assert g['hi'] == 'hello'
assert g['HI'] == 'Hello'
g.merge('hi', 'HI')
assert g['HI'] == 'hello'

A SynonymDict is a typed collection of SynonymSets or subclasses, each of which is called an entry. The SynonymDict is responsible for managing the set of terms and preventing collisions. It can be case-sensitive or case-insensitive.

A key functionality of the dict is in combining entries. When creating a new entry, the dict first checks to see if any terms are already assigned to an existing entry. If they are, the merge strategy determines what to do among the choices of "merge", "prune", or "strict":

  • The default is to merge the terms into the existing entry. This fails with MergeError if the incoming terms match two or more entries.
  • If "prune" is specified, the duplicate terms are removed from the new entry and it is created using only unknown terms.
  • If neither "merge" nor "prune" are specified, the new entry is created only if every term is unknown; otherwise a TermExists error is raised.

LowerDict

d = LowerDict()
d['smeeb'] = 42
assert d['   SMeeB '] == 42
d[' dRoOl '] = 17
assert d['drool'] == 17
assert list(d.keys()) == ['smeeb', 'dRoOl']

A simple dict subclass that implements case-insensitivity. Also strips leading and trailing whitespace. Used to implement case-insensitivity in SynonymDicts

Subclasses

The main utility of these classes comes in subclassing. The standard approach is to create a subclass of SynonymSet that describes an object of some sort, and then to subclass SynonymDict to manage the set of entries. Two examples are provided and tested and will someday be documented.

Contributing

Fork or open an issue! Please! I crave critical appraisals of my design and/or implementation decisions.

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

synonym_dict-0.1.5.post0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

synonym_dict-0.1.5.post0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file synonym_dict-0.1.5.post0.tar.gz.

File metadata

  • Download URL: synonym_dict-0.1.5.post0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.6

File hashes

Hashes for synonym_dict-0.1.5.post0.tar.gz
Algorithm Hash digest
SHA256 077720de67eb65874df618af3ab2fdaad3de695bf39ee0e1c3722c089943aa30
MD5 83df03fd623b9ea47747a68e971a44b7
BLAKE2b-256 c4519a98341adce83c43d56a815226a898311d4f27fe0a26a00f0ed58ca50ead

See more details on using hashes here.

File details

Details for the file synonym_dict-0.1.5.post0-py3-none-any.whl.

File metadata

  • Download URL: synonym_dict-0.1.5.post0-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.6

File hashes

Hashes for synonym_dict-0.1.5.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 b185a6457fbb6da95df11c51ffbc5e34776517830e429e704fce3c34c0dd1a06
MD5 43c0e7a46bacf94534357108a3f87021
BLAKE2b-256 3aa965a83e55accae9b3291d1ff8d0d2f6e4cdcab2a03e34ade4e8a6ab5dfb27

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