Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make speakable descriptions of objects #5

Open
psychemedia opened this issue Aug 29, 2024 · 1 comment
Open

Make speakable descriptions of objects #5

psychemedia opened this issue Aug 29, 2024 · 1 comment

Comments

@psychemedia
Copy link
Contributor

psychemedia commented Aug 29, 2024

Can we make natural language descriptions of objects?

# via claude.ai and chatgpt, plus a bit of me...
def speakable(obj, is_nested=False):
    if isinstance(obj, dict):
        return speakable_dict(obj, is_nested)
    elif isinstance(obj, list):
        return speakable_list(obj)
    elif isinstance(obj, tuple):
        return speakable_tuple(obj)
    elif isinstance(obj, set):
        return speakable_set(obj)
    elif isinstance(obj, str):
        return f'"{obj}"'
    elif isinstance(obj, bool):
        return f'is a boolean {obj}'
    elif isinstance(obj, int):
        return f'is an integer {obj}'
    elif isinstance(obj, float):
        return f'is a float {obj}'
    elif obj is None:
        return "None"
    else:
        return f'an object of type {type(obj).__name__} made up of {obj}'

def speakable_dict(d, is_nested=False):
    result = []
    result.append(f"is a dictionary with {len(d)} keys: ")
    
    for i, (key, value) in enumerate(d.items()):
        result.append(f'key "{key}" {speakable(value, is_nested=True)}')
        if i < len(d) - 1:
            result.append(", ")
    _nested = "nested" if is_nested else ""
    result.append(f"; and that's it for the {_nested} dict")
    
    return " ".join(result)

def speakable_list(lst):
    result = [f"is a list with {len(lst)} items: "]
    result.append(", ".join(speakable(item, is_nested=True) for item in lst))
    result.append("; and that's it for the list")
    return " ".join(result)

def speakable_tuple(tpl):
    result = [f"is a tuple with {len(tpl)} items: "]
    result.append(", ".join(speakable(item, is_nested=True) for item in tpl))
    result.append("; and that's it for the tuple")
    return " ".join(result)

def speakable_set(st):
    result = [f"is a set with {len(st)} items: "]
    result.append(", ".join(speakable(item, is_nested=True) for item in st))
    result.append("; and that's it for the set")
    return " ".join(result)

Then eg:

arb_dict={"name":"john", "age":21, "likes":["dogs", "cats"], kids:{"sons":[adam}, deceased: True}

and: speakable(arb_dict) or f"arb_dict is {speakable(arb_dict)}".

To get the variable name, we'd need some hacky trickery; eg something like https://github.com/pwwang/python-varname

@psychemedia
Copy link
Contributor Author

Also worth remembering https://github.com/python-humanize/humanize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant