Message Boards Message Boards

0
|
21281 Views
|
11 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Tools question: how to search a directory full of notebooks

Posted 11 years ago
As I'm learning to program Mathematica, I'm quickly collecting quite a number of notebooks that I've written.

When I want to remember an idiom or technique I've used, I need to search the notebooks.  I've found that the directory search in Win 7 is pretty bad for this.  For example if I search for Reap, it will find it when it starts a line, but not if it is "buried" as in Cases[Reap[...

What tools do folks use for this?

Thanks!
-Joe
POSTED BY: Joe Gilray
11 Replies
Is this any use? You might be able to adapt it...or even get rid of the pattern matching stuff...
POSTED BY: C ormullion
Posted 11 years ago
OK, I've gotten it good enough for me.  Here's the code I'm using:
nbScan[line_, str_] :=
Block[{s = StringSplit[StringDrop[line, -1],
   x : RegularExpression[(str)] :> Style[x, Red, Bold, 16]]}, If[Length@s >= 2, Print[Row[s]]]]
onefile[fn_, str_] := Block[{}, Print["searching for \"", str, "\" in ", fn];
   Map[nbScan[#, str] &, StringSplit[Import[fn, "Plaintext", IgnoreEmptyLines -> True], "\n"]]]
nbs[str_] := Block[{files = FileNames[StringJoin[NotebookDirectory[], "*.nb"]]},
   Map[onefile[#, str] &, files];]

It works with regexps and does a decent job of highlighting.  I can simply type:

nbs["Cases"]

or 

nbs["P..t"]

try it!  I'm sure there are some improvements that can be made

-Joe
POSTED BY: Joe Gilray
Posted 11 years ago
I've done some web searches and still haven't found anything.

Anybody got the same issue?  Maybe I need to write a quick tool to do these notebook searches?

-Joe
POSTED BY: Joe Gilray
Posted 11 years ago
Thanks that was a great starting point!  OK, I've made a start, but I need help:

I created the following simple functions (which could be put in init.m, for example)
nbScan[line_, str_] := If[StringMatchQ[line, ___ ~~ str ~~ ___], Print@line]
onefile[fn_, str_] := (Print["searching for \"", str, "\" in ", fn];
   Map[nbScan[#, str] &, StringSplit[Import[fn, "Plaintext"], "\n"]];)
nbs[str_] := (files = FileNames[StringJoin[NotebookDirectory[], "*.nb"]];
   Map[onefile[#, str] &, files];)

Now I can search all notebooks in the current directory by typing something like this in a cell":
nbs["Plot"]

Here is sample output:
searching for "Plot" in C:\Users\Gilrays\Documents\Mathematica\Play1.nb
ListPlot[Map[Total, Map[Last, FactorInteger[Range[2, 1000]], {2}]]]

Manipulate[ListPlot[Total/@Map[Last, FactorInteger[Range[2, x]], {2}]], {x, 2, 1000}]

searching for "Plot" in C:\Users\Gilrays\Documents\Mathematica\tips.nb
...

Some things I'd like help with:

1) How do I get rid of the extra empty line after each "match"
2) I'd like to bold each match like this:
   ListPlot[Map[Total, Map[Last, FactorInteger[Range[2, 1000]], {2}]]]
3) other ideas?

Thanks in advance,
-Joe
POSTED BY: Joe Gilray
Posted 11 years ago
Has anybody tried the code above?  I had a funny typo ("Limes" instead of "Lines"), fixed now.

I've been using it very happily (I have a directory of about 80 small notebooks - I can search them pretty quickly for idioms, use of functions, etc.), but I'm sure it can be improved.

Thanks,
-Joe
POSTED BY: Joe Gilray
Posted 9 years ago

Hi again, I've started using Mathematica on Linux now, have learned some things (surprise!) and have fixed the code as well as added a "recursive search" feature.

Let me know if you try it. You can make it available in all notebooks by putting it in your init.m file. -Joe

(* provides notebook searching functionality *)
(* nbs["P..t"] will  match all lines in notebooks in the current directory *)
(* nbs["Idiom","directory"] will match all lines in notebooks in the passed directory and below *)
nbScan[line_, str_] := 
     Block[{s = StringSplit[StringDrop[line, -1], x : RegularExpression[(str)] :> Style[x, Red, Bold, 16]]}, 
      If[Length@s >= 2, Print[Row[s]]]]
onefile[fn_, str_] := Block[{}, Print["searching for \"", str, "\" in ", fn];
      Map[If[# != "", nbScan[#, str]] &, StringSplit[Import[fn, "Plaintext"], "\n"]]]
nbs[str_, lookup_: ""] := Block[{lu = lookup, nbd = NotebookDirectory[], files = {}}, 
      If[lu == "", files = FileNames[StringJoin[nbd, "*.nb"]],
         SetDirectory[lu]; files = FileNames["*.nb", {Directory[]}, Infinity]; SetDirectory[nbd]]
        Map[onefile[#, str] &, files];]
POSTED BY: Joe Gilray

Though the method works quite well, why not try the new TextSearch function. I've used it already a few times, works super convenient!

si = CreateSearchIndex[directory];
TextSearch[si, "never"]
POSTED BY: Sander Huisman

Agent Ransack

POSTED BY: Frank Iannarilli
Posted 9 years ago

Hi Guys, thanks for the great suggestions.

I still think nbs[] has value for the following reasons:

1) tools like Agent Ransack run outside of Mathematica. I like to be able to search "in situ" while I'm working in a Mathematica workbook.

2) TextSearch is not in earlier versions of Mathematica (I'm still on 9.0.1 for example) and it is marked "Experimental" in the docs at least for now.

Thanks again!

Joe

POSTED BY: Joe Gilray

I am attempting to use the Text Search function...

I cannot get the b[[i]] to cycle through all the i's the table function. It has something to do with the hold rest attribute for rule delayed symbol in the last line of code. Can someone help me?

You can try it by putting in your own file directory where you see mine.

d = InputString[];

si = CreateSearchIndex[
   "/Users/michaelmccain/Library/Mobile \ \
Documents/com~apple~CloudDocs/Ventura College/Mathematica/How To/"];

doc = TextSearch[si, d];

b = Table[doc[[i]]["Location"], {i, 1, Length[doc]}]; a = 
 Table[doc[[i]]["FileName"], {i, 1, Length[doc]}];

ActionMenu["Search Results", 
 Table[a[[i]] :> SystemOpen[b[[1]]], {i, 1, Length[doc]}]]
POSTED BY: Michael McCain

I am attempting to use the Text Search function...

I cannot get the b[[i]] to cycle through all the i's the table function. It has something to do with the hold rest attribute for rule delayed symbol in the last line of code. Can someone help me?

You can try it by putting in your own file directory where you see mine.

d = InputString[];

si = CreateSearchIndex[
   "/Users/michaelmccain/Library/Mobile \ \
Documents/com~apple~CloudDocs/Ventura College/Mathematica/How To/"];

doc = TextSearch[si, d];

b = Table[doc[[i]]["Location"], {i, 1, Length[doc]}]; a = 
 Table[doc[[i]]["FileName"], {i, 1, Length[doc]}];

ActionMenu["Search Results", 
 Table[a[[i]] :> SystemOpen[b[[1]]], {i, 1, Length[doc]}]]
POSTED BY: Michael McCain
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