首页 > 其他 > 详细

Book Review of “The practice of programming” (Ⅲ)

时间:2015-12-07 00:36:16      阅读:136      评论:0      收藏:0      [点我收藏+]

The practice of programming


Chapter 3 Design and Implementation

In this section, we focus on one kind problem: generate random English text that reads well.

The solution is called ‘The Markov Chain Algorithm‘, and its main idea is shown as follows:

  First we scan an article and save it in a special chart. For example, we use two adjacent words as prefix, by searching which we can get all the possible following words. We use ‘hashtable‘, ‘list‘, ‘vector‘ to realize this function. The details are left out.

  Then we are to generate a paragraph based on the data above. The first several words are picked up randomly. And each time, we choose one of its suffix randomly. To ensure the random in just one scanning, we use a stractage, that is, ‘use a variable "n" to count the current number of suffixes, use a string to save the previous chosen suffix. each time the present suffix can replace the previous suffix by the possibilition 1/n. When the scan is over, the suffix chosen is determined.‘

  Last, to limit to total length of the generated paragraph, we add a symbol of ending to each prefix‘s map. Also, use a variable to count to total length we have generated.

 

Book Review of “The practice of programming” (Ⅲ)

原文:http://www.cnblogs.com/Christen/p/5024777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!