You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can we make natural language descriptions of objects?
# via claude.ai and chatgpt, plus a bit of me...defspeakable(obj, is_nested=False):
ifisinstance(obj, dict):
returnspeakable_dict(obj, is_nested)
elifisinstance(obj, list):
returnspeakable_list(obj)
elifisinstance(obj, tuple):
returnspeakable_tuple(obj)
elifisinstance(obj, set):
returnspeakable_set(obj)
elifisinstance(obj, str):
returnf'"{obj}"'elifisinstance(obj, bool):
returnf'is a boolean {obj}'elifisinstance(obj, int):
returnf'is an integer {obj}'elifisinstance(obj, float):
returnf'is a float {obj}'elifobjisNone:
return"None"else:
returnf'an object of type {type(obj).__name__} made up of {obj}'defspeakable_dict(d, is_nested=False):
result= []
result.append(f"is a dictionary with {len(d)} keys: ")
fori, (key, value) inenumerate(d.items()):
result.append(f'key "{key}" {speakable(value, is_nested=True)}')
ifi<len(d) -1:
result.append(", ")
_nested="nested"ifis_nestedelse""result.append(f"; and that's it for the {_nested} dict")
return" ".join(result)
defspeakable_list(lst):
result= [f"is a list with {len(lst)} items: "]
result.append(", ".join(speakable(item, is_nested=True) foriteminlst))
result.append("; and that's it for the list")
return" ".join(result)
defspeakable_tuple(tpl):
result= [f"is a tuple with {len(tpl)} items: "]
result.append(", ".join(speakable(item, is_nested=True) foritemintpl))
result.append("; and that's it for the tuple")
return" ".join(result)
defspeakable_set(st):
result= [f"is a set with {len(st)} items: "]
result.append(", ".join(speakable(item, is_nested=True) foriteminst))
result.append("; and that's it for the set")
return" ".join(result)
Can we make natural language descriptions of objects?
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
The text was updated successfully, but these errors were encountered: