Accelerated C++ 7.4 生成句子

来源:互联网 发布:java 数组分割 编辑:程序博客网 时间:2024/06/08 07:20

We'll wrap up this chapter with a slightly more complicated example: We can use a map to write a program that takes 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, and so on.

The sentences that we can construct will be more interesting if we can handle complicated rules. For example, rather than saying merely that a sentence is a noun followed by a verb, we might allow noun phrases, where a noun phrase is either simply a noun or an adjective followed by a noun phrase. 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
 
输入:
<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
输出:
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
请按任意键继续. . .
原创粉丝点击