Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active September 8, 2016 14:56
Show Gist options
  • Save CliffordAnderson/a1ac3141828b504ee756 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/a1ac3141828b504ee756 to your computer and use it in GitHub Desktop.
XQuery for Pig Latin
xquery version "3.0";
let $word := "thermometer"
let $first := fn:substring($word,1,1)
let $last := fn:substring($word,2)
return $last || $first || "ay"
xquery version "3.0";
let $word := "thermometer"
let $first := fn:replace($word, "^(.*?)[a,e,i,o,u].*", "$1")
let $last := fn:replace($word, "^.*?([a,e,i,o,u].*)", "$1")
return $last || $first || "ay"
xquery version "3.0";
declare function local:move-letters ($x as xs:string*, $y as xs:string*) as xs:string*
{
let $vowels := ("a","e","i","o","u")
return
if ($x = $vowels) then fn:string-join(($x, $y, "ay"))
else local:move-letters($y[1], ($y[position()=(2 to last())], $x))
};
let $word := "thermometer"
let $seq := fn:string-to-codepoints($word) ! fn:codepoints-to-string(.)
return local:move-letters($seq[1], $seq[position()=(2 to last())])
xquery version "3.0";
declare function local:move-letters ($x as xs:string*, $y as xs:string*) as xs:string*
{
let $vowels := ("a","e","i","o","u")
return
if ($x = $vowels) then fn:string-join(($x, $y, "ay"))
else local:move-letters($y[1], ($y[position()=(2 to last())], $x))
};
declare function local:pig-parse($word as xs:string) as xs:string*
{
let $seq := fn:string-to-codepoints($word) ! fn:codepoints-to-string(.)
return
if (fn:count($seq) > 1)
then local:move-letters($seq[1], $seq[position()=(2 to last())])
else $seq || "ay"
};
let $phrase := "Will you adjust the thermometer?"
let $punctuation := fn:substring($phrase,fn:string-length($phrase))
let $phrase := fn:translate($phrase,$punctuation,"")
let $seq := fn:tokenize($phrase, " ")
for $word in $seq
return local:pig-parse($word)
xquery version "3.0";
declare function local:move-letters ($x as xs:string*, $y as xs:string*) as xs:string*
{
let $vowels := ("a","e","i","o","u")
return
if ($x = $vowels) then fn:string-join(($x, $y, "ay"))
else local:move-letters($y[1], ($y[position()=(2 to last())], $x))
};
declare function local:parse-word($word as xs:string) as xs:string*
{
let $seq := fn:string-to-codepoints($word) ! fn:codepoints-to-string(.)
return
if (fn:count($seq) > 1)
then local:move-letters($seq[1], $seq[position()=(2 to last())])
else $seq || "ay"
};
declare function local:parse-sentence($sentence as xs:string) as xs:string*
{
let $punctuation := fn:substring($sentence,fn:string-length($sentence))
let $phrase := fn:translate($sentence,$punctuation, "")
let $words :=
let $seq := fn:tokenize($phrase, " ")
for $word in $seq
return local:parse-word($word)
return fn:string-join($words, " ") || $punctuation
};
local:parse-sentence("Will you adjust the thermometer?")
@joewiz
Copy link

joewiz commented Sep 8, 2016

An alternative to platin2-reg.xqy, using a single fn:analyze-string() instead of the double fn:replace():

xquery version "3.0";

let $word := "thermometer"
let $analyze := fn:analyze-string($word, "^(.*?)([aeiou].*)")
let $first := $analyze/fn:match/fn:group[1]
let $last := $analyze/fn:match/fn:group[2]
return $last || $first || "ay"

The fn:analyze-string() here evaluates to:

<fn:analyze-string-result xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <fn:match>
        <fn:group nr="1">th</fn:group>
        <fn:group nr="2">ermometer</fn:group>
    </fn:match>
</fn:analyze-string-result>

fn:analyze-string() is quite a powerful function. For some additional uses, see de-hyphenating mis-hyphenated words at https://gist.github.com/joewiz/6312943 (essentially, a spell-checker) and searching for cross-reference strings in text to improve TEI ref values at https://gist.github.com/joewiz/2369367de3babba30e0aad8c9beec893.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment