SQL> drop table chajol.phrases;
Table dropped.
SQL> create table chajol.phrases as
2 select 'house, ,,, house ,,, house,' str from dual union all
3 select 'house house; house' str from dual union all
4 select '123 12345 - 345 123 345' str from dual union all
5 select '1 South, Southhampton' str from dual union all
6 select ' The Old ;.,-=` The Oldman ' str from dual union all
7 select ',The Old ,The Oldman' str from dual union all
8 select 'The Old Millers The Old' from dual union all
9 select 'The Old Millers The Old Millers' from dual union all
10 select '3 Mayers Court 3 Mayers Court' str from dual union all
11 select 'Manor House Manor' str from dual union all
12 select '2 Briar Cottages 2 Briar' str from dual;
Table created.
SQL>
SQL> create or replace function chajol.no_dup_words(ar_param varchar2, v_done varchar2 default 0)
2 return varchar2
3 as
4 v_working varchar2(4096) := '';
5 v_result varchar2(4096) := '';
6 v_word varchar2(4096) := '';
7 v_1char varchar2(1) := '';
8 begin
9 for i in 1..length(ar_param) loop
10 v_working := substr(ar_param, i, 1) || v_working;
11 end loop;
12 v_working := ltrim(replace(v_working, ' ', ' ')) || ' ';
13 while v_working is not null loop
14 v_word := ' '||trim(substr(v_working, 1 ,instr(v_working, ' ')))||' ';
15 v_result := v_result || ' ' || trim(v_word);
16 v_working := ' '||trim(replace(' '||v_working, v_word, ''))||' ';
17 v_1char := substr(trim(v_word), 1, 1);
18 if v_1char not between '0' and '9'
19 and v_1char not between 'A' and 'Z'
20 and v_1char not between 'a' and 'z'
21 and length(trim(v_word)) > 1
22 and trim(translate(upper(trim(v_word)),translate(upper(trim(v_word)), '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ' '), ' ')) is not null
23 then
24 v_word := ' '||substr(trim(v_word), 2) || ' ';
25 v_result := ' '||replace(v_result, ' ', ' ')||' ';
26 v_result := replace(v_result, v_word, '');
27 v_result := replace(trim(v_result), ' ', ' ');
28 v_working := replace(v_working, v_word, '');
29 end if;
30 v_working := ltrim(v_working);
31 end loop;
32 if v_done = 0 then
33 v_working := chajol.no_dup_words(v_result, 1);
34 end if;
35 v_working := v_result;
36 v_result := '';
37 for i in 1..length(v_working) loop
38 v_result := substr(v_working, i, 1) || v_result;
39 end loop;
40 return trim(v_result);
41 end;
42 /
Function created.
SQL>
SQL> select str, chajol.no_dup_words(str) ndw from chajol.phrases;
STR
----------------------------------
NDW
----------------------------------------------------------------------------------------------------------------------------------
house, ,,, house ,,, house,
,,, house,
house house; house
house;
123 12345 - 345 123 345
12345 - 123 345
1 South, Southhampton
1 South, Southhampton
The Old ;.,-=` The Oldman
Old ;.,-=` The Oldman
,The Old ,The Oldman
Old ,The Oldman
The Old Millers The Old
Millers The Old
The Old Millers The Old Millers
The Old Millers
3 Mayers Court 3 Mayers Court
3 Mayers Court
Manor House Manor
House Manor
2 Briar Cottages 2 Briar
Cottages 2 Briar
11 rows selected.
SQL>