10
$\begingroup$

The following gives a list of all counties in the state of Maryland.

counties = EntityClass["AdministrativeDivision", "USCountiesMaryland"]
   ["Name"];
Flatten[StringDrop[StringCases[#, __ ~~ ","], -1] & /@ counties]

  (* {Allegany County, Maryland,Anne Arundel County, Maryland,
    Baltimore (independent city), Maryland,Baltimore County,
    Maryland,Calvert County, Maryland,Caroline County, Maryland,
    Carroll County, Maryland,Cecil County, Maryland,Charles County,
    Maryland,Dorchester County, Maryland,Frederick County, Maryland,Garrett
    County, Maryland,Harford County, Maryland,Howard County, Maryland,
    Kent County, Maryland,Montgomery County, Maryland,Prince George's
    County, Maryland,Queen Anne's County, Maryland,Somerset County,
    Maryland,St. Mary's County, Maryland,Talbot County, Maryland,
    Washington County, Maryland,Wicomico County, Maryland,Worcester 
    County, Maryland} *)

The first line is based on code another person wrote. I have seen use of "AdministrativeDivision" and "Name" before. How would I determine that "USCountiesMaryland" is needed? Also, how does one know when EntityClass needs more than one argument?

$\endgroup$
1
  • 1
    $\begingroup$ For discovery, use <kbd>Ctrl</kbd> + <kbd>=</kbd> to bring up the WolframAlpha prompt, and then type in "counties in Maryland". Alternatively, use WolframAlpha["counties in Maryland", "WolframParse"]. As for EntityClass, it always takes at least 2 arguments. $\endgroup$
    – Carl Woll
    Aug 5, 2017 at 1:31

2 Answers 2

9
$\begingroup$

(See also this: http://reference.wolfram.com/language/guide/KnowledgeRepresentationAndAccess.html)

This is all in the docs, but maybe it's good to lay out here.

If you need classes, use EntityClassList:

EntityClassList["AdministrativeDivision"] // CanonicalName // 
 Take[#, 5] &

{"ADM1", "ADM2", "ADM3", "AllUSStatesAndTerritories", \
"AllUSStatesPlusDC"}

If you need properties, use EntityProperties:

EntityProperties["AdministrativeDivision"] // CanonicalName // 
 Take[#, 5] &

{"AccommodationAndFoodServicesSales", "AggravatedAssault", \
"AggravatedAssaultRate", "AggregateHouseholdIncome", "AnnualBirths"}

If you want straight-up entities, use EntityList:

EntityList["Aircraft"] // CanonicalName // Take[#, 5] &

{"RB1", "CAP1", "TSR2", "3ISkyArrow", "3SigmaNearchos"}

Note that EntityList can be applied to a class, too and EntityProperties can be applied to individual entities.

If you want the type name, use EntityTypeName, see also CanonicalName and CommonName

I don't actually know how to get a list of every EntityPropertyClass for an entity type without direct access to the EntityStore

If you want an Association or Dataset of your data, you can specify that to EntityValue. This works even for custom entities provided via EntityStore:

EntityValue[Entity["AirlineSafety", "Aer Lingus"], "Association"]

<|EntityProperty["AirlineSafety", "Label"] -> "Aer Lingus", 
 EntityProperty["AirlineSafety", "Airline"] -> "Aer Lingus", 
 EntityProperty["AirlineSafety", "AvailableSeatKilometersPerWeek"] -> 
  320906734, EntityProperty["AirlineSafety", "Incidents85to99"] -> 2, 
 EntityProperty["AirlineSafety", "FatalAccidents85to99"] -> 0, 
 EntityProperty["AirlineSafety", "Fatalities85to99"] -> 0, 
 EntityProperty["AirlineSafety", "Incidents00to14"] -> 0, 
 EntityProperty["AirlineSafety", "FatalAccidents00to14"] -> 0, 
 EntityProperty["AirlineSafety", "Fatalities00to14"] -> 0|>

Note that this also can be used on types or classes, e.g,

EntityClass["Airline", "Departures" -> GreaterThan[1000000]][
  "Association"] // Map[Take[#, 3] &]

<|Entity["Airline", 
   "SouthwestAirlinesCo::tcv64"] -> <|EntityProperty["Airline", 
     "AircraftFuel"] -> Quantity[1.53*10^9, "Gallons"], 
   EntityProperty["Airline", "AirlineID"] -> "19393", 
   EntityProperty["Airline", "Airtime"] -> 
    Quantity[36319489/20, "Hours"]|>|>
$\endgroup$
8
$\begingroup$

This is what it looks like when using free-form input to get entities (as Carl has already suggested in a comment). To use the hotkey =, first place the cursor in an empty cell. ctrl+= can be used inside expressions. ctrl+= can also be found in the menu under Insert, where it goes by the name Inline Free-form Input.

= counties in Maryland

Free form 1

ctrl= counties in Maryland

enter image description here

$\endgroup$
1
  • $\begingroup$ Very useful answer +1. $\endgroup$
    – Ted Ersek
    Aug 6, 2017 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.