I\'m trying to write a lexical database for storing words comprised of roots and patterns, and I was wondering how I could create a column that will combine the root and pattern
I must admit to not liking to do much string manipulation in sql/plpgsql functions. Perl has an operator for replacing regexp matches with generated replacements, which works fairly nicely:
create or replace function splice_to_word(root text, root_i text)
returns text strict immutable language plperl as $$
my $roots = shift;
my $template = shift;
$template =~ s{(\d+)}{substr($roots,$1-1,1)}ge;
return $template;
$$;
There is some nastiness in that postgresql arrays don't seem to be translated into Perl lists, so I've assumed the roots are passed in as a string, e.g.:
select root, root_i, splice_to_word(array_to_string(root, ''), root_i) from data
select
root,
root_i,
translate(root_i, "123", array_to_string(root,'')) as word_i
NATURAL JOIN tbl_patterns
NATURAL JOIN tbl_patterns_triliteral
where root is not null and root_i is not null;