You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Ewa Szwed <ew...@gmail.com> on 2013/12/06 18:54:03 UTC

multiple replacements in one BIND

Hi,

Is it possible to do multiple replacements in one BINDING.

I am trying to avoid the following:

BIND( replace(?place, "Alabama", "al") AS ?place_1)
BIND( replace(?place1, "Alaska", "ak") AS ?place_3)
.
.
.

50 times

BIND( replace(?place50, "Wyoming", "wy") AS ?place_final)

Re: multiple replacements in one BIND

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Dec 6, 2013 at 12:54 PM, Ewa Szwed <ew...@gmail.com> wrote:
> Is it possible to do multiple replacements in one BINDING.
>
> I am trying to avoid the following:
>
> BIND( replace(?place, "Alabama", "al") AS ?place_1)
> BIND( replace(?place1, "Alaska", "ak") AS ?place_3)
> …
> BIND( replace(?place50, "Wyoming", "wy") AS ?place_final)

It *is* possible to replace multiple occurrences of a match, but I
don't think you can do what you're trying to do. For instance, what
you can do is:

  select * where {
    values ?place { "A B C D E" }
    bind( replace( ?place, "(B|D)", "[$1]" ) as ?place_updated )
  }

and that produces

  ?place  ?place_updated
  "A B C D E"     "A [B] C [D] E"

so you've done multiple replacements, but each match was replaced with
the same text (parameterized by $1).  However, you want to replace
matches with _different_ text depending on what the particular match
was, and I don't think you'll be able to do that, since I don't see a
way to conditionalize the replacement text.

For what it's worth, the pattern of replacements you're describing is
very similar to one of the hits for "split string in sparql", a
answers.semanticweb.com question,  Split and trim strings in SPARQL
[http://answers.semanticweb.com/questions/19162/split-and-trim-strings-in-sparql]
.

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: multiple replacements in one BIND

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Dec 6, 2013 at 1:41 PM, Andy Seaborne <an...@apache.org> wrote:
> On 06/12/13 17:54, Ewa Szwed wrote:
>> Is it possible to do multiple replacements in one BINDING.
>> I am trying to avoid the following:
>>
>> BIND( replace(?place, "Alabama", "al") AS ?place_1)
>> BIND( replace(?place1, "Alaska", "ak") AS ?place_3)
>> …
>> BIND( replace(?place50, "Wyoming", "wy") AS ?place_final)
>
> 1/ If you can work with exact replacements:
> You can create a translation table with VALUES:
>
>   VALUES (?place ?rewrite) {
>     ("Alabama"  "al")
> …
>     ("Wyoming", "wy")
>   }
>
> the optionally join it with the pattern that found ?place:
>
> SELECT *
> {
>   ... ?place ...
>
>   OPTIONAL {
>     VALUES (?place ?rewrite) {
>       ("Alabama"  "al")
>       ("Alaska"   "ak")
>       ..
>       ("Wyoming" "wy")
>   } }
>
> }
>
> The OPTIONAL copes with the case of no match.

I think that Ewa wanted to do multiple replacements on the same
string.  E.g., to turn "I moved from New Hampshire to New York" would
turn into "I moved from nh to ny".  This (unless I'm misunderstanding
it) looks like it's just replacing one one exact match with its
replacement.

//JT
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: multiple replacements in one BIND

Posted by Andy Seaborne <an...@apache.org>.
On 06/12/13 17:54, Ewa Szwed wrote:
> Hi,
>
> Is it possible to do multiple replacements in one BINDING.
>
> I am trying to avoid the following:
>
> BIND( replace(?place, "Alabama", "al") AS ?place_1)
> BIND( replace(?place1, "Alaska", "ak") AS ?place_3)
> .
> .
> .
>
> 50 times
>
> BIND( replace(?place50, "Wyoming", "wy") AS ?place_final)
>


1/ If you can work with exact replacements:

You can create a translation table with VALUES:

   VALUES (?place ?rewrite) {
     ("Alabama"  "al")
     ("Alaska"   "ak")
     ...
     ("Wyoming", "wy")
   }

the optionally join it with the pattern that found ?place:

SELECT *
{
   ... ?place ...

   OPTIONAL {
     VALUES (?place ?rewrite) {
       ("Alabama"  "al")
       ("Alaska"   "ak")
       ..
       ("Wyoming" "wy")
   } }

}

The OPTIONAL copes with the case of no match.

2/
Or add a custom function:

   BIND( my:fixup(?place) AS ?place_final)

and write the translation in Java which can use a regex or any other 
string manipulation you want.

	Andy