in reply to A Tagcloud For Cory Doctorow

Nicely done. Works like a dream.

It is lacking one thing, I think. What about notes where tags collide. For example, maybe I want to find all the notes that have @foo AND @baz tags.

I tried using the search box by typing:
@foo @baz
but it searches for "@foo @baz". So unless these tags are added in exactly that order, the search is empty.

Am I missing something obvious or is this perl script unable to find tag collisions? Is there a fix? Workaround?

Have a nice day,
Thorarin Bjarnason

Replies are listed 'Best First'.
Re^2: A Tagcloud For Cory Doctorow
by McD (Chaplain) on Jun 05, 2009 at 13:08 UTC
    Nicely done. Works like a dream.

    Thanks! Glad you like it.

    It is lacking one thing, I think. What about notes where tags collide. For example, maybe I want to find all the notes that have @foo AND @baz tags.

    The word "collide" threw me there for a minute, but I think I understand - search for notes with two or more tags in common. The search function as implemented is a simple string match in the notes, you're thinking more along the lines of a "search engine" search. Interesting.

    Is there a fix? Workaround?

    You could change the code to make the search more in line with what you're envisioning. It would be a bit more plumbing to separate the search string into discreet terms, and then change the matching (and highlighting) code to work with a set of terms and logical conditions (AND and OR) rather than just a straight match/no match. That's a bit of work, actually.

    But there is a very simple, if ugly, workaround. First, you need to remove the \Q from the pattern matching loop here:

    # A little ugly: We're going to grep thru @all_notes looking for # a match - but we need to strip the HTML markup (which we've # added to turn tags into links) out of the notes before checkin +g # for a match, so that you don't match inside the HTML markup # while searching. Also, you need to use a temp var, because # otherwise grep will modify $_. Finally, use \Q (quotemeta) +- # we don't want full patterns here, too much risk foreach (grep {my $t; ($t=$_) =~ s/<.*?>//g; $t =~ /\Q$search_string/i} @main::all_notes) {
    (Note the comment about \Q being put in there explicitly because I thought regex searches would be too risky. We're taking the safety off, watch where you aim this thing...)

    Anyway, change that line to look like this:

    $t =~ /$search_string/i}

    ...and now you can do what you want by using the techniques outlined in The Perl Cookbook, Chapter 6.17, "Expressing AND, OR, and NOT in a Single Pattern." To search for @foo AND @bar, you'd search for:

    @foo.*@bar|@bar.*@foo

    To search for @foo OR @bar, you'd use:

    @foo|@bar

    Cavaet - if you're searching for strings not found in tags, search result highlighting will be screwy with this approach.

    So that's a quick and dirty fix. Can't say I'm in love with it, but it does do what you want, and making the search box more robust would be a good deal more work.
    Have a nice day

    Thanks, I already am. You too. :-)
      Wow - you are fast!

      This will do for me. Thanks you!

      Thorarin Bjarnason