Skip to content

Similar Tags

imgtools.dicom.similar_tags cached

similar_tags(
    keyword: str, n: int = 3, threshold: float = 0.6
) -> typing.List[str]

Find similar DICOM tags for a given keyword.

Useful for User Interface to suggest similar tags based on a misspelled keyword.

Parameters:

Name Type Description Default

keyword

str

The keyword to search for similar tags.

required

n

int

Maximum number of similar tags to return (default is 3).

3

threshold

float

Minimum similarity ratio (default is 0.6).

0.6

Returns:

Type Description
typing.List[str]

A list of up to n similar DICOM tags.

Examples:

Find similar tags for a misspelled keyword:

>>> similar_tags("PatinetID")
['PatientID', 'PatientName', 'PatientBirthDate']

Adjust the number of results and threshold:

>>> similar_tags(
...     "PatinetID",
...     n=5,
...     threshold=0.7,
... )
['PatientID', 'PatientName']
Source code in src/imgtools/dicom/utils.py
@functools.lru_cache(maxsize=1024)
def similar_tags(
    keyword: str, n: int = 3, threshold: float = 0.6
) -> List[str]:
    """Find similar DICOM tags for a given keyword.

    Useful for User Interface to suggest similar tags based on a misspelled keyword.

    Parameters
    ----------
    keyword : str
        The keyword to search for similar tags.
    n : int, optional
        Maximum number of similar tags to return (default is 3).
    threshold : float, optional
        Minimum similarity ratio (default is 0.6).

    Returns
    -------
    List[str]
        A list of up to `n` similar DICOM tags.

    Examples
    --------
    Find similar tags for a misspelled keyword:

    >>> similar_tags("PatinetID")
    ['PatientID', 'PatientName', 'PatientBirthDate']

    Adjust the number of results and threshold:

    >>> similar_tags(
    ...     "PatinetID",
    ...     n=5,
    ...     threshold=0.7,
    ... )
    ['PatientID', 'PatientName']
    """
    return difflib.get_close_matches(keyword, ALL_DICOM_TAGS, n, threshold)