Last active
September 8, 2016 14:56
-
-
Save CliffordAnderson/a1ac3141828b504ee756 to your computer and use it in GitHub Desktop.
XQuery for Pig Latin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.0"; | |
let $word := "thermometer" | |
let $first := fn:substring($word,1,1) | |
let $last := fn:substring($word,2) | |
return $last || $first || "ay" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative to
platin2-reg.xqy
, using a singlefn:analyze-string()
instead of the doublefn:replace()
:The
fn:analyze-string()
here evaluates to: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 TEIref
values at https://gist.github.com/joewiz/2369367de3babba30e0aad8c9beec893.