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

Questions about Atomspace with Python #781

Open
nworb999 opened this issue Oct 14, 2024 · 2 comments
Open

Questions about Atomspace with Python #781

nworb999 opened this issue Oct 14, 2024 · 2 comments

Comments

@nworb999
Copy link

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)
  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)?

Thank you for your work!

@vsbogd
Copy link
Collaborator

vsbogd commented Oct 14, 2024

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:

def get_atoms(self):
def query(self, pattern):

  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
Copy link
Author

Thank you so much!

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

2 participants