Permalink
Browse files

Interface change

  • Loading branch information...
1 parent ccfb860 commit 0ce208c03d08080517667c2becd92f576efbae38 Sean Grove committed Nov 14, 2010
Showing with 55 additions and 26 deletions.
  1. +21 −14 README.md
  2. +34 −12 inflector.lisp
View
@@ -5,48 +5,55 @@ A common lisp library to easily pluralize and singularize English words.
This is a support package for the [Vana web framework][1], and is a port of the same from Rails' ActiveSupport module.
-
Installation
------------------
(ql:quickload 'vana-inflector)
Usage
-----------
-Basic Usage:
+Basic Usage, `plural-of` and `singular-of`:
(use-package :vana-inflector)
- > (pluralize "octopus")
+ > (plural-of "octopus")
"octopi"
- > (pluralize "datum")
+ > (plural-of "datum")
"data"
- > (singularize "children")
+ > (singular-of "children")
"child"
- > (singularize "cats")
+ > (singular-of "cats")
"cat"
- > (singularize "data")
+ > (singular-of "data")
"datum"
+Basic Usage, `pluralize`:
+
+ (use-package :vana-inflector)
+ > (pluralize 2 "octopus")
+ "octopi"
+ > (pluralize 1 "octopus")
+ "octopus"
+
Use `irregular` to add an irregular:
- > (singularize "feet")
+ > (singular-of "feet")
"feet"
> (irregular "foot" "feet")
- > (singularize "feet")
+ > (singular-of "feet")
"foot"
- > (pluralize "foot")
+ > (plural-of "foot")
"feet"
Use `uncountable` to add an uncountable:
- > (pluralize "advice")
+ > (plural-of "advice")
"advices"
- > (singularize "advice")
+ > (singular-of "advice")
"advice"
- > (singularize "advices")
+ > (singular-of "advices")
"advice"
> (uncountable "advice")
- > (pluralize "advice")
+ > (plural-of "advice")
"advice"
TODO
View
@@ -69,11 +69,11 @@
(defvar irregulars
(args->alist
"person" "people"
- "man" "men"
- "child" "children"
- "sex" "sexes"
- "move" "moves"
- "cow" "kine"))
+ "man" "men"
+ "child" "children"
+ "sex" "sexes"
+ "move" "moves"
+ "cow" "kine"))
;; Interface for adding new uncountables, querying, etc.
(defun uncountable (word)
@@ -109,21 +109,43 @@
singular
(-> singular irregulars)))
-(defun pluralize (word)
+(defun plural-of (word)
"Returns the plural of a word if it's singular, or itself if already plural"
(cond ((uncountable? word) word)
((irregular? word) (get-irregular-plural word))
(t (inflector-helper word plurals))))
-(defun singularize (word)
+(defun singular-of (word)
"Returns the singular of a word if it's singular, or itself if already singular"
(cond ((uncountable? word) word)
((irregular? word) (get-irregular-singular word))
(t (inflector-helper word singulars))))
+(defun pluralize (count word)
+ (if (not (= count 1))
+ (plural-of word)
+ word))
+
(defun inflector-helper (word regexes)
- (multiple-value-bind (string match-found?)
- (cl-ppcre:regex-replace (first (first regexes)) word (second (first regexes)))
- (if match-found?
- string
- (inflector-helper word (rest regexes)))))
+ (if (null regexes)
+ word
+ (multiple-value-bind (string match-found?)
+ (cl-ppcre:regex-replace (first (first regexes)) word (second (first regexes)))
+ (if match-found?
+ string
+ (inflector-helper word (rest regexes))))))
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

0 comments on commit 0ce208c

Please sign in to comment.