Searching a text file

A.M.'s icon

Hi!
I'm looking for a way to search the text file read by a text object for specific words (ideally typing in the word in a textedit object).
Any idea?

MIB's icon

depending how complicated your search is I'd start with either [route] or [regexpr]. [regexpr] is going to be much more powerful BUT if you are not familiar with regular expression it's a huge learning curve...

A.M.'s icon

Yeah, I was worried about that. I did use regexpr before but for very basic things and I never worked with route.
The search will be pretty simple, just a word (which will be unique). Ideally I would like something outputting the line containing that word out of the [text] outlet.

Peter Ostry's icon

You can [iter] the [text] output and format it to get stored in a [coll] which can be queried. See attached example patch.

Max Patch
Copy patch and select New From Clipboard in Max.

You may have to deal with punctuations and lower/upper case (coll is case sensitive). This can be done by regex or, for regex dummies like me, using [atoi] --> working with numbers --> [itoa]. Or a healthy mix of regexpr and numbers ;-)

A.M.'s icon

Thanks! this is super useful, I'm studying your patch, I can't work out what the printf syntax is (complete noob in C here) but I'll learn, I'm reading the documentation.
I actually need something that outputs the number of the line in which the word can be found so slightly different than that but I can probably find a solution.

A.M.'s icon

Or at least something that once I found the word, outputs the entire line.

Bill 2's icon

I think this is kinda close to it. Still not quite right - punctuation's a concern, as Peter said. And it prints twice, don't know why. Anyway...

Max Patch
Copy patch and select New From Clipboard in Max.

Peter Ostry's icon

A.M. wrote: "I actually need something that outputs the number of the line ... Or at least something that once I found the word, outputs the entire line."

This is a different approach because (compared to my first suggestion) we need to store complete text lines in the [coll] and search them individually.

Here is another patch which stores text lines, let's you search for a word and tells you on which line in which position the word is found. Optionally you can find all occurences or stop after you found the word:

Max Patch
Copy patch and select New From Clipboard in Max.

I think you can clearly see wherefrom I get the line and word numbers, so you can take what you want. The [sprintf] output is just to show the information at once.

You could count the lines in the [text] object, but for the example I just assumed that there are max. 100 lines and max. 200 words per line. Open the [p coll_queries] patcher to adjust the numbers to your needs. The patch and the sub-patch are commented, but feel free to ask if you need more explanation.

I am pretty sure that it can be done in a more elegant way, but so far I see this one works and I understand it myself :-)

A.M.'s icon

Guys this is amazing, thanks!
Bill, I don't know why it prints twice but I re-routed and it works.
Peter, this is FANTASTIC. I don't need all that info (line number is enough) so I'll try to simplify it. Thank you so so much.

Bill 2's icon

Wow - that's really thorough, Peter! :-)

And it taught me a few handy things. Seeing examples of sprintf, regexp and coll's nth message used in something I've attempted made much more sense to me than dry reference texts. Thanks!

Peter Ostry's icon

Lucky accident. Last year I tried generative music made from poems, had a hard time dealing with text in Max and still remember some of the proven techniques.

Example: GenText_1

A.M.'s icon

this is fantastic Peter, it was like having a lesson! I studied your patch and I used a small portion of it modifying it to my needs and in the process I learned a TON!
thank you again

ATelichan's icon

Bumping this post...

I'm curious if @Peter Ostry would be willing to share any of the sonification strategies employed in the GenText_1 patch posted at the end of this thread. I'd be very interested to learn how you mapped the poetry you selected onto generative routines.