Reverse the gender of a string: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 320:
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!</pre>
 
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
{{works with|jq}}
'''Also works with both jaq and gojq, the Rust and Go implementations of jq'''
 
In this entry, we use the Unicode character classes for "open", "close",
and "other" punctuation, but show the test for a small set of specific
punctuation characters.
 
<syntaxhighlight lang="jq">
def swaps: {
"She": "He", "she": "he", "Her": "His", "her": "his", "hers": "his", "He": "She",
"he": "she", "His": "Her", "his": "her", "him": "her"
};
 
def isPunctuation:
type == "string" and
length == 1 and
# test("[!\"#%&'()*,-./:;?@\\[\\]\\\\_{}¡§«¶·»¿]")
# open, close, other
test("\\p{Ps}|\\p{Pe}|\\p{Po}|")
;
def reverseGender:
reduce splits(" *") as $word ([];
swaps[$word] as $s
| if $s then . + [$s]
elif ($word[-1:] | isPunctuation)
then swaps[$word[:-1]] as $s
| if $s then . + [$s + $word[-1:]]
else . + [$word]
end
else . + [$word]
end)
| join(" ");
 
def sentences: [
"She was a soul stripper. She took his heart!",
"He was a soul stripper. He took her heart!",
"She wants what's hers, he wants her and she wants him!",
"Her dog belongs to him but his dog is hers!"
];
 
sentences[]
| reverseGender
</syntaxhighlight>
{{Output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
Line 1,864 ⟶ 1,913:
=={{header|Wren}}==
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Char
 
var swaps = {
9,479

edits