Reverse the gender of a string: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 7:
The returned string should contain this initial string, with all references to be gender switched.
 
<langsyntaxhighlight lang="pseudocode">print rev_gender("She was a soul stripper. She took my heart!")
He was a soul stripper. He took my heart!</langsyntaxhighlight>
 
 
Line 17:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="11l">F reverse_gender(=s)
V words = [‘She’, ‘she’, ‘Her’, ‘her’, ‘hers’, ‘He’, ‘he’, ‘His’, ‘his’, ‘him’]
V repls = [‘He_’, ‘he_’, ‘His_’, ‘his_’, ‘his_’, ‘She_’, ‘she_’, ‘Her_’, ‘her_’, ‘her_’]
Line 29:
print(reverse_gender(‘He was a soul stripper. He took her heart!’))
print(reverse_gender(‘She wants what's hers, he wants her and she wants him!’))
print(reverse_gender(‘Her dog belongs to him but his dog is hers!’))</langsyntaxhighlight>
 
{{out}}
Line 40:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
DEFINE COUNT="10"
PTR ARRAY words(COUNT)
Line 132:
Test("She wants what's hers, he wants her and she wants him!")
Test("She, she, Her, her, hers, He, he, His, his, him")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Reverse_the_gender_of_a_string.png Screenshot from Atari 8-bit computer]
Line 155:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="rebol">reverseGender: function [str][
ret: new str
entries: ["She" "she" "Her" "her" "hers" "He" "he" "His" "his" "him"]
Line 169:
print reverseGender "He was a soul stripper. He took her heart!"
print reverseGender "She wants what's hers, he wants her and she wants him!"
print reverseGender "Her dog belongs to him but his dog is hers!"</langsyntaxhighlight>
 
{{out}}
Line 182:
 
To avoid swapping words which have already been swapped, thereby nullifying the original swap, I've appended an underscore to each replacement word and then removed all the underscores when all swaps have been made. This assumes, of course, that the text didn't include any underscores to start with.
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isWordChar(s As String) As Boolean
Line 228:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 239:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 260:
fmt.Println(t)
fmt.Println(reverseGender(t))
}</langsyntaxhighlight>
 
{{out}}
Line 276:
{{works with|汉语拼音}}
{{works with|文言文}}
<syntaxhighlight lang ="haskell">id</langsyntaxhighlight>
 
=={{header|J}}==
Line 290:
So, for now, we limit ourselves to the simple case specified in the task example, and do not even do all that great of a job there, either:
 
<langsyntaxhighlight Jlang="j">cheaptrick=: rplc&(;:'She He He She')</langsyntaxhighlight>
 
And, the task example:
 
<langsyntaxhighlight Jlang="j"> cheaptrick 'She was a soul stripper. She took my heart!'
He was a soul stripper. He took my heart!
cheaptrick cheaptrick 'She was a soul stripper. She took my heart!'
She was a soul stripper. She took my heart!</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|J}}
<langsyntaxhighlight lang="java">public class ReallyLameTranslationOfJ {
 
public static void main(String[] args) {
Line 316:
return s;
}
}</langsyntaxhighlight>
 
<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}}==
{{trans|Kotlin}}
<langsyntaxhighlight Julialang="julia">module ReverseGender
 
const MARKER = "\0"
Line 342 ⟶ 391:
@show ReverseGender.reverse("He was a soul stripper. He took her heart!")
@show ReverseGender.reverse("She wants what's hers, he wants her and she wants him!")
@show ReverseGender.reverse("Her dog belongs to him but his dog is hers!")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
This program uses a similar approach to the FreeBASIC entry:
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun reverseGender(s: String): String {
Line 364 ⟶ 413:
println(reverseGender("She wants what's hers, he wants her and she wants him!"))
println(reverseGender("Her dog belongs to him but his dog is hers!"))
}</langsyntaxhighlight>
 
{{out}}
Line 373 ⟶ 422:
His dog belongs to her but her dog is his!
</pre>
 
=={{header|Lua}}==
Sufficient for the task as worded, but without attempting to go beyond (because several indeterminate cases exist). It does at least demonstrate an idiomatic way of doing multiple simultaneous substring substitutions.
<syntaxhighlight lang="lua">function sufficient(s)
local xref = { She="He", He="She" }
return (s:gsub("(%w+)", function(s) return xref[s] or s end))
end
s = "She was a soul stripper. She took my heart!"
print(sufficient(s))
print(sufficient(sufficient(s)))
print(sufficient(sufficient(s)) == s)</syntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart!
She was a soul stripper. She took my heart!
true</pre>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">cap = function(w) // (capitalize a word)
return w[0].upper + w[1:]
end function
Line 406 ⟶ 470:
test "He was a soul stripper. He took her heart!"
test "She wants what's hers, he wants her and she wants him!"
test "Her dog belongs to him but his dog is hers!"</langsyntaxhighlight>
 
{{out}}
Line 423 ⟶ 487:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import re, strutils
 
const
Line 439 ⟶ 503:
echo reverseGender("He was a soul stripper. He took her heart!")
echo reverseGender("She wants what's hers, he wants her and she wants him!")
echo reverseGender("Her dog belongs to him but his dog is hers!")</langsyntaxhighlight>
 
{{out}}
Line 449 ⟶ 513:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="objeck">class ReallyLameTranslationOfJ {
function : Main(args : String[]) ~ Nil {
s := "She was a soul stripper. She took my heart!";
Line 467 ⟶ 531:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 477 ⟶ 541:
=={{header|Perl}}==
A minimal implementation, using a hash to manage swaps. But this approach breaks down if, for instance, 'him' were to replace 'me', as 'her' can't be used to map to both 'his' and 'him'.
<langsyntaxhighlight lang="perl">my %swaps = (
'she' => 'he',
'his' => 'her',
Line 494 ⟶ 558:
$original = qq{She was this soul sherpa. She took his heart! They say she's going to put me on a shelf.\n};
print $swapped = gender_swap($original);
print $reverted = gender_swap($swapped);</langsyntaxhighlight>
{{out}}
<pre>He was this soul sherpa. He took her heart! They say he's going to put me on a shelf.
Line 503 ⟶ 567:
words is a pair-list, ie "she","he" maps both ways, with first-upper-case handled too, and<br>
replacing the words right->left means no need to fiddle with indexes when lengths differ.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"she"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"he"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"him"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"her"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"hers"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"his"</span><span style="color: #0000FF;">}</span>
Line 542 ⟶ 606:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">&</span><span style="color: #000000;">r</span><span style="color: #0000FF;">&</span><span style="color: #000000;">rr</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 556 ⟶ 620:
=={{header|PowerShell}}==
{{trans|J}} (Made more PowerShelly.)
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Switch-Gender ([string]$String)
{
Line 575 ⟶ 639:
Switch-Gender "She was a soul stripper. She took my heart!"
Switch-Gender (Switch-Gender "She was a soul stripper. She took my heart!")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 583 ⟶ 647:
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
# -*- coding: utf-8 -*- #
Line 706 ⟶ 770:
return "".join([ word+switch[gen] for word,gen in zip(text[::2],text[1::2])])+text[-1]
print rev_gender(text)</langsyntaxhighlight>
'''Output:'''
<pre>
Line 721 ⟶ 785:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang at-exp racket
 
Line 816 ⟶ 880:
by the cannibal propensity he nourished in his untutored youth.
}))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 838 ⟶ 902:
For extremely limited circumstances such as this example, this should suffice. Notice case matching and replication. Handles contractions, but ignores embedded matching text.
 
<syntaxhighlight lang="raku" perl6line>say S:g:ii/«'she'»/he/ given "She was a soul stripper. She took my heart! They say she's going to put me on a shelf.";</langsyntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart! They say he's going to put me on a shelf.</pre>
Line 849 ⟶ 913:
 
Given the large size of the table (list), it would make more sense to put the words in a separate file instead of coding them in-line (within the computer program).
<langsyntaxhighlight lang="rexx">/*REXX program reverse genderizes a text string (that may contain gender-specific words)*/
parse value linesize()-1 with sw @ @. !. /*get screen width, nullify some vars.*/
parse arg old
Line 1,552 ⟶ 1,616:
if $\='' then say strip($)
say
return</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]]. <br>
Line 1,591 ⟶ 1,655:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Reverse the gender of a string
 
Line 1,659 ⟶ 1,723:
next
return alist2
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,677 ⟶ 1,741:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/cpBaoMf/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/0dajvapgRRChRZaZgpeqnQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object RevGender extends App {
val s = "She was a soul stripper. She took my heart!"
println(cheapTrick(s))
Line 1,688 ⟶ 1,752:
}
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var male2female = <<'EOD'
maleS femaleS, maleness femaleness, him her, himself herself, his her, his
hers, he she, Mr Mrs, Mister Missus, Ms Mr, Master Miss, MasterS MistressES,
Line 1,759 ⟶ 1,823:
func reverse_gender(text) {
text.gsub(gen_re, { |a| copy_case(a, dict{a.lc}) })
}</langsyntaxhighlight>
 
Example:
<langsyntaxhighlight lang="ruby">say reverse_gender("She was a soul stripper. She took my heart!");</langsyntaxhighlight>
{{out}}
<pre>He was a soul stripper. He took my heart!</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Construct the mapping variables from the source mapping
apply {{} {
global genderMap genderRE
Line 1,820 ⟶ 1,884:
string map $genderMap &
]}]
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts [reverseGender "She was a soul stripper. She took my heart!"]\n
puts [reverseGender "When a new-hatched savage running wild about his native
woodlands in a grass clout, followed by the nibbling goats, as if
Line 1,831 ⟶ 1,895:
who were the wives of unconquerable warriors. There was excellent
blood in his veins-royal stuff; though sadly vitiated, I fear,
by the cannibal propensity he nourished in his untutored youth."]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,849 ⟶ 1,913:
=={{header|Wren}}==
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Char
 
var swaps = {
Line 1,878 ⟶ 1,942:
]
 
for (sentence in sentences) System.print(reverseGender.call(sentence))</langsyntaxhighlight>
 
{{out}}
9,485

edits