reverse_the_gender_of_a_string.sf 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/ruby
  2. var male2female = <<'EOD'
  3. maleS femaleS, maleness femaleness, him her, himself herself, his her, his
  4. hers, he she, Mr Mrs, Mister Missus, Ms Mr, Master Miss, MasterS MistressES,
  5. uncleS auntS, nephewS nieceS, sonS daughterS, grandsonS granddaughterS,
  6. brotherS sisterS, man woman, men women, boyS girlS, paternal maternal,
  7. grandfatherS grandmotherS, GodfatherS GodmotherS, GodsonS GoddaughterS,
  8. fiancéS fiancéeS, husband wife, husbands wives, fatherS motherS, bachelorS
  9. spinsterS, bridegroomS brideS, widowerS widowS, KnightS DameS, Sir DameS,
  10. KingS QueenS, DukeS DuchessES, PrinceS PrincessES, Lord Lady, Lords Ladies,
  11. MarquessES MarchionessES, EarlS CountessES, ViscountS ViscountessES, ladS
  12. lassES, sir madam, gentleman lady, gentlemen ladies, BaronS BaronessES,
  13. stallionS mareS, ramS eweS, coltS fillieS, billy nanny, billies nannies,
  14. bullS cowS, godS goddessES, heroS heroineS, shirtS blouseS, undies nickers,
  15. sweat glow, jackarooS jillarooS, gigoloS hookerS, landlord landlady,
  16. landlords landladies, manservantS maidservantS, actorS actressES, CountS
  17. CountessES, EmperorS EmpressES, giantS giantessES, heirS heiressES, hostS
  18. hostessES, lionS lionessES, managerS manageressES, murdererS murderessES,
  19. priestS priestessES, poetS poetessES, shepherdS shepherdessES, stewardS
  20. stewardessES, tigerS tigressES, waiterS waitressES, cockS henS, dogS bitchES,
  21. drakeS henS, dogS vixenS, tomS tibS, boarS sowS, buckS roeS, peacockS
  22. peahenS, gander goose, ganders geese, friarS nunS, monkS nunS
  23. EOD
  24.  
  25. var m2f = male2female.split(/,\s*/).map { |tok| tok.words}
  26.  
  27. var re_plural = /E?S\z/
  28. var re_ES = /ES\z/
  29.  
  30. func gen_pluralize(m, f) {
  31. [
  32. [m - re_plural, f - re_plural],
  33. [m.sub(re_ES, 'es'), f.sub(re_ES, 'es')],
  34. [m.sub(re_plural, 's'), f.sub(re_plural, 's')],
  35. ]
  36. }
  37.  
  38. var dict = Hash()
  39.  
  40. for m,f in m2f {
  41. for x,y in gen_pluralize(m, f).map{.map{.lc}} {
  42. if (x ~~ dict) {
  43. dict{y} = x
  44. } else {
  45. dict{x, y} = (y, x)
  46. }
  47. }
  48. }
  49.  
  50. var gen_re = Regex.new('\b(' + dict.keys.join('|') + ')\b', 'i')
  51.  
  52. func copy_case(orig, repl) {
  53. var a = orig.chars
  54. var b = repl.chars
  55.  
  56. var uc = 0
  57. var min = [a, b].map{.len}.min
  58. for i in ^min {
  59. if (a[i] ~~ /^[[:upper:]]/) {
  60. b[i].uc!
  61. ++uc
  62. }
  63. }
  64.  
  65. uc == min ? repl.uc : b.join('')
  66. }
  67.  
  68. func reverse_gender(text) {
  69. text.gsub(gen_re, { |a| copy_case(a, dict{a.lc}) })
  70. }
  71. var rev = reverse_gender("She was a soul stripper. She took my heart!");
  72. say rev;
  73. assert_eq(rev, "He was a soul stripper. He took my heart!");