5
$\begingroup$

When extracting the "name" of an Entity one has CommonName. Good. Before I discovered this, I simply applied a rule like:

en = Entity["Language", "English"];
en /. Entity[a_, b_] :> b (* out: "English"*) 

Maybe I´m missing something stupid, but why does the following not work?

Cases[en, Entity[a_, b_] :> b] (* out: {} *) 
$\endgroup$
6
$\begingroup$

You need to specify the levelspec as {0} using the third argument of Cases:

Cases[en, Entity[a_, b_] :> b, {0}]
(* or Cases[en, x_Entity:> Last[x], {0}] *)

{"English"}

Notes: from Cases >> Details and Options

enter image description here

enter image description here

enter image description here

The last one explains why Cases[{en}, Entity[a_, b_] :> b] and, more generally, Cases[foo[en], Entity[a_, b_] :> b] gives {"English"}.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you! I knew it must be something stupid simple.... $\endgroup$ – mgamer Jul 14 '17 at 13:14
  • $\begingroup$ @mgamer, my pleasure. Thank you for the accept. It is easy to forget the three argument form of Cases:) $\endgroup$ – kglr Jul 14 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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