Message Boards Message Boards

1
|
3301 Views
|
0 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to extract the WL commands from a demonstrations notebook?

Posted 9 years ago

I have been trying for some time to compare my WL code with e.g. the code of all the Wolfram demonstrations. Thanks to WolframLanguageData in v10, I finally made my attempt. I am asking other community members if they can check if I am right or make suggestions for improvement. First, I made a list of all the WL command frequencies in all of the Wolfram Demonstrations:

allWLCommandFrequencies =(*get all frequencies from several domains*)

    WolframLanguageData[All, {"CanonicalName", "Frequencies"}];
allDemoCommandFrequencies =(*get those from the Demonstrations only \
and convert to %*)
 Reverse@SortBy[
   DeleteCases[
    allWLCommandFrequencies /. {symbol_, {__, 
        Rule["WolframDemonstrations", frequency_], __}} :> {symbol, 
       100*frequency}, {_, Missing["NotAvailable"]}], Last]

To get the frequencies of my own demos, I made the function: commandQ[wordstring]

(*all WL symbol names*)
WLdat = WolframLanguageData[];
(*True if a string is a WL symbol name: false if not*)

commandQ[word_String] := 
 MemberQ[WLdat, Entity["WolframLanguageSymbol", word]]

and a function: getWLCommands[filepath], to extract the WL commands from my own demo files (maybe this should be improved??)

getWLCommands[
  filepath_] :=(*enter filepath of a demonstration -author or -source \
nb;
 imports an author notebook as text & gets the WL commands out of it*)
 Quiet@Module[{fileText, codeText, start, stop, codeString, codeWords},
   (*import the ademo author notebook file as a string"*)

   fileText = Import[filepath, "Plaintext"];
   (*detect the Manipulate beginning and end*)
   start = StringPosition[fileText, "Manipulate"][[1, 1]](*-1*);
   stop = StringPosition[fileText, "-Dynamic-"][[1, 1]] - 1;
   (*select only the Manipulate code part*)

   codeText = StringTrim@StringTake[fileText, {start, stop}];
   (*make new string with FullForm expressions*)

   codeString = ToString[FullForm[ToExpression@codeText]];
   (*split into code words*)

   codeWords = 
    StringTrim /@ 
     StringSplit[
      codeString, {"[", "]", ","}];(*select WL commands only*)

   Select[codeWords, commandQ]]

Then released it on the list of my demonstration notebook file paths:

myDemoCommands = 
  Table[getWLCommands[myDemoFileNames[[i]]], {i, 1, 
    Length@myDemoFileNames}];
myDemoCommandTally = 
  TakeLargestBy[Tally[Flatten@myDemoCommands], Last, UpTo[1000]];
myDemoCommandFrequencies = 
  myDemoCommandTally /. {s_, t_} -> {s, 
     100. t/Leng@Flatten[myDemoCommands]};

The data from {myDemoCommandFrequencies,allDemoCommandFrequencies} enabled me to make several comparisons like e.g this barchart of my 16 most frequently used commands

enter image description here

If my function getWLCommands is correct, one could use this to analyze one's own coding style and compare it to the WolframLanguageData database or other people's coding style? Of course, usage frequency of WL commands is only one aspect and maybe sequences of commands should also be investigated. But first, I need to know if my command-extraction method is correct...

Attachments:
POSTED BY: Erik Mahieu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract