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$
3
  • $\begingroup$ Cases[{en}, Entity[a_, b_] :> b] works $\endgroup$
    – TimRias
    Jul 14, 2017 at 11:39
  • $\begingroup$ Related: reference.wolfram.com/language/ref/CanonicalName.html $\endgroup$
    – Greg Hurst
    Jul 17, 2017 at 4:43
  • $\begingroup$ That is not restricted to Entity that is the usual behavior of Cases, for example, for an undefined variable b, Cases[b,b] outputs {}. The fix-> Cases[en, Entity[a_, b_] :> b, All] $\endgroup$ Nov 26, 2022 at 16:56

1 Answer 1

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"}.

$\endgroup$
2
  • $\begingroup$ Thank you! I knew it must be something stupid simple.... $\endgroup$
    – mgamer
    Jul 14, 2017 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, 2017 at 13:18

Your Answer

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

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