I want to parse a Mathematica Notebook to get some information regarding the relative use of symbols.
So first things first:
file = "<some-file>.nb";
nb = FullForm[Import[file, "NB"]] (* if there is a better way to import notebook files without having them evaluate I would be happy to hear it.*)
Notebook[{(...341209412...)}]
wolframSymbols = WolframLanguageData[All, EntityProperty["WolframLanguageSymbol", "Name"]];
Since various content in the Notebook is user defined, I would like to first do some form of replacement along the lines of:
nbFiltered = ReplaceAll[nb, Except[#]->NotWolfram&/@wolframSymbols]
except what I put above does not work.
Assuming it did, what I would really like to figure out how to do (for starters) is get all instances of "SymbolA[SymbolB":
This would require some form of pattern wolframSymbols~~"["~~wolframSymbols
, but more importantly I would like to extract
SymbolA->SymbolB (* if this occurs more than once, then I would like a list of each instance *)
where the valid symbols are Join[wolframSymbols,NotWolfram]
.
It seems the general consensus (see comments) from StringPattern: Not one of pattern? and Limiting scope of StringPattern to first match for efficiency? is that StringPattern
s are not the right approach for this.
So how can I apply this symbol level pattern to extract symbol relations all the while not evaluating the code?
@bdforbes suggestions Verbatim
is the key.
and @Mr.Wizard 's solution goes over my head (as often is the case)
except in a different - but similar post - @Mr.Wizard thinks that "I believe that if you want to match a Symbol by its constituent characters that you do need to use string patterns..."
However, unlike the above posts and solutions, I am not trying this on a symbol or a small collection of symbols. I want to do this on all symbols.
ToExpression
withHold
in the third argument, and parse naturally viaCases
. Almost certainly you’ll need to test againstContext
and just be sure that if you do that the test function hasHoldFirst
or similar as an attribute. $\endgroup$.nb
files from GitHub. I am then trying to import them viaImport[file, "NB"] or NotebookImport[file, "Input"]
. In this case, they are not strings (and auto evaluate on import) soToExpression[nb, <form>, Hold]
doesnt work for almost all forms that would be relevant (e.g.InputForm
,StandardForm
). If I convert to string then try, I get empty array when using cases... for example this is a.nb
I have raw.githubusercontent.com/FeynCalc/feyncalc/master/FeynCalc/… $\endgroup$