[WSG20] Wolfram Language Basics from EIWL (Days 6, 7, 8, 9)

During week two of the Wolfram Study Group Apr 2020 (WSG20) we are looking at the following topics from Stephen Wolfram’s book Elementary Introduction to the Wolfram Language:

Day 6: Chapter 16 Real-World Data, Chapter 35: Natural Language Understanding, Chapter 44: Importing and Exporting

Day 7: Chapter 5: Operations on Lists, Chapter 13: Arrays, or Lists of Lists, Chapter 30: Rearranging Lists, Chapter 31: Parts of Lists

Day 8: Chapter 34: Associations, Chapter 45: Datasets

Day 9: Chapter 27: Applying Functions Repeatedly, Chapter 29: More about Pure Functions, Chapter 46: Writing Good Code, Chapter 47: Debugging Your Code

We are looking at videos from the Wolfram U interactive course on EIWL and also working on simple exercises and mini projects.

Feel free to post questions on the material we covered in these sessions here.

2 Likes

Daily challenge (Day 6):

List the EntityProperties for the “Country” entity that have a name beginning with the letter “w”? How many did you find?

Entity[“Country”, “WestBank”][“Properties”]
Entity[“Country”, “WesternSahara”][“Properties”]

The following 9:

{EntityProperty["Country", "WageAndSalariedWorkers"], 
 EntityProperty["Country", "WageAndSalariedWorkersFraction"], 
 EntityProperty["Country", "WagesCostIndex"], 
 EntityProperty["Country", "WaterArea"], 
 EntityProperty["Country", "WaterArrivals"], 
 EntityProperty["Country", "WaterProductivity"], 
 EntityProperty["Country", "WaterwayLength"], 
 EntityProperty["Country", "Workforce"], 
 EntityProperty["Country", "WPI"]}

Thank you @Abrita Chakravarty. I have done suggested modifications.

I have found 7 properties beginning with small letter “w”, by Common Names:

In[1]:= Select[CommonName /@ EntityProperties["Country"], StringMatchQ[#, "w" ~~ __] &]

Out[1]= {"wage and salaried workers",
          "wage and salaried workers fraction",
          "wage cost index",
          "water area",
          "water productivity",
          "waterway length",
          "wholesale price index"}

and 9, by Canonical Names:

In[2]:= Select[CanonicalName /@ EntityProperties["Country"], StringMatchQ[#, "W" ~~ __] &]

Out[2]= {"WageAndSalariedWorkers",
         "WageAndSalariedWorkersFraction",
         "WagesCostIndex",
         "WaterArea",
         "WaterArrivals",
         "WaterProductivity",
         "WaterwayLength",
         "Workforce",
         "WPI"}

2 Canonical Names have Common Names that do not start with letter “w”.

It looks so that the correct result is 7, as the formulation ask for names starting with the small letter “w”..

There are nine

 Entity["Country"]["Properties"] // 
   Select[ToLowerCase[StringTake[CanonicalName@#, 1]] == "w" &]

(*
{EntityProperty["Country", "WageAndSalariedWorkers"], 
 EntityProperty["Country", "WageAndSalariedWorkersFraction"], 
 EntityProperty["Country", "WagesCostIndex"], 
 EntityProperty["Country", "WaterArea"], 
 EntityProperty["Country", "WaterArrivals"], 
 EntityProperty["Country", "WaterProductivity"], 
 EntityProperty["Country", "WaterwayLength"], 
 EntityProperty["Country", "Workforce"], 
 EntityProperty["Country", "WPI"]}
*)
1 Like

Hi Valeriu,

The question asks for ‘beginning with the letter “w”’. You need to anchor the pattern to the start of the string or it will match “w” anywhere in the name.

StartOfString ~~ "w" | "W" ~~ ___

@Moses Paul Thanks for your reply. We are looking for EntityProperties that start with “w”, not Country Entities. Try:

EntityProperties["Country"]

@Muhammad Ali Thanks for your reply. Can you also post the code you used to get to this answer?

@Valeriu Ungureanu Thanks for your reply. You want to modify the pattern a tad bit - so only property names starting with “w” are caught.

@Rohit Namjoshi Nice one.
[at0]: https://community.wolfram.com/web/iammosespaulr

1 Like

@Abrita Chakravarty

Cases[
	EntityProperties["Country"],
	x_/;Or@@StringMatchQ[
		{CanonicalName[x],CommonName[x]},
		"W*",
		IgnoreCase->True
	]
]
1 Like

Hi Muhammad,

There is a difficulty with your code. I have executed it:

In[61]:= Cases[EntityProperties["Country"], 
 x_ /; Or @@ 
   StringMatchQ[{CanonicalName[x], CommonName[x]}, "W*", 
    IgnoreCase -> True]]

Out[61]= {EntityProperty["Country", "WageAndSalariedWorkers"], 
 EntityProperty["Country", "WageAndSalariedWorkersFraction"], 
 EntityProperty["Country", "WagesCostIndex"], 
 EntityProperty["Country", "WaterArea"], 
 EntityProperty["Country", "WaterArrivals"], 
 EntityProperty["Country", "WaterProductivity"], 
 EntityProperty["Country", "WaterwayLength"], 
 EntityProperty["Country", "Workforce"], 
 EntityProperty["Country", "WPI"]}

and found that for 2 of your results their Common Names do not start with small letter “w”. So, for

“WaterArrivals” the Common Name is “arrivals by sea”

and for

“Workforce” the Common Name is “employment”

It looks so that the code must be modified because, according to formulation, property names must begin with small letter “w”. At least, such observation did @Abrita Chakravarty

Thank you, Rohit, for your suggestion!

Here’s another solution:

StringCases[#[[2]] & /@ EntityProperties["Country"], 
  StartOfString ~~ "w" ~~ ___, IgnoreCase -> True] // Flatten

Daily Challenge (Day 7):

This one is from EIWL Chapter 31–
Make a histogram of where the letter “e” occurs in the words in WordList[ ]

Small or capital “w”. But good catch - interesting that “WaterArrivals” and “Workforce” have different common names. For this challenge all canonical names beginning with “w” should be accepted.

1 Like

@Valeriu Ungureanu In my experience when working with Entities it is advisable to look for both CanonicalName and CommonName. CanonicalName is the actual field name in the database so in many databases they are kept as short and cryptic to avoid naming conflicts. As an example, if you create your own SQL database and map it onto the EntityFramework using RelationalDatabase, you will see that database field names are mapped to CanonicalName. If you restrict your searching to CanonicalName only you will miss out on many relevant properties.

Regarding the question, it didn’t specify what name it was referring to (that’s why I did an exhaustive search using the Or in my code) but I agree one can become pedantic and take “w” as a literal for the lower case. But I don’t think that was the intention of the question.

1 Like

Agree!

My code for today (day 7) challenge:

Last /@ Position[Characters@WordList[], "e"] // Histogram
1 Like

1 Like

A shorter code:

Histogram@Flatten@StringPosition[WordList[], "e"]

With such code, day 7 challenge becomes a simple problem :slight_smile:

Hi Valeriu,

The counts are doubled in this solution because StringPosition returns a pair of numbers.