5
$\begingroup$

I was trying to find all the Mathematica functions listed in the index of the Elementary Introduction to the Wolfram Language.

I just cut and pasted everything to a text file and then strung together the ugliest nested Select statement I could based on predicates I found by searching:

f = Import["/Users/joe/Documents/z school/mma elementary index.txt"];
firstLetterCapital[x_] := First[ToCharacterCode[x]] < 91
fStringSplitAtSpace = StringSplit[f];
fWordsOnly = Select[
  DeleteDuplicates[
   Select[
    Select[fStringSplitAtSpace, DictionaryWordQ], 
    StringStartsQ[LetterCharacter]
    ]
   ]
  , firstLetterCapital]

I'm not winning any prizes for the above but it kinda-mostly works.

My question: is there a straight forward way to find the intersection of the Global rule base(?) with this list? or string yet another Select function with a say, WolframFunctionNameQ predicate?

Of course, more elegant methods of getting there would be appreciated.

$\endgroup$
5
$\begingroup$

A more rigours way to get function names is to examine the html elements. Here I assumed the bold text of functions are intended functions, so things like Alignment is not included.

data = Import["https://www.wolfram.com/language/elementary-introduction/2nd-ed/book-index.html", "Source"];
Intersection[
 DeleteDuplicates[
  StringCases[data, 
   "<span class=\"FunctionName\">" ~~ Shortest[fun__] ~~ "</span>" :> 
    fun]], Names["System`*"]]
$\endgroup$
  • $\begingroup$ thanks - with my xp level, this answer makes sense. also, that garden path that i went down set me up to think that it should be a simple Intersection question so your answer was a perfect moment for me to learn about Names. I have no idea what MB1965 is doing... $\endgroup$ – Joe May 24 '17 at 5:29
  • $\begingroup$ @Joe more or less this but I applied a StringCases to pull a more compact set of function names so that we don't get anything that's in the headers or footers of the page. I used Cases instead of Intersection. $\endgroup$ – b3m2a1 May 24 '17 at 5:35
  • $\begingroup$ @happyfish bizarre I get Abs fine. Did you have text = Import["https://www.wolfram.com/language/elementary-introduction/2nd-ed/book-index.html"];? I forgot to include that line it seems. Also sans duplicates I have 594. $\endgroup$ – b3m2a1 May 24 '17 at 5:51
  • $\begingroup$ @happyfish interestingly your method returns 642 unique functions. I wonder how many of those are ones my rather-string StringCases missed and how many come from the header and footer and things. I'm betting most are in the former set. $\endgroup$ – b3m2a1 May 24 '17 at 5:57
  • $\begingroup$ @happyfish I think you should stick a StringTrim[__, Except[("$"|WordCharacter)]..] in your code. I applied some StringCases to drop the footer and header and with that plus the string trim I get some 647 functions (vs 636 without the trim). And using a cleaner (I think) version of my code I get 597 there. $\endgroup$ – b3m2a1 May 24 '17 at 6:07
3
$\begingroup$

I think you'll be better served by taking the data you can find from Names:

rawText = 
  Import["https://www.wolfram.com/language/elementary-introduction/2nd-ed/book-index.html"];
text =
  StringSplit[
    StringCases[rawText, "Index" ~~ Whitespace ~~ "\n" ~~ __][[1]],
    "\n\n"
    ][[1]];
StringCases[
    StringSplit[
     text,
     "\n"
     ],
    (func : (("$" | "") ~~ WordCharacter ..) ~~ ___ ~~ NumberString) :> 
     func
    ] // Flatten // Cases[Alternatives @@ Names["System`*"]] // Sort

I get about 597 unique functions from that.


Mentioned this in a comment to happyfish but rather than parsing the HTML it is often nicer to use an XMLObject representation:

xml = Import[
   "https://www.wolfram.com/language/elementary-introduction/2nd-ed/book-index.html", {"HTML", "XMLObject"}
   ];

Intersection[
  Cases[
     xml,
     XMLElement[_, {___, "class" -> "FunctionName", ___}, f_] :> f,
     \[Infinity]
     ] // Flatten // DeleteDuplicates,
  Names["System`*"]
  ]
$\endgroup$
1
$\begingroup$

In:

url = "https://www.wolfram.com/language/elementary-introduction/2nd-ed/book-index.html";
xmlElements[url_] := Import[url, "XMLObject"];
functionElements[xes_] := Position[xes, XMLElement["span", {"class" -> "FunctionName"},{_String}]] // Extract[xss, #] &
systemFunctionNames[xss_] := Map[#[[3]] &, xss] // Union // Flatten // Intersection[#,Names["System`*"]] &
importSystemFunctionNames[url_] := Composition[systemFunctionNames, functionElements, xmlElements][url]
importSystemFunctionNames[url]
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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