trueagi-io / hyperon-experimental

MeTTa programming language implementation
https://metta-lang.dev
MIT License
151 stars 49 forks source link

Questions about Atomspace with Python #781

Open nworb999 opened 1 month ago

nworb999 commented 1 month ago

Hello,

I am using the hyperon package for my Python application and I was wondering about some best practices for outputting the AtomSpace to a text file. Right now, I am using metta.run("!(get-atoms &self)") to visually check that things I am adding are being represented correctly. I was wondering:

  1. Is there a better, cleaner, more Pythonic way to dump atoms in a space into a text file?
  2. While dumping atoms into a space in a text file, there's a lot of generated code currently that looks like below, as well as many records of (= True True), (= True True), (= True True), -- is there a way to automatically filter that out? And only include atoms that have information like (Is Hat_Hat2279 Hat), (entity Hat_Hat2280), (Is Hat_Hat2280 Hat)?
    (= ($entity#318764 $rest#415864) ($entity#318764 $rest#318765)),
    (= ($entity#318764 $rest#415864) ($entity#318764 $rest#318765)), 
    (= ($entity#318764 $rest#415864) ($entity#318764 $rest#318765)),
    (= ($entity#318764 $rest#415864) ($entity#318764 $rest#318765)), 
    (= ($entity#318764 $rest#415864) ($entity#318764 $rest#318765)),
    (Is color red), (Is $entity#154287 $subsumption#154288)
  3. Is there a way to filter the space without having to match the exact pattern, e.g. I want any atom that contains the string Necklace, without having to specify (fact $query ($rest))/(fact Necklace (is Brown)) or (entity $query)/(entity Necklace)?

Thank you for your work!

vsbogd commented 1 month ago

Hello @nworb999 ,

  1. Is there a better, cleaner, more Pythonic way to dump atoms in a space into a text file?

If you are running you MeTTa program from Python code using MeTTa runner then you can use MeTTa.space() method to get the top level space and use SpaceRef.get_atoms or SpaceRef.query method to get atoms from the space: https://github.com/trueagi-io/hyperon-experimental/blob/1507f82756ca0766b5e69c8937b3b081b4409301/python/hyperon/runner.py#L78 https://github.com/trueagi-io/hyperon-experimental/blob/1507f82756ca0766b5e69c8937b3b081b4409301/python/hyperon/base.py#L229 https://github.com/trueagi-io/hyperon-experimental/blob/1507f82756ca0766b5e69c8937b3b081b4409301/python/hyperon/base.py#L248

  1. While dumping atoms into a space in a text file, there's a lot of generated code currently that looks like below, as well as many records of (= True True), (= True True), (= True True), -- is there a way to automatically filter that out?

Am I right that these atoms are generated by your MeTTa program? In general they can be filtered out by specifying query to SpaceRef.query() method, or iterating through the collection of atoms and filtering them out in Python according to some criteria. Another approach is to create few separate spaces in your MeTTa program using new-space function from the MeTTa standard library and to keep auto generated code and useful atoms in separate spaces. Then you can save only space with useful data.

  1. Is there a way to filter the space without having to match the exact pattern, e.g. I want any atom that contains the string Necklace, without having to specify (fact $query ($rest))/(fact Necklace (is Brown)) or (entity $query)/(entity Necklace)?

It is possible doing this in Python by converting atom to string and check if the string contains some substring. But it is not poss in MeTTa program without writing additional MeTTa functions for doing these things.

nworb999 commented 1 month ago

Thank you so much!