We'll wrap up this chapter with a slightly more complicated example:we can use a map to write a program that takes a desc Ription of a sentence structure-a Grammar-and generates random sentences that meet. For example, we might describe an 中文版 sentence as a noun and a verb, or as a noun, a verb, and an object, and.
The sentences that we can construct would be more interesting if we can handle complicated rules. For example, rather than saying merely this a sentence is a noun followed by a verb, we might allow noun phrases, where a Noun phrase is either simply a noun or, adjective by a followed noun. As a concrete example, given the following input
Categories Rules
<noun> cat
<noun> dog
<noun> table
<noun-phrase> <noun >
<noun-phrase> <adjective> <noun-phrase>
<adjective> Large
<adjective> Brown
<adjective> absurd
<verb> jumps
< Verb> sits
<location> on the stairs
<location> under the sky
< Location> wherever it wants
<sentence> the <noun-phrase> <verb> <location >
Our program might generate
The table jumps wherever it wants
#include <iostream> #include <string> #include <cctype> #include <map> #include <vector> #
Include <algorithm> #include <cstdlib> using namespace std;
typedef vector<string> rule;
typedef vector<rule> Rule_collection;
typedef map<string, rule_collection> Grammar; BOOL Spaces (char c) {return isspace (c);} bool Not_space (char c) {return!isspace (c);} vector<string> Split (c
Onst string& str) {typedef string::const_iterator ITER;
vector<string> ret;
iter i = Str.begin ();
while (I!= str.end ()) {//ignore leading blanks i = find_if (i, Str.end (), not_space);
Find the next word iter j = find_if (i, Str.end (), space);
Copy the characters in [I, J) if (I!= str.end ()) Ret.push_back (String (i, j));
i = j;
return ret; }//Read a grammar from a given input stream grammar Read_grammar (istream& in) {GramMar ret;
String line; Read the input while (getline) {//split the input into words vector<string> ent
ry = Split (line); if (!entry.empty ())//use of the category to store the associated rule ret[entry[0]].push_back (
Entry.begin () + 1, entry.end ());
return ret; BOOL Bracketed (const string& s) {return s.size () > 1 && s[0] = = ' < ' && s[s.size ()-1] = = ' &G
t; '; //return a random integer in the range [0, N) int nrand (int n) {if (n <= 0 | | n > Rand_max) throw D
Omain_error ("Argument to Nrand are out of range");
const int bucket_size = rand_max/n;
int R;
Do r = rand ()/bucket_size;
while (R >= N);
return R;
} void Gen_aux (const grammar& g, const string& Word, vector<string>& ret) {if (!bracketed (word))
{Ret.push_back (word);
else {//Locate the rule this corresponds to Word Grammar::const_iterator it = g.find (word);
if (it = = G.end ()) Throw logic_error ("empty rule");
Fetch the set of possible rules const rule_collection& c = it->second;
From which we select one at random const rule& r = C[nrand (C.size ())]; Recursively expand the selected rule for (Rule::const_iterator i = R.begin (); I!= r.end (); ++i) ge
N_aux (g, *i, ret);
} vector<string> gen_sentence (const grammar& g) {vector<string> ret;
Gen_aux (g, "<sentence>", ret);
return ret;
int main () {Grammar G=read_grammar (CIN);
for (int ii=0;ii<6;ii++) {vector<string> sentence = gen_sentence (g);
Vector<string>::const_iterator it = Sentence.begin ();
if (!sentence.empty ()) {cout << *it;
++it; }//write the rest of the words, each preceded by a spaces while (it!= sentence.end ()) {cout << "" &L t;< *it;
++it;
} cout << Endl;
}////generate the sentence//vector<string> sentence = gen_sentence (CIN);
Write the ' the ', if any//vector<string>::const_iterator it = Sentence.begin ();
if (!sentence.empty ())//{//cout << *it;
++it; ////write the rest of the words, each preceded by a spaces//while (it!= sentence.end ())//{/
cout << "" << *it;
++it;
}//cout << Endl;
return 0; }
Input:
<n> Cat
<n> Dog
<n> Pig
<np> <n>
<np> <a> <np>
<a> Beautiful
<a> Big
<a> Small
<v> Jumps
<v> sits
<v> runs
<l> under the sky
<l> on the table
<l> in the computer
<sentence> the <np> <v> <l>
^z
Output:
The beautiful big Dog runs in the computer
The pig runs on the table
The cat sits under the sky
The big cat jumps on the table
The big beautiful big cat sits in the computer
The beautiful small small beautiful cat runs under the sky
Please press any key to continue ...