Em 30 de setembro de 2010 11:50, Fernando <[email protected]> escreveu:

>  Veja a soundex() function
>
> http://www.postgresql.org/docs/8.3/static/fuzzystrmatch.html
>
> Você pode gravar o valor fonético da descrição em um atributo e utilizar
> para fazer a pesquisa fonética
>
>
>
Só não acredito que ele seja eficiente com retornar bons códigos fonéticos
de palavras na língua portuguesa, veja o que diz a documentação:

"The Soundex system is a method of matching similar-sounding names by
converting them to the same code. It was initially used by the United States
Census in 1880, 1900, and 1910. *Note that Soundex is not very useful for
non-English names*."

Há algum tempo escrevi uma pequena PL para retornar códigos fonéticos na
nossa língua e postei aqui na lista... de qualquer forma estou enviando
novamente caso vc queira dar uma analisada...

Cordialmente,
-- 
Fabrízio de Royes Mello
>> Blog sobre TI: http://fabriziomello.blogspot.com
>> Perfil Linkedin: http://br.linkedin.com/in/fabriziomello
-- Funcao para geracao de código fonético em pt_BR
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 = '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;


-- Testes

drop table clientes;
create table clientes (
  codigo serial primary key,
  nome varchar(50)
);
create index clientes_nomefonetica_in on clientes(fc_fonetica(nome) varchar_pattern_ops);

insert into clientes (nome) values ('Luis');
insert into clientes (nome) values ('Luiz');
insert into clientes (nome) values ('Luís');
insert into clientes (nome) values ('Luíz');
insert into clientes (nome) values ('Luys');
insert into clientes (nome) values ('Luyz');
insert into clientes (nome) values ('Luisa');
insert into clientes (nome) values ('Luiza');

\echo -------------------
\echo Procurando por Luis
\echo -------------------
select * from clientes where fc_fonetica(nome) = fc_fonetica('Luis');

\echo -------------------
\echo Procurando por Luisa
\echo -------------------
select * from clientes where fc_fonetica(nome) = fc_fonetica('Luisa');
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a