pipian

pluralize.cpp

Dec 16th, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <string>
  2.  
  3. #define MAP(suffix, plural)                                             \
  4.   if (singular.length() >= std::string(suffix).length()) {                              \
  5.     if (singular.substr(singular.length() - std::string(suffix).length(), std::string(suffix).length()) == std::string(suffix)) { \
  6.       return singular.substr(0, singular.length() - std::string(suffix).length()) + plural; \
  7.     }                                                                   \
  8.   }
  9.  
  10. /* NOTE: We assume all lower case! */
  11. std::string pluralize(std::string singular) {
  12.   /* Irregular */
  13.   MAP("person", "people");
  14.   MAP("man", "men");
  15.   MAP("child", "children");
  16.   MAP("sex", "sexes");
  17.   MAP("move", "moves");
  18.   MAP("cow", "kine");
  19.   MAP("zombie", "zombies");
  20.  
  21.   /* Mappings */
  22.   MAP("quiz", "quizzes");
  23.   if (singular == "oxen") {
  24.     return "oxen";
  25.   }
  26.   if (singular == "ox") {
  27.     return "oxen";
  28.   }
  29.   MAP("mice", "mice");
  30.   MAP("lice", "lice");
  31.   MAP("mouse", "mice");
  32.   MAP("louse", "lice");
  33.   MAP("matrix", "matrices");
  34.   MAP("vertix", "vertices");
  35.   MAP("indix", "indices");
  36.   MAP("matrex", "matrices");
  37.   MAP("vertex", "vertices");
  38.   MAP("index", "indices");
  39.   MAP("x", "xes");
  40.   MAP("ch", "ches");
  41.   MAP("ss", "sses");
  42.   MAP("sh", "shes");
  43.   if (singular.length() >= 2) {
  44.     if (singular.substr(singular.length() - 1, 1) == "y") {
  45.       if (singular.substr(singular.length() - 2, 1) != "a" &&
  46.           singular.substr(singular.length() - 2, 1) != "e" &&
  47.           singular.substr(singular.length() - 2, 1) != "i" &&
  48.           singular.substr(singular.length() - 2, 1) != "o" &&
  49.           singular.substr(singular.length() - 2, 1) != "u" &&
  50.           singular.substr(singular.length() - 2, 1) != "y") {
  51.         return singular.substr(0, singular.length() - 1) + "ies";
  52.       }
  53.     }
  54.   }
  55.   MAP("quy", "quies");
  56.   MAP("hive", "hives");
  57.   if (singular.length() >= 3) {
  58.     if (singular.substr(singular.length() - 2, 2) == "fe") {
  59.       if (singular.substr(singular.length() - 2, 1) != "f") {
  60.         return singular.substr(0, singular.length() - 2) + "ves";
  61.       }
  62.     }
  63.   }
  64.   MAP("lf", "lves");
  65.   MAP("rf", "rves");
  66.   MAP("sis", "ses");
  67.   MAP("ta", "ta");
  68.   MAP("ia", "ia");
  69.   MAP("tum", "ta");
  70.   MAP("ium", "ia");
  71.   MAP("buffalo", "buffaloes");
  72.   MAP("tomato", "tomatoes");
  73.   MAP("bus", "buses");
  74.   MAP("alias", "aliases");
  75.   MAP("status", "statuses");
  76.   MAP("octopi", "octopii");
  77.   MAP("viri", "virii");
  78.   MAP("octopus", "octopuses");
  79.   MAP("virus", "viruses");
  80.   MAP("axis", "axes");
  81.   MAP("testis", "testes");
  82.   MAP("s", "s");
  83.   return singular + "s";
  84. }
Advertisement
Add Comment
Please, Sign In to add comment