Message Boards Message Boards

0
|
8830 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Filtering Entity lists?

Posted 8 years ago

Desired output: a list of entities from the same class that have been filtered (e.g., they aren't missing "Image", they have a "Length" > 30 ft., or they have a "BirthDate" ? 1950).

I can accomplish this by retrieving one entity at a time, testing it, and adding it to a list if it passes the test:

dinos = {};
While[Length[dinos] < 4,
  tryDino = RandomChoice[EntityList[EntityClass["Dinosaur", "DinosaurSpecies"]]];
  If[! (MemberQ[dinos, tryDino] ||  MissingQ[EntityValue[tryDino, "Image"]]), 
   dinos = Append[dinos, tryDino]]];
{dinos}

In this case, I get four dinosaurs that have images, which is what I want. But I get the feeling that there is a better way to do this, something along the lines of "Get a list four dinosaurs that have images" rather than "Get one dinosaur, and if it has an image, add it to the list." I know how to use the RandomSample[] function. Is there a way to stipulate when querying the database that the entities should have certain properties or property values?

(By the way, I did search this community for answers first and found a discussion about dwarf planets that seemed to answer the question. On further investigation, though, I was unable to apply that solution to my situation.)

Thank you in advance,

Mark Greenberg

POSTED BY: Mark Greenberg
3 Replies

The best way to filter entities is to use "Implicit Entities". The documentation has this to say about them:

Property values in implicitly defined entity classes may make use of Quantity (and intervals of Quantity) for dimensional values, DateObject for dates, TakeLargest and TakeSmallest for ordinal selections, and ContainsAll, ContainsExactly, ContainsAny, ContainsOnly, ContainsNone for entities...

So you can filter things like dates and quantities since they can be compared. You can also filter by set membership.

EntityValue["Dinosaur", "Properties"]

{"location", "diet", "discovery country", "discovery year", \
"etymology", "image", "known habitat", "full length", "name", "other \
members of", "parent entity", "period", "pronunciation", "scientific \
name", "sub entities", "taxonomic level", "taxonomic sequence", "time \
span", "weight"}

Here are the ten longest dinosaurs:

EntityList[EntityClass["Dinosaur", {"Length" -> TakeLargest[10]}]]

Here are all dinosaurs over a certain length

largeDinosaurs = 
EntityList@
 EntityClass[
  "Dinosaur", {"Length" -> GreaterThan[Quantity[50, "Feet"]]}]

Once you have a list of them, you can get random samples from that list:

RandomSample[largeDinosaurs, 4]

Or you can run RandomEntity on the EntityClass directly:

RandomEntity@
 EntityClass[
  "Dinosaur", {"Length" -> GreaterThan[Quantity[50, "Feet"]]}]

Currently, you can't use implicit entities to filter out entities that do not have images. If I wanted to randomly get dinosaurs which had images, I would randomly sample dinosaurs and throw out ones that didn't have images. Clean code for doing that would look like this:

SetAttributes[rndSelect, HoldAllComplete]
rndSelect[rndfunc_, pred_] := NestWhile[rndfunc &, rndfunc, Not@*pred];

rndSelect[RandomEntity["Dinosaur"], ImageQ[#["Image"]] &]

I first define a function called rndSelect. The argument is some code that produces random output. The second argument is a function which returns True when I want to keep the random output and false when I want to ignore it.

POSTED BY: Sean Clarke
Posted 8 years ago

Thank you for your reply. This is exactly what I needed.

I had seen mention of "Implicit Entities" in the docs, but, being somewhat overwhelmed by the learning curve as I teach myself Wolfram Language, I did not recognize it as the solution to my question. The last part of your answer is still mysterious and magical to me, but I'll get it eventually.

Thanks again,

Mark

POSTED BY: Mark Greenberg
POSTED BY: Arben Kalziqi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract