- Forward


Pluralization
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Idea:
    • We often need slightly different text depending on whether we are dealing with a single item or multiple items
  • Example:
    • There is  1 poodle  available for adoption.
    • There are 3 poodles available for adoption.
Review
Back SMYC Forward
A Solution that is Prone to Error
javaexamples/programmingpatterns/pluralization/Pluralization.java (Fragment: 1)
 
Review (cont.)
Back SMYC Forward
A Cumbersome Solution
javaexamples/programmingpatterns/pluralization/Pluralization.java (Fragment: 2)
 
Thinking About the Problem
Back SMYC Forward
  • The Need:
    • Think in one form (e.g., the singular) and convert to other forms (e.g., the plural) when needed
  • Unfortunately:
    • This is a difficult natural language processing problem
  • Fortunately:
    • You are good at converting from one form to the other
The Pattern
Back SMYC Forward
  • Getting Started:
    • Write a method that is passed both the singular and plural as parameters, and returns the appropriate one
  • Then:
    • Create methods that use it
The Pattern (cont.)
Back SMYC Forward
The General Purpose Method
javaexamples/programmingpatterns/pluralization/Pluralize.java (Fragment: form)
 
Example
Back SMYC Forward
Specific Words
javaexamples/programmingpatterns/pluralization/Pluralize.java (Fragment: is)
 
Example
Back SMYC Forward
Regular Nouns
javaexamples/programmingpatterns/pluralization/Pluralize.java (Fragment: regular)
 
Example (cont.)
Back SMYC Forward
Using the Pattern
javaexamples/programmingpatterns/pluralization/Pluralization.java (Fragment: 3)
 
javaexamples/programmingpatterns/pluralization/Pluralization.java (Fragment: 4)
 
There's Always More to Learn
Back -