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.