[ Pobierz całość w formacie PDF ]
.2/124.It starts at the first element in ret and looks at all the elements insequence.As you read the body of the for loop, remember that dereferencing a map iteratoryields a value of type pair.The first element of the pair holds the (const) key, andthe second element is the value associated with that key.We begin the for loop by writing the word that we're processing and a message:cout second.We want commas to separate those numbers, but we don't want a stray comma at the end.Therefore, we must treat either the first or the last element specially.We choose totreat the first one specially, by writing that element explicitly.It is safe to do sobecause every element of ret represents a word with at least one reference to it.Havingwritten an element, we increment the iterator to indicate that we've done so.Then thewhile loop iterates through the remaining elements (if any) of the vector.For eachelement, it writes a comma, followed by the value of the element.This document is created with the unregistered version of CHM2PDF Pilot7.4 Generating sentencesWe'll wrap up this chapter with a slightly more complicated example: We can use a map to write a program thattakes a description of a sentence structure a grammar and generates random sentences that meet that description.For example, we might describe an English sentence as a noun and a verb, or as a noun, a verb, and an object, andso on.The sentences that we can construct will be more interesting if we can handle complicated rules.For example, ratherthan saying merely that a sentence is a noun followed by a verb, we might allow noun phrases, where a noun phrase iseither simply a noun or an adjective followed by a noun phrase.As a concrete example, given the following inputCategories Rulescatdogtablelargebrownabsurdjumpssitson the stairsunder the skywherever it wantstheour program might generatethe table jumps wherever it wantsThe program should always start by finding a rule for how to make a sentence.In this input, there is only one suchrule the last one in our table:theThis rule says that to make a sentence, we write the word the, a noun-phrase, a verb, and finally a location.Theprogram begins by randomly selecting a rule that matches.Evidently the program chose the ruleand then resolved the noun using the ruletableThe program must still resolve verb and location, which apparently it did by selectingjumpsfor the verb andwherever it wantsThis document is created with the unregistered version of CHM2PDF Pilotfor the location.Note that this last rule maps a category to several words that wind up in the generated sentence.7.4.1 Representing the rulesOur table contains two kinds of entries: categories, which are enclosed in angle brackets, and ordinary words.Eachcategory has one or more rules; each ordinary word stands for itself.When the program sees a string enclosed inangle brackets, we know that the string will represent a category, so we will have to make the program find a rule thatmatches the category and expand the right-hand part of that rule.If the program sees words that are unadorned byangle brackets, then we know that it will be able to place those words directly into the generated sentence.Thinking about how our program might operate, it appears that the program will read a description of how to createsentences, and then randomly generate a sentence.So the first question is: How should we store the description?When we generate sentences, we need to be able to match each category to a rule that will expand that category.Forexample, we first need to find the rule for how to create a ; from that rule, we will need to find rules for, , ; and so on.Apparently, we'll want a map that maps categories to thecorresponding rules.But what kind of map? The categories are easy: We can store them as strings, so the key type of our map will bestring.The value type is more complicated.If we look at the table again, we can see that any given rule may be a collectionof strings.For example, the category sentence is associated with a rule that has four components: the word the andthree other strings, which are themselves categories.We know how to represent values of this kind: We canuse a vector to hold each rule.The problem is that each category may appear more than once in the input.For example, in our sample input description, the category appears three times, as do the categoriesand.Because these categories appear three times, each one will have three matching rules.The easiest way to manage multiple instances of the same key will be to store each collection of rules in its ownvector.Thus, we'll store the grammar in a map from string to vectors, which themselves hold vector.This type is quite a mouthful.Our program will be clearer if we introduce synonyms for our intermediate types.Wesaid that each rule is a vector, and that each category maps to a vector of these rules
[ Pobierz całość w formacie PDF ]