Skip to main content.
home | support | download

Back to List Archive

Re: swish.cgi tweaks?

From: Dobrica Pavlinusic <dpavlin(at)not-real.rot13.org>
Date: Mon Apr 19 2004 - 16:23:41 GMT
On Mon, Apr 19, 2004 at 05:55:39AM -0700, Brent Eades wrote:
> 1. Using '+' and '-' as Boolean operators. I'd like to allow users to use
> 'Google-style' queries, up to a point anyway -- e.g., monetary -policy,
> rather than monetary NOT policy, etc.

I'm using something like this in one of my projects (it's also using
Lingua::Spelling::Alternative to generate different spelling for each
word):

foreach (split(/\s+/,$search)) {
	if (m/^([+-])(\S+)/) {
		$s.= ($s) ? "and " : "";
		$s.="not " if ($1 eq "-");
		if (@spellings && !param('no_affix')) {
			my $w = $2; $w =~ s/[\*\s]+//g;
			$w =~ s/^(['"]*)([^'"]+)(['"]*)/$2/;
			my $or="";
			foreach my $spelling_alt (@spellings) {
				$s.="$or$1(".join("* or
",$spelling_alt->alternatives($w))."*)$3 ";
				$or = "or ";
			}
		} else {
			$s.="$2* ";
		}
	} else {
		if (@spellings && !param('no_affix')) {
			my $w = $_; $w =~ s/[\*\s]+//g;
			#$s.="(".join("* or ",$spelling_alt->alternative
s($w))."*) ";
			my $or="";
			foreach my $spelling_alt (@spellings) {
				$s.="$or(".join("* or ",$spelling_alt->a
lternatives($w))."*) ";
				$or = "or ";
			}
		} else {
			$s.="$_* ";
		}
	}
}

Don't let the length scare you. If you want just +/- notation, you should be
fine with just following (untested):

foreach (split(/\s+/,$search)) {
	if (m/^([+-])(\S+)/) {
		$s.= ($s) ? "and " : "";
		$s.="not " if ($1 eq "-");
		$s.="$2* ";
	} else {
		$s.=$_* ";
	}
}

I'm not quite sure where in swish.cgi should this fit nicely, but
someone else will surely know.

-- 
Dobrica Pavlinusic               2share!2flame            dpavlin@rot13.org
Unix addict. Internet consultant.             http://www.rot13.org/~dpavlin
Received on Mon Apr 19 09:23:44 2004