1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Chapter 17. Dynamic functions</title>
<link rel="stylesheet" href="../diveintopython.css" type="text/css">
<link rev="made" href="mailto:f8dy@diveintopython.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
<meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
<meta name="description" content="Python from novice to pro">
<link rel="home" href="../toc/index.html" title="Dive Into Python">
<link rel="up" href="../toc/index.html" title="Dive Into Python">
<link rel="previous" href="../functional_programming/summary.html" title="16.8. Summary">
<link rel="next" href="stage1.html" title="17.2. plural.py, stage 1">
</head>
<body>
<table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td id="breadcrumb" colspan="5" align="left" valign="top">You are here: <a href="../index.html">Home</a> > <a href="../toc/index.html">Dive Into Python</a> > <span class="thispage">Dynamic functions</span></td>
<td id="navigation" align="right" valign="top"> <a href="../functional_programming/summary.html" title="Prev: “Summary”"><<</a> <a href="stage1.html" title="Next: “plural.py, stage 1”">>></a></td>
</tr>
<tr>
<td colspan="3" id="logocontainer">
<h1 id="logo"><a href="../index.html" accesskey="1">Dive Into Python</a></h1>
<p id="tagline">Python from novice to pro</p>
</td>
<td colspan="3" align="right">
<form id="search" method="GET" action="http://www.google.com/custom">
<p><label for="q" accesskey="4">Find: </label><input type="text" id="q" name="q" size="20" maxlength="255" value=" "> <input type="submit" value="Search"><input type="hidden" name="cof" value="LW:752;L:http://diveintopython.org/images/diveintopython.png;LH:42;AH:left;GL:0;AWFID:3ced2bb1f7f1b212;"><input type="hidden" name="domains" value="diveintopython.org"><input type="hidden" name="sitesearch" value="diveintopython.org"></p>
</form>
</td>
</tr>
</table>
<!--#include virtual="/inc/ads" -->
<div class="chapter" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="plural"></a>Chapter 17. Dynamic functions
</h2>
</div>
</div>
<div></div>
</div>
<div class="toc">
<ul>
<li><span class="section"><a href="index.html#plural.divein">17.1. Diving in</a></span></li>
<li><span class="section"><a href="stage1.html">17.2. plural.py, stage 1</a></span></li>
<li><span class="section"><a href="stage2.html">17.3. plural.py, stage 2</a></span></li>
<li><span class="section"><a href="stage3.html">17.4. plural.py, stage 3</a></span></li>
<li><span class="section"><a href="stage4.html">17.5. plural.py, stage 4</a></span></li>
<li><span class="section"><a href="stage5.html">17.6. plural.py, stage 5</a></span></li>
<li><span class="section"><a href="stage6.html">17.7. plural.py, stage 6</a></span></li>
<li><span class="section"><a href="summary.html">17.8. Summary</a></span></li>
</ul>
</div>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="plural.divein"></a>17.1. Diving in
</h2>
</div>
</div>
<div></div>
</div>
<div class="abstract">
<p>I want to talk about plural nouns. Also, functions that return other functions, advanced regular expressions, and generators.
Generators are new in <span class="application">Python</span> 2.3. But first, let's talk about how to make plural nouns.
</p>
</div>
<p>If you haven't read <a href="../regular_expressions/index.html" title="Chapter 7. Regular Expressions">Chapter 7, <i>Regular Expressions</i></a>, now would be a good time. This chapter assumes you understand the basics of regular expressions, and quickly descends into
more advanced uses.
</p>
<p>English is a schizophrenic language that borrows from a lot of other languages, and the rules for making singular nouns into
plural nouns are varied and complex. There are rules, and then there are exceptions to those rules, and then there are exceptions
to the exceptions.
</p>
<p>If you grew up in an English-speaking country or learned English in a formal school setting, you're probably familiar with
the basic rules:
</p>
<div class="orderedlist">
<ol type="1">
<li>If a word ends in S, X, or Z, add ES. “<span class="quote">Bass</span>” becomes “<span class="quote">basses</span>”, “<span class="quote">fax</span>” becomes “<span class="quote">faxes</span>”, and “<span class="quote">waltz</span>” becomes “<span class="quote">waltzes</span>”.
</li>
<li>If a word ends in a noisy H, add ES; if it ends in a silent H, just add S. What's a noisy H? One that gets combined with
other letters to make a sound that you can hear. So “<span class="quote">coach</span>” becomes “<span class="quote">coaches</span>” and “<span class="quote">rash</span>” becomes “<span class="quote">rashes</span>”, because you can hear the CH and SH sounds when you say them. But “<span class="quote">cheetah</span>” becomes “<span class="quote">cheetahs</span>”, because the H is silent.
</li>
<li>If a word ends in Y that sounds like I, change the Y to IES; if the Y is combined with a vowel to sound like something else,
just add S. So “<span class="quote">vacancy</span>” becomes “<span class="quote">vacancies</span>”, but “<span class="quote">day</span>” becomes “<span class="quote">days</span>”.
</li>
<li>If all else fails, just add S and hope for the best.</li>
</ol>
</div>
<p>(I know, there are a lot of exceptions. “<span class="quote">Man</span>” becomes “<span class="quote">men</span>” and “<span class="quote">woman</span>” becomes “<span class="quote">women</span>”, but “<span class="quote">human</span>” becomes “<span class="quote">humans</span>”. “<span class="quote">Mouse</span>” becomes “<span class="quote">mice</span>” and “<span class="quote">louse</span>” becomes “<span class="quote">lice</span>”, but “<span class="quote">house</span>” becomes “<span class="quote">houses</span>”. “<span class="quote">Knife</span>” becomes “<span class="quote">knives</span>” and “<span class="quote">wife</span>” becomes “<span class="quote">wives</span>”, but “<span class="quote">lowlife</span>” becomes “<span class="quote">lowlifes</span>”. And don't even get me started on words that are their own plural, like “<span class="quote">sheep</span>”, “<span class="quote">deer</span>”, and “<span class="quote">haiku</span>”.)
</p>
<p>Other languages are, of course, completely different.</p>
<p>Let's design a module that pluralizes nouns. Start with just English nouns, and just these four rules, but keep in mind that
you'll inevitably need to add more rules, and you may eventually need to add more languages.
</p>
</div>
</div>
<table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
<tr>
<td width="35%" align="left"><br><a class="NavigationArrow" href="../functional_programming/summary.html"><< Summary</a></td>
<td width="30%" align="center"><br> <span class="divider">|</span> <span class="thispage">1</span> <span class="divider">|</span> <a href="stage1.html" title="17.2. plural.py, stage 1">2</a> <span class="divider">|</span> <a href="stage2.html" title="17.3. plural.py, stage 2">3</a> <span class="divider">|</span> <a href="stage3.html" title="17.4. plural.py, stage 3">4</a> <span class="divider">|</span> <a href="stage4.html" title="17.5. plural.py, stage 4">5</a> <span class="divider">|</span> <a href="stage5.html" title="17.6. plural.py, stage 5">6</a> <span class="divider">|</span> <a href="stage6.html" title="17.7. plural.py, stage 6">7</a> <span class="divider">|</span> <a href="summary.html" title="17.8. Summary">8</a> <span class="divider">|</span>
</td>
<td width="35%" align="right"><br><a class="NavigationArrow" href="stage1.html">plural.py, stage 1 >></a></td>
</tr>
<tr>
<td colspan="3"><br></td>
</tr>
</table>
<div class="Footer">
<p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
</div>
</body>
</html>
|