Lec 1: Pluralizer Project
Writing a function in Scheme which can pluralize a noun. Based on a set of examples.
This is by no means completed but a first stab at writing something in Scheme. Using standard procedures and definitions along with some of the extras provided by the simply Scheme library.
The program below uses the function I wrote to solve the problem and run through a set of tests.
Here's the outcome from running the program...
'(dogs dogs => ✅)
'(boxes boxes => ✅)
'(wolves wolves => ✅)
'(mangoes mangoes => ✅)
'(cities cities => ✅)
'(rays rays => ✅)
'(alumni alumni => ✅)
'(crises crises => ✅)
'(criteria criteria => ✅)
'(news news => ✅)
'(fish fish => ✅)
'(sheep sheep => ✅)
'(men men => ✅)
'(women women => ✅)
'(oxen oxen => ✅)
'(geese geese => ✅)
'(mothers-in-law mothers-in-law => ✅)
'(fathers-in-law fathers-in-law => ✅)
'(grandmothers grandmothers => ✅)
'(aunties aunties => ✅)
Though if you were to do a more complete set of words then this program would not be sufficient. I have a new found respect for any good pluralize() function.
Here are more details on the full complexity of the problem in a paper.
#lang racket
(require (planet dyoo/simply-scheme:2:2))
; this is the first attempt at writing a procedure which takes a word and makes it plural
(define (plural wd)
(word wd 's))
; go number 2
; helper functions
(define (length xs)
(if (null? xs)
0
(+ 1 (length (cdr xs)))))
(define (exists? wd lst)
(if (null? lst)
#f
(if (eqv? wd (car lst))
#t
(exists? wd (cdr lst)))))
(define (iter-match-pairs wd lst)
(when (not (null? lst))
(if (equal? wd (car (car lst)))
(car (cdr (car lst)))
(iter-match-pairs wd (cdr lst)))))
(define (ends-in-is wd)
(if (and (equal? 's (last wd)) (equal? 'i (last (butlast wd))))
#t
#f))
(define (ends-in-us wd)
(if (and (equal? 's (last wd)) (equal? 'u (last (butlast wd))))
#t
#f))
(define (ends-in-letter-y wd letters-lst)
(if (and
(equal? 'y (last wd))
(exists? (last (butlast wd)) letters-lst))
#t
#f))
(define (ends-in-o wd)
(if (equal? 'o (last wd))
#t
#f))
(define (ends-in-f-or-fe wd)
(if (and (equal? 'f (last wd)) (equal? 'e (last (butlast wd))))
#t
(if (equal? 'f (last wd))
#t
#f)))
(define (ends-in-s-x-z-ch-sh wd)
(if (or (equal? 's (last wd)) (equal? 'x (last wd)) (equal? 'z (last wd)))
#t
(if (or (and (equal? 'h (last wd)) (equal? 'c (last (butlast wd)))) (and (equal? 'h (last wd)) (equal? 's (last (butlast wd)))))
#t
#f)))
(define (ends-in-law wd)
(if (and (equal? 'w (last wd))
(equal? 'a (last (butlast wd)))
(equal? 'l (last (butlast (butlast wd))))
(equal? '- (last (butlast (butlast (butlast wd)))))
(equal? 'n (last (butlast (butlast (butlast (butlast wd))))))
(equal? 'i (last (butlast (butlast (butlast (butlast (butlast wd))))))))
#t
#f))
; defined vocabulary
; define list of irregular nouns and their plural form as a list of lists
(define irregular-nouns (list 'mouse 'mice 'foot 'tooth 'child 'goose 'ox 'woman 'man))
(define irregular-nouns-plurals (list (list 'mouse 'mice) (list 'foot 'feet) (list 'tooth 'teeth) (list 'child 'children) (list 'goose 'geese) (list 'ox 'oxen) (list 'woman 'women) (list 'man 'men)))
; define list of nouns with common plural form
(define common-plural-nouns (list 'news 'scissors 'furniture 'deer 'fish 'police 'sheep))
; define list of nouns with 'on' to 'a' in the plural form
(define on-a-nouns (list 'criterion 'phenomenon))
(define on-a-nouns-plurals (list (list 'criterion 'criteria) (list 'phenomenon 'phenomena)))
; define vowels and consonants
(define vowels (list 'a 'e 'i 'o 'u))
(define consonants (list 'b 'c 'd 'f 'g 'h 'j 'k 'l 'm 'n 'p 'q 'r 's 't 'v 'w 'x 'y 'z))
(define (plural_ wd)
(cond
; identify Irregular nouns
((exists? wd irregular-nouns) (iter-match-pairs wd irregular-nouns-plurals))
; identify common form plural nouns
((exists? wd common-plural-nouns) wd)
; Changing ‘on’ to ‘a’
((exists? wd on-a-nouns) (iter-match-pairs wd on-a-nouns-plurals))
; Changing ‘is’ to ‘es’
((ends-in-is wd) (word (butlast (butlast wd)) 'es))
; Changing ‘us’ to ‘i’
((ends-in-us wd) (word (butlast (butlast wd)) 'i))
; Adding ‘s’ to words ending with a ‘y’ preceded by a vowel
((ends-in-letter-y wd vowels) (word wd 's))
; Adding ‘ies’ to words ending with a ‘y’ preceded by a consonant
((ends-in-letter-y wd consonants) (word (butlast wd) 'ies))
; Adding ‘es’ to nouns ending with an ‘o’
((ends-in-o wd) (word wd 'es))
; Adding ‘ves’ for nouns ending with an ‘f’ or ‘fe’
((ends-in-f-or-fe wd) (word (butlast wd) 'ves))
; Adding 'es' for nouns ending with 's', 'x', 'z', 'ch' or 'sh'
((ends-in-s-x-z-ch-sh wd) (word wd 'es))
; Hyphenated relationships ending in 'in-law'
((ends-in-law wd) (word (plural_ (butlast (butlast (butlast (butlast (butlast (butlast (butlast wd)))))))) '-in-law))
(else (word wd 's))))
; Tests from: https://byjus.com/english/plural-noun/
(define (is_equal? wd1 wd2)
(if (equal? wd1 wd2)
(sentence wd1 wd2 '=> '✅)
(sentence wd1 wd2 '=> '❌)))
; Adding 's'
(is_equal? (plural_ 'dog) 'dogs)
; Adding 'es'
(is_equal? (plural_ 'box) 'boxes)
; Adding 'ves'
(is_equal? (plural_ 'wolf) 'wolves)
; Adding 'es' when the word ends with 'o'
(is_equal? (plural_ 'mango) 'mangoes)
; Adding ‘ies’ to words ending with a ‘y’ preceded by a consonant
(is_equal? (plural_ 'city) 'cities)
; Adding ‘s’ to words ending with a ‘y’ preceded by a vowel
(is_equal? (plural_ 'ray) 'rays)
; Changing ‘us’ to ‘i’
(is_equal? (plural_ 'alumnus) 'alumni)
; Changing ‘is’ to ‘es’
(is_equal? (plural_ 'crisis) 'crises)
; Changing ‘on’ to ‘a’
(is_equal? (plural_ 'criterion) 'criteria)
; Nouns with a common singular and plural form
(is_equal? (plural_ 'news) 'news)
(is_equal? (plural_ 'fish) 'fish)
(is_equal? (plural_ 'sheep) 'sheep)
; Irregular nouns
(is_equal? (plural_ 'man) 'men)
(is_equal? (plural_ 'woman) 'women)
(is_equal? (plural_ 'ox) 'oxen)
(is_equal? (plural_ 'goose) 'geese)
; Plural form for hyphenated nouns and relationships
(is_equal? (plural_ 'mother-in-law) 'mothers-in-law)
(is_equal? (plural_ 'father-in-law) 'fathers-in-law)
(is_equal? (plural_ 'grandmother) 'grandmothers)
(is_equal? (plural_ 'aunty) 'aunties)