
-- a commenter si besoin
drop table if exists public.char_equivalence cascade;
drop function if exists public.replace_char(text);
drop function if exists public.normalize(text);

-- char_code correspond au code ascii du caractere a remplacer
-- char_replace correspond au caractere de remplacement

create table public.char_equivalence ( char_code numeric, char_replace varchar);
insert into public.char_equivalence(char_code,char_replace) values (543,'h');
insert into public.char_equivalence(char_code,char_replace) values (369,'u');
insert into public.char_equivalence(char_code,char_replace) values (214,'O');
insert into public.char_equivalence(char_code,char_replace) values (287,'g');
insert into public.char_equivalence(char_code,char_replace) values (351,'s');
insert into public.char_equivalence(char_code,char_replace) values (374,'Y');
insert into public.char_equivalence(char_code,char_replace) values (608,'g');
insert into public.char_equivalence(char_code,char_replace) values (223,'SS');
insert into public.char_equivalence(char_code,char_replace) values (213,'O');
insert into public.char_equivalence(char_code,char_replace) values (286,'G');
insert into public.char_equivalence(char_code,char_replace) values (413,'N');
insert into public.char_equivalence(char_code,char_replace) values (224,'a');
insert into public.char_equivalence(char_code,char_replace) values (285,'g');
insert into public.char_equivalence(char_code,char_replace) values (598,'d');
insert into public.char_equivalence(char_code,char_replace) values (201,'E');
insert into public.char_equivalence(char_code,char_replace) values (480,'A');
insert into public.char_equivalence(char_code,char_replace) values (416,'O');
insert into public.char_equivalence(char_code,char_replace) values (542,'H');
insert into public.char_equivalence(char_code,char_replace) values (288,'G');
insert into public.char_equivalence(char_code,char_replace) values (257,'a');
insert into public.char_equivalence(char_code,char_replace) values (481,'a');
insert into public.char_equivalence(char_code,char_replace) values (352,'S');
insert into public.char_equivalence(char_code,char_replace) values (347,'s');
insert into public.char_equivalence(char_code,char_replace) values (309,'j');
insert into public.char_equivalence(char_code,char_replace) values (501,'g');
insert into public.char_equivalence(char_code,char_replace) values (283,'e');
insert into public.char_equivalence(char_code,char_replace) values (437,'Z');
insert into public.char_equivalence(char_code,char_replace) values (373,'w');
insert into public.char_equivalence(char_code,char_replace) values (258,'A');
insert into public.char_equivalence(char_code,char_replace) values (203,'E');
insert into public.char_equivalence(char_code,char_replace) values (194,'A');
insert into public.char_equivalence(char_code,char_replace) values (259,'a');
insert into public.char_equivalence(char_code,char_replace) values (375,'y');
insert into public.char_equivalence(char_code,char_replace) values (212,'O');
insert into public.char_equivalence(char_code,char_replace) values (376,'Y');
insert into public.char_equivalence(char_code,char_replace) values (284,'G');
insert into public.char_equivalence(char_code,char_replace) values (504,'N');
insert into public.char_equivalence(char_code,char_replace) values (479,'a');
insert into public.char_equivalence(char_code,char_replace) values (232,'e');
insert into public.char_equivalence(char_code,char_replace) values (262,'C');
insert into public.char_equivalence(char_code,char_replace) values (536,'S');
insert into public.char_equivalence(char_code,char_replace) values (310,'K');
insert into public.char_equivalence(char_code,char_replace) values (263,'c');
insert into public.char_equivalence(char_code,char_replace) values (548,'Z');
insert into public.char_equivalence(char_code,char_replace) values (462,'a');
insert into public.char_equivalence(char_code,char_replace) values (599,'d');
insert into public.char_equivalence(char_code,char_replace) values (350,'S');
insert into public.char_equivalence(char_code,char_replace) values (545,'d');
insert into public.char_equivalence(char_code,char_replace) values (371,'u');
insert into public.char_equivalence(char_code,char_replace) values (193,'A');
insert into public.char_equivalence(char_code,char_replace) values (537,'s');
insert into public.char_equivalence(char_code,char_replace) values (324,'n');
insert into public.char_equivalence(char_code,char_replace) values (260,'A');
insert into public.char_equivalence(char_code,char_replace) values (349,'s');
insert into public.char_equivalence(char_code,char_replace) values (206,'I');
insert into public.char_equivalence(char_code,char_replace) values (321,'L');
insert into public.char_equivalence(char_code,char_replace) values (372,'W');
insert into public.char_equivalence(char_code,char_replace) values (196,'A');
insert into public.char_equivalence(char_code,char_replace) values (417,'o');
insert into public.char_equivalence(char_code,char_replace) values (500,'G');
insert into public.char_equivalence(char_code,char_replace) values (364,'U');
insert into public.char_equivalence(char_code,char_replace) values (528,'R');
insert into public.char_equivalence(char_code,char_replace) values (195,'A');
insert into public.char_equivalence(char_code,char_replace) values (410,'l');
insert into public.char_equivalence(char_code,char_replace) values (36,'S');
insert into public.char_equivalence(char_code,char_replace) values (539,'t');
insert into public.char_equivalence(char_code,char_replace) values (370,'U');
insert into public.char_equivalence(char_code,char_replace) values (409,'k');
insert into public.char_equivalence(char_code,char_replace) values (282,'E');
insert into public.char_equivalence(char_code,char_replace) values (323,'N');
insert into public.char_equivalence(char_code,char_replace) values (330,'N');
insert into public.char_equivalence(char_code,char_replace) values (281,'e');
insert into public.char_equivalence(char_code,char_replace) values (204,'I');
insert into public.char_equivalence(char_code,char_replace) values (311,'k');
insert into public.char_equivalence(char_code,char_replace) values (420,'P');
insert into public.char_equivalence(char_code,char_replace) values (302,'I');
insert into public.char_equivalence(char_code,char_replace) values (527,'o');
insert into public.char_equivalence(char_code,char_replace) values (312,'k');
insert into public.char_equivalence(char_code,char_replace) values (261,'a');
insert into public.char_equivalence(char_code,char_replace) values (538,'T');
insert into public.char_equivalence(char_code,char_replace) values (202,'E');
insert into public.char_equivalence(char_code,char_replace) values (496,'j');
insert into public.char_equivalence(char_code,char_replace) values (358,'T');
insert into public.char_equivalence(char_code,char_replace) values (432,'u');
insert into public.char_equivalence(char_code,char_replace) values (207,'I');
insert into public.char_equivalence(char_code,char_replace) values (304,'I');
insert into public.char_equivalence(char_code,char_replace) values (556,'O');
insert into public.char_equivalence(char_code,char_replace) values (363,'u');
insert into public.char_equivalence(char_code,char_replace) values (368,'U');
insert into public.char_equivalence(char_code,char_replace) values (8364,'E');
insert into public.char_equivalence(char_code,char_replace) values (553,'e');
insert into public.char_equivalence(char_code,char_replace) values (241,'n');
insert into public.char_equivalence(char_code,char_replace) values (198,'AE');
insert into public.char_equivalence(char_code,char_replace) values (208,'D');
insert into public.char_equivalence(char_code,char_replace) values (271,'d');
insert into public.char_equivalence(char_code,char_replace) values (554,'O');
insert into public.char_equivalence(char_code,char_replace) values (329,'n');
insert into public.char_equivalence(char_code,char_replace) values (529,'r');
insert into public.char_equivalence(char_code,char_replace) values (270,'D');
insert into public.char_equivalence(char_code,char_replace) values (507,'a');
insert into public.char_equivalence(char_code,char_replace) values (353,'s');
insert into public.char_equivalence(char_code,char_replace) values (555,'o');
insert into public.char_equivalence(char_code,char_replace) values (354,'T');
insert into public.char_equivalence(char_code,char_replace) values (268,'C');
insert into public.char_equivalence(char_code,char_replace) values (506,'A');
insert into public.char_equivalence(char_code,char_replace) values (273,'d');
insert into public.char_equivalence(char_code,char_replace) values (549,'z');
insert into public.char_equivalence(char_code,char_replace) values (331,'n');
insert into public.char_equivalence(char_code,char_replace) values (359,'t');
insert into public.char_equivalence(char_code,char_replace) values (162,'C');
insert into public.char_equivalence(char_code,char_replace) values (531,'r');
insert into public.char_equivalence(char_code,char_replace) values (431,'U');
insert into public.char_equivalence(char_code,char_replace) values (616,'i');
insert into public.char_equivalence(char_code,char_replace) values (303,'i');
insert into public.char_equivalence(char_code,char_replace) values (274,'E');
insert into public.char_equivalence(char_code,char_replace) values (205,'I');
insert into public.char_equivalence(char_code,char_replace) values (530,'R');
insert into public.char_equivalence(char_code,char_replace) values (165,'Y');
insert into public.char_equivalence(char_code,char_replace) values (293,'h');
insert into public.char_equivalence(char_code,char_replace) values (421,'p');
insert into public.char_equivalence(char_code,char_replace) values (357,'t');
insert into public.char_equivalence(char_code,char_replace) values (550,'A');
insert into public.char_equivalence(char_code,char_replace) values (340,'R');
insert into public.char_equivalence(char_code,char_replace) values (269,'c');
insert into public.char_equivalence(char_code,char_replace) values (277,'e');
insert into public.char_equivalence(char_code,char_replace) values (510,'O');
insert into public.char_equivalence(char_code,char_replace) values (355,'t');
insert into public.char_equivalence(char_code,char_replace) values (438,'z');
insert into public.char_equivalence(char_code,char_replace) values (505,'n');
insert into public.char_equivalence(char_code,char_replace) values (278,'E');
insert into public.char_equivalence(char_code,char_replace) values (535,'u');
insert into public.char_equivalence(char_code,char_replace) values (296,'I');
insert into public.char_equivalence(char_code,char_replace) values (360,'U');
insert into public.char_equivalence(char_code,char_replace) values (339,'oe');
insert into public.char_equivalence(char_code,char_replace) values (435,'Y');
insert into public.char_equivalence(char_code,char_replace) values (275,'e');
insert into public.char_equivalence(char_code,char_replace) values (534,'U');
insert into public.char_equivalence(char_code,char_replace) values (221,'Y');
insert into public.char_equivalence(char_code,char_replace) values (265,'c');
insert into public.char_equivalence(char_code,char_replace) values (436,'y');
insert into public.char_equivalence(char_code,char_replace) values (178,'2');
insert into public.char_equivalence(char_code,char_replace) values (532,'U');
insert into public.char_equivalence(char_code,char_replace) values (346,'S');
insert into public.char_equivalence(char_code,char_replace) values (218,'U');
insert into public.char_equivalence(char_code,char_replace) values (595,'b');
insert into public.char_equivalence(char_code,char_replace) values (295,'h');
insert into public.char_equivalence(char_code,char_replace) values (267,'c');
insert into public.char_equivalence(char_code,char_replace) values (552,'E');
insert into public.char_equivalence(char_code,char_replace) values (345,'r');
insert into public.char_equivalence(char_code,char_replace) values (367,'u');
insert into public.char_equivalence(char_code,char_replace) values (219,'U');
insert into public.char_equivalence(char_code,char_replace) values (573,'L');
insert into public.char_equivalence(char_code,char_replace) values (513,'a');
insert into public.char_equivalence(char_code,char_replace) values (430,'T');
insert into public.char_equivalence(char_code,char_replace) values (266,'C');
insert into public.char_equivalence(char_code,char_replace) values (551,'a');
insert into public.char_equivalence(char_code,char_replace) values (429,'t');
insert into public.char_equivalence(char_code,char_replace) values (220,'U');
insert into public.char_equivalence(char_code,char_replace) values (512,'A');
insert into public.char_equivalence(char_code,char_replace) values (366,'U');
insert into public.char_equivalence(char_code,char_replace) values (574,'T');
insert into public.char_equivalence(char_code,char_replace) values (348,'S');
insert into public.char_equivalence(char_code,char_replace) values (533,'u');
insert into public.char_equivalence(char_code,char_replace) values (511,'o');
insert into public.char_equivalence(char_code,char_replace) values (217,'U');
insert into public.char_equivalence(char_code,char_replace) values (276,'E');
insert into public.char_equivalence(char_code,char_replace) values (365,'u');
insert into public.char_equivalence(char_code,char_replace) values (469,'U');
insert into public.char_equivalence(char_code,char_replace) values (491,'o');
insert into public.char_equivalence(char_code,char_replace) values (427,'t');
insert into public.char_equivalence(char_code,char_replace) values (341,'r');
insert into public.char_equivalence(char_code,char_replace) values (315,'L');
insert into public.char_equivalence(char_code,char_replace) values (246,'o');
insert into public.char_equivalence(char_code,char_replace) values (235,'e');
insert into public.char_equivalence(char_code,char_replace) values (571,'C');
insert into public.char_equivalence(char_code,char_replace) values (576,'z');
insert into public.char_equivalence(char_code,char_replace) values (470,'u');
insert into public.char_equivalence(char_code,char_replace) values (342,'R');
insert into public.char_equivalence(char_code,char_replace) values (318,'l');
insert into public.char_equivalence(char_code,char_replace) values (313,'L');
insert into public.char_equivalence(char_code,char_replace) values (225,'a');
insert into public.char_equivalence(char_code,char_replace) values (317,'L');
insert into public.char_equivalence(char_code,char_replace) values (169,'C');
insert into public.char_equivalence(char_code,char_replace) values (380,'z');
insert into public.char_equivalence(char_code,char_replace) values (316,'l');
insert into public.char_equivalence(char_code,char_replace) values (379,'Z');
insert into public.char_equivalence(char_code,char_replace) values (234,'e');
insert into public.char_equivalence(char_code,char_replace) values (337,'o');
insert into public.char_equivalence(char_code,char_replace) values (575,'s');
insert into public.char_equivalence(char_code,char_replace) values (378,'z');
insert into public.char_equivalence(char_code,char_replace) values (290,'G');
insert into public.char_equivalence(char_code,char_replace) values (401,'F');
insert into public.char_equivalence(char_code,char_replace) values (319,'L');
insert into public.char_equivalence(char_code,char_replace) values (314,'l');
insert into public.char_equivalence(char_code,char_replace) values (338,'OE');
insert into public.char_equivalence(char_code,char_replace) values (226,'a');
insert into public.char_equivalence(char_code,char_replace) values (626,'n');
insert into public.char_equivalence(char_code,char_replace) values (245,'o');
insert into public.char_equivalence(char_code,char_replace) values (572,'c');
insert into public.char_equivalence(char_code,char_replace) values (181,'U');
insert into public.char_equivalence(char_code,char_replace) values (402,'f');
insert into public.char_equivalence(char_code,char_replace) values (64,'A');
insert into public.char_equivalence(char_code,char_replace) values (279,'e');
insert into public.char_equivalence(char_code,char_replace) values (356,'T');
insert into public.char_equivalence(char_code,char_replace) values (407,'I');
insert into public.char_equivalence(char_code,char_replace) values (192,'A');
insert into public.char_equivalence(char_code,char_replace) values (248,'o');
insert into public.char_equivalence(char_code,char_replace) values (428,'T');
insert into public.char_equivalence(char_code,char_replace) values (514,'A');
insert into public.char_equivalence(char_code,char_replace) values (565,'n');
insert into public.char_equivalence(char_code,char_replace) values (320,'l');
insert into public.char_equivalence(char_code,char_replace) values (468,'u');
insert into public.char_equivalence(char_code,char_replace) values (163,'L');
insert into public.char_equivalence(char_code,char_replace) values (200,'E');
insert into public.char_equivalence(char_code,char_replace) values (294,'H');
insert into public.char_equivalence(char_code,char_replace) values (517,'e');
insert into public.char_equivalence(char_code,char_replace) values (199,'C');
insert into public.char_equivalence(char_code,char_replace) values (617,'l');
insert into public.char_equivalence(char_code,char_replace) values (516,'E');
insert into public.char_equivalence(char_code,char_replace) values (406,'l');
insert into public.char_equivalence(char_code,char_replace) values (382,'z');
insert into public.char_equivalence(char_code,char_replace) values (291,'g');
insert into public.char_equivalence(char_code,char_replace) values (244,'o');
insert into public.char_equivalence(char_code,char_replace) values (105,'i');
insert into public.char_equivalence(char_code,char_replace) values (515,'a');
insert into public.char_equivalence(char_code,char_replace) values (489,'k');
insert into public.char_equivalence(char_code,char_replace) values (488,'K');
insert into public.char_equivalence(char_code,char_replace) values (388,'b');
insert into public.char_equivalence(char_code,char_replace) values (361,'u');
insert into public.char_equivalence(char_code,char_replace) values (381,'Z');
insert into public.char_equivalence(char_code,char_replace) values (233,'e');
insert into public.char_equivalence(char_code,char_replace) values (243,'o');
insert into public.char_equivalence(char_code,char_replace) values (280,'E');
insert into public.char_equivalence(char_code,char_replace) values (289,'g');
insert into public.char_equivalence(char_code,char_replace) values (344,'R');
insert into public.char_equivalence(char_code,char_replace) values (570,'A');
insert into public.char_equivalence(char_code,char_replace) values (518,'E');
insert into public.char_equivalence(char_code,char_replace) values (332,'O');
insert into public.char_equivalence(char_code,char_replace) values (242,'o');
insert into public.char_equivalence(char_code,char_replace) values (239,'i');
insert into public.char_equivalence(char_code,char_replace) values (383,'f');
insert into public.char_equivalence(char_code,char_replace) values (557,'o');
insert into public.char_equivalence(char_code,char_replace) values (490,'O');
insert into public.char_equivalence(char_code,char_replace) values (362,'U');
insert into public.char_equivalence(char_code,char_replace) values (567,'j');
insert into public.char_equivalence(char_code,char_replace) values (236,'i');
insert into public.char_equivalence(char_code,char_replace) values (558,'O');
insert into public.char_equivalence(char_code,char_replace) values (343,'r');
insert into public.char_equivalence(char_code,char_replace) values (237,'i');
insert into public.char_equivalence(char_code,char_replace) values (493,'o');
insert into public.char_equivalence(char_code,char_replace) values (197,'A');
insert into public.char_equivalence(char_code,char_replace) values (292,'H');
insert into public.char_equivalence(char_code,char_replace) values (492,'O');
insert into public.char_equivalence(char_code,char_replace) values (238,'i');
insert into public.char_equivalence(char_code,char_replace) values (301,'i');
insert into public.char_equivalence(char_code,char_replace) values (384,'b');
insert into public.char_equivalence(char_code,char_replace) values (486,'G');
insert into public.char_equivalence(char_code,char_replace) values (464,'i');
insert into public.char_equivalence(char_code,char_replace) values (326,'n');
insert into public.char_equivalence(char_code,char_replace) values (272,'D');
insert into public.char_equivalence(char_code,char_replace) values (524,'O');
insert into public.char_equivalence(char_code,char_replace) values (336,'O');
insert into public.char_equivalence(char_code,char_replace) values (463,'I');
insert into public.char_equivalence(char_code,char_replace) values (525,'o');
insert into public.char_equivalence(char_code,char_replace) values (299,'i');
insert into public.char_equivalence(char_code,char_replace) values (240,'d');
insert into public.char_equivalence(char_code,char_replace) values (251,'u');
insert into public.char_equivalence(char_code,char_replace) values (230,'ae');
insert into public.char_equivalence(char_code,char_replace) values (335,'o');
insert into public.char_equivalence(char_code,char_replace) values (526,'O');
insert into public.char_equivalence(char_code,char_replace) values (252,'u');
insert into public.char_equivalence(char_code,char_replace) values (297,'i');
insert into public.char_equivalence(char_code,char_replace) values (475,'U');
insert into public.char_equivalence(char_code,char_replace) values (385,'B');
insert into public.char_equivalence(char_code,char_replace) values (523,'i');
insert into public.char_equivalence(char_code,char_replace) values (560,'O');
insert into public.char_equivalence(char_code,char_replace) values (322,'l');
insert into public.char_equivalence(char_code,char_replace) values (210,'O');
insert into public.char_equivalence(char_code,char_replace) values (300,'I');
insert into public.char_equivalence(char_code,char_replace) values (465,'O');
insert into public.char_equivalence(char_code,char_replace) values (231,'c');
insert into public.char_equivalence(char_code,char_replace) values (250,'u');
insert into public.char_equivalence(char_code,char_replace) values (386,'b');
insert into public.char_equivalence(char_code,char_replace) values (209,'N');
insert into public.char_equivalence(char_code,char_replace) values (487,'g');
insert into public.char_equivalence(char_code,char_replace) values (648,'t');
insert into public.char_equivalence(char_code,char_replace) values (520,'I');
insert into public.char_equivalence(char_code,char_replace) values (387,'b');
insert into public.char_equivalence(char_code,char_replace) values (466,'o');
insert into public.char_equivalence(char_code,char_replace) values (562,'Y');
insert into public.char_equivalence(char_code,char_replace) values (264,'C');
insert into public.char_equivalence(char_code,char_replace) values (519,'e');
insert into public.char_equivalence(char_code,char_replace) values (484,'G');
insert into public.char_equivalence(char_code,char_replace) values (461,'A');
insert into public.char_equivalence(char_code,char_replace) values (566,'t');
insert into public.char_equivalence(char_code,char_replace) values (229,'a');
insert into public.char_equivalence(char_code,char_replace) values (392,'c');
insert into public.char_equivalence(char_code,char_replace) values (485,'g');
insert into public.char_equivalence(char_code,char_replace) values (559,'o');
insert into public.char_equivalence(char_code,char_replace) values (228,'a');
insert into public.char_equivalence(char_code,char_replace) values (333,'o');
insert into public.char_equivalence(char_code,char_replace) values (249,'u');
insert into public.char_equivalence(char_code,char_replace) values (215,'X');
insert into public.char_equivalence(char_code,char_replace) values (227,'a');
insert into public.char_equivalence(char_code,char_replace) values (305,'i');
insert into public.char_equivalence(char_code,char_replace) values (478,'A');
insert into public.char_equivalence(char_code,char_replace) values (327,'N');
insert into public.char_equivalence(char_code,char_replace) values (563,'y');
insert into public.char_equivalence(char_code,char_replace) values (377,'Z');
insert into public.char_equivalence(char_code,char_replace) values (391,'C');
insert into public.char_equivalence(char_code,char_replace) values (298,'I');
insert into public.char_equivalence(char_code,char_replace) values (393,'D');
insert into public.char_equivalence(char_code,char_replace) values (403,'G');
insert into public.char_equivalence(char_code,char_replace) values (467,'U');
insert into public.char_equivalence(char_code,char_replace) values (334,'O');
insert into public.char_equivalence(char_code,char_replace) values (561,'o');
insert into public.char_equivalence(char_code,char_replace) values (307,'ij');
insert into public.char_equivalence(char_code,char_replace) values (211,'O');
insert into public.char_equivalence(char_code,char_replace) values (216,'O');
insert into public.char_equivalence(char_code,char_replace) values (253,'y');
insert into public.char_equivalence(char_code,char_replace) values (472,'u');
insert into public.char_equivalence(char_code,char_replace) values (408,'K');
insert into public.char_equivalence(char_code,char_replace) values (308,'J');
insert into public.char_equivalence(char_code,char_replace) values (474,'u');
insert into public.char_equivalence(char_code,char_replace) values (564,'l');
insert into public.char_equivalence(char_code,char_replace) values (473,'U');
insert into public.char_equivalence(char_code,char_replace) values (255,'y');
insert into public.char_equivalence(char_code,char_replace) values (394,'D');
insert into public.char_equivalence(char_code,char_replace) values (306,'IJ');
insert into public.char_equivalence(char_code,char_replace) values (521,'i');
insert into public.char_equivalence(char_code,char_replace) values (471,'U');
insert into public.char_equivalence(char_code,char_replace) values (256,'A');
insert into public.char_equivalence(char_code,char_replace) values (328,'n');
insert into public.char_equivalence(char_code,char_replace) values (476,'u');
insert into public.char_equivalence(char_code,char_replace) values (522,'I');
insert into public.char_equivalence(char_code,char_replace) values (325,'N');
insert into public.char_equivalence(char_code,char_replace) values (389,'b');
insert into public.char_equivalence(char_code,char_replace) values (174,'R');

alter table public.char_equivalence add column badchar text;
update public.char_equivalence set badchar = chr(char_code::int);


-- replace les caracteres non voulus par leur equivalence, les espaces sont convertis en underscore '_'
CREATE OR REPLACE FUNCTION public.replace_char(text)
  RETURNS text AS
$BODY$
 select array_to_string (array(select case when char_replace is null then c else char_replace end from  regexp_split_to_table(regexp_replace($1,E'\\\\s','_','g'), E'\\\\s*') t(c) left outer join char_equivalence ce on (ce.badchar = t.c)),'')
$BODY$
  LANGUAGE sql IMMUTABLE
  COST 100;

GRANT EXECUTE ON FUNCTION public.replace_char( TEXT )  TO public;

-- normalize supprime tous les signes de ponctuations, supprime les accents avec replace_char, et convertit en minuscule
CREATE OR REPLACE FUNCTION public.normalize( TEXT ) RETURNS TEXT as $$
 SELECT array_to_string( ARRAY
        ( 
		SELECT  unnest(regexp_split_to_array(lower(regexp_replace(replace_char($1), '[[:punct:]]',' ', 'g')), E'\\\\s+'))
        )
        ,' ');
$$ language sql immutable;
GRANT EXECUTE ON FUNCTION public.normalize( TEXT )  TO public;


