1
;;; inflections.el ---
2
3
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
4
5
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
6
;;          Howard Yeh <hayeah at gmail dot com>
7
8
;; Keywords: ruby rails languages oop
9
;; $URL$
10
;; $Id$
11
12
;;; License
13
14
;; This program is free software; you can redistribute it and/or
15
;; modify it under the terms of the GNU General Public License
16
;; as published by the Free Software Foundation; either version 2
17
;; of the License, or (at your option) any later version.
18
19
;; This program is distributed in the hope that it will be useful,
20
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
;; GNU General Public License for more details.
23
24
;; You should have received a copy of the GNU General Public License
25
;; along with this program; if not, write to the Free Software
26
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27
28
;;; Code:
29
30
(defvar inflection-singulars    nil)
31
(defvar inflection-plurals      nil)
32
(defvar inflection-irregulars   nil)
33
(defvar inflection-uncountables nil)
34
35
(defmacro define-inflectors (&rest specs)
36
  `(progn
37
     ,@(loop for (type . rest) in specs collect
38
	   (case type
39
	     (:singular `(push ',rest inflection-singulars))
40
	     (:plural `(push ',rest inflection-plurals))
41
	     (:irregular `(push ',rest inflection-irregulars))
42
	     (:uncountable `(setf inflection-uncountables
43
				  (append ',rest inflection-uncountables)))))))
44
45
(define-inflectors
46
  (:plural "$" "s")
47
  (:plural "s$" "s")
48
  (:plural "\\(ax\\|test\\)is$" "\\1es")
49
  (:plural "\\(octop\\|vir\\)us$" "\\1i")
50
  (:plural "\\(alias\\|status\\)$" "\\1es")
51
  (:plural "\\(bu\\)s$" "\\1ses")
52
  (:plural "\\(buffal\\|tomat\\)o$" "\\1oes")
53
  (:plural "\\([ti]\\)um$" "\\1a")
54
  (:plural "sis$" "ses")
55
  (:plural "\\(?:\\([^f]\\)fe\\|\\([lr]\\)f\\)$" "\\1\\2ves")
56
  (:plural "\\(hive\\)$" "\\1s")
57
  (:plural "\\([^aeiouy]\\|qu\\)y$" "\\1ies")
58
  (:plural "\\(x\\|ch\\|ss\\|sh\\)$" "\\1es")
59
  (:plural "\\(matr\\|vert\\|ind\\)ix\\|ex$" "\\1ices")
60
  (:plural "\\([m\\|l]\\)ouse$" "\\1ice")
61
  (:plural "^\\(ox\\)$" "\\1en")
62
  (:plural "\\(quiz\\)$" "\\1zes")
63
64
  (:singular "s$" "")
65
  (:singular "\\(n\\)ews$" "\\1ews")
66
  (:singular "\\([ti]\\)a$" "\\1um")
67
  (:singular "\\(\\(a\\)naly\\|\\(b\\)a\\|\\(d\\)iagno\\|\\(p\\)arenthe\\|\\(p\\)rogno\\|\\(s\\)ynop\\|\\(t\\)he\\)ses$" "\\1\\2sis")
68
  (:singular "\\(^analy\\)ses$" "\\1sis")
69
  (:singular "\\([^f]\\)ves$" "\\1fe")
70
  (:singular "\\(hive\\)s$" "\\1")
71
  (:singular "\\(tive\\)s$" "\\1")
72
  (:singular "\\([lr]\\)ves$" "\\1f")
73
  (:singular "\\([^aeiouy]\\|qu\\)ies$" "\\1y")
74
  (:singular "\\(s\\)eries$" "\\1eries")
75
  (:singular "\\(m\\)ovies$" "\\1ovie")
76
  (:singular "\\(x\\|ch\\|ss\\|sh\\)es$" "\\1")
77
  (:singular "\\([m\\|l]\\)ice$" "\\1ouse")
78
  (:singular "\\(bus\\)es$" "\\1")
79
  (:singular "\\(o\\)es$" "\\1")
80
  (:singular "\\(shoe\\)s$" "\\1")
81
  (:singular "\\(cris\\|ax\\|test\\)es$" "\\1is")
82
  (:singular "\\(octop\\|vir\\)i$" "\\1us")
83
  (:singular "\\(alias\\|status\\)es$" "\\1")
84
  (:singular "^\\(ox\\)en" "\\1")
85
  (:singular "\\(vert\\|ind\\)ices$" "\\1ex")
86
  (:singular "\\(matr\\)ices$" "\\1ix")
87
  (:singular "\\(quiz\\)zes$" "\\1")
88
89
  (:irregular "stratum" "strate")
90
  (:irregular "syllabus" "syllabi")
91
  (:irregular "radius" "radii")
92
  (:irregular "addendum" "addenda")
93
  (:irregular "cactus" "cacti")
94
  (:irregular "child" "children")
95
  (:irregular "corpus" "corpora")
96
  (:irregular "criterion" "criteria")
97
  (:irregular "datum" "data")
98
  (:irregular "genus" "genera")
99
  (:irregular "man" "men")
100
  (:irregular "medium" "media")
101
  (:irregular "move" "moves")
102
  (:irregular "person" "people")
103
  (:irregular "man" "men")
104
  (:irregular "child" "children")
105
  (:irregular "sex" "sexes")
106
  (:irregular "move" "moves")
107
108
  (:uncountable "equipment" "information" "rice" "money" "species" "series" "fish" "sheep" "news"))
109
110
(defun singularize-string (str)
111
  (when (stringp str)
112
    (or (car (member str inflection-uncountables))
113
        (caar (member* (downcase str) inflection-irregulars :key 'cadr :test 'equal))
114
        (loop for (from to) in inflection-singulars
115
              for singular = (string=~ from str (sub to))
116
              when singular do (return singular))
117
        str)))
118
119
(defun pluralize-string (str)
120
  (when (stringp str)
121
    (or (car (member str inflection-uncountables))
122
        (cadar (member* (downcase str) inflection-irregulars :key 'car :test 'equal))
123
        (loop for (from to) in inflection-plurals
124
              for plurals = (string=~ from str (sub to))
125
              when plurals do (return plurals))
126
        str)))
127
128
(provide 'inflections)