Em 2 de novembro de 2010 01:20, Eduardo Az - EMBRASIS Informática e O&M <
[email protected]> escreveu:

>   Oi pessoal.
>
> Existe alguma forma de fazer uma busca semântica no pg?
> Tipo: busco luís, mostra luis e luiz
>
>
Há algum tempo implementei uma pequena PL que retorna um "código fonético"
de uma palavra, justamente para permitir esse tipo de pesquisa que vc
deseja.

Em anexo vai a PL, dai para usar é simples:

SELECT * FROM tabela WHERE fc_fonetica(campo) = fc_fonetica('luis');

Pode ser que isso possa de ajudar...

-- 
Fabrízio de Royes Mello
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
create or replace function fc_fonetica(text) returns text as 
$$
declare
	sString  text;
	sRetorno text;
	cLetra   char(1);
	cProximaLetra char(1);
	cLetraAnterior char(1);
	iConta   integer;
begin
	sString  := trim(upper(coalesce($1, '')));
	sRetorno := '';
	
	for iConta in 1..length(sString)
	loop
		cLetra         := substr(sString, iConta, 1);
		cProximaLetra  := substr(sString, iConta+1, 1);
		cLetraAnterior := substr(sString, iConta-1, 1);

		if cLetra = ' ' then
			sRetorno := sRetorno || '0';
			
		elsif cLetra = 'C' then
			
			if cProximaLetra = 'H' then
				sRetorno := sRetorno || 'X';

			elsif cProximaLetra in ('E', 'I') then
				sRetorno := sRetorno || 'SS';

			else
				sRetorno := sRetorno || 'K';

			end if;

		elsif cLetra in ('B', 'D', 'F', 'G', 'J', 'K', 'L', 'M', 'R', 'T', 'V', 'X') then
			sRetorno := sRetorno || cLetra;

		elsif cLetra = 'L' then

			if cProximaLetra = 'L' then
				sRetorno := sRetorno || 'L';
			elsif cLetraAnterior = 'L' then
				sRetorno := sRetorno || '';
			else
				sRetorno := sRetorno || 'L';
			end if;

		elsif cLetra = 'N' then
			
			if cProximaLetra in ('', '0') or cProximaLetra not in ('A', 'E', 'I', 'O', 'U') then
				sRetorno := sRetorno || 'M';
			else
				sRetorno := sRetorno || 'N';
			end if;
			
		elsif cLetra = 'P' then
			
			if cProximaLetra = 'H' then
				sRetorno := sRetorno || 'F';
			else
				sRetorno := sRetorno || 'P';
			end if;
		
		elsif cLetra = 'Q' then
			sRetorno := sRetorno || 'K';

		elsif cLetra = 'S' then
		
			if cProximaLetra = 'H' then 
				sRetorno := sRetorno || 'X';
			elsif cLetraAnterior = 'S' or cProximaLetra = 'S' or cProximaLetra not in ('A', 'E', 'I', 'O', 'U') then
				sRetorno := sRetorno || 'S';
			elsif iConta = 0 or cProximaLetra not in ('A', 'E', 'I', 'O', 'U') then
				sRetorno := sRetorno || 'SS';
			else
				sRetorno := sRetorno || 'Z';
			end if;
			
		elsif cLetra = 'W' then
			
			if cProximaLetra in ('A', 'E', 'I', 'O', 'U') then
				sRetorno := sRetorno || 'V';
			end if;

		elsif cLetra = 'Z' then

			if cProximaLetra = 'Z' then
				sRetorno := sRetorno || 'Z';
			elsif cLetraAnterior = 'Z' then
				sRetorno := sRetorno || '';
			elsif cProximaLetra not in ('A', 'E', 'I', 'O', 'U') then
				sRetorno := sRetorno || 'S';
			elsif cProximaLetra = '' then
				sRetorno := sRetorno || 's';
			else
				sRetorno := sRetorno || 'Z';
			end if;

    elsif cLetra = '%' then
      sRetorno := sRetorno || cLetra;

		end if;

	end loop;

	return sRetorno;
end;
$$ language 'plpgsql' immutable;


_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a