Guest User

Untitled

a guest
Nov 24th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. def pluralize(x):
  2. "May not always be accurate. Add exceptions as you find them. See https://www.grammarly.com/blog/plural-nouns/"
  3.  
  4. # Add to this
  5. exceptions = {
  6. "bus": "buses",
  7. "gas": "gasses",
  8. "roof": "roofs",
  9. "halo": "halos",
  10. "fish": "fish",
  11. "belief": "beliefs",
  12. "chef": "chefs",
  13. "chief": "chiefs",
  14. "photo": "photos",
  15. "piano": "pianos",
  16. "child": "children",
  17. "goose": "geese",
  18. "man": "men",
  19. "woman": "women",
  20. "tooth": "teeth",
  21. "foot": "feet",
  22. "mouse": "mice",
  23. "person": "people",
  24. "series": "series",
  25. "species": "species",
  26. "deer": "deer"
  27. }
  28.  
  29. # Handle caps
  30. lower = x.lower()
  31.  
  32. # Handle exceptions
  33. for i in exceptions.keys():
  34. if lower == i:
  35. return exceptions[i]
  36.  
  37.  
  38. if lower.endswith("y") and len(x) >= 2:
  39. for i in ["a", "e", "i", "o", "u"]:
  40. if lower[-2] == i:
  41. return x+"s"
  42. return x[:-1]+"es"
  43.  
  44. if lower.endswith("us"):
  45. return x[:-2]+"i"
  46.  
  47. if lower.endswith("is"):
  48. return x[:-2]+"es"
  49.  
  50. if lower.endswith("on"):
  51. return x[:-2]+"a"
  52.  
  53. for i in ["f", "fe"]:
  54. if lower.endswith(i):
  55. return x[:-1]+"ves"
  56.  
  57. for i in ["s", "sh", "ch", "x", "z", "o"]:
  58. if lower.endswith(i):
  59. return x+"es"
  60.  
  61. # Default
  62. return x+"s"
  63.  
  64. # Test set
  65. for i in [
  66. "cat",
  67. "bus",
  68. "gas",
  69. "wolf",
  70. "roof",
  71. "city",
  72. "boy",
  73. "potato",
  74. "halo",
  75. "cactus",
  76. "analysis",
  77. "phenomenon",
  78. "fish"
  79. ]:
  80. print(f"{i} {pluralize(i)}")
  81.  
  82. # You can test it
  83. while True:
  84. print(pluralize(input("> ")))
Advertisement
Add Comment
Please, Sign In to add comment