CS106B Assignment #2 Part A : Random writing

CS106B Assignment #2 Part A : Random writing and Markov models of language

任务地址:https://see.stanford.edu/materials/icspacs106b/H16-Assign2ADTs.pdf

基于马尔可夫链的随机文段产生,而且可以自己设置马尔可夫阶数

#include <iostream>
#include "genlib.h"
#include "simpio.h"
#include "map.h"
#include "vector.h"
#include "random.h"

void CreateMap(string &line);
string GetSeed();
void GenerateText(string seed);

const int MAX_CHARS = 2000;

Map<Vector<char> > seedMap;
Vector<string> allseeds; // 获取全部seed用于GetSeed函数
int orderK;

void main() {
    
    Randomize();
    //获得马尔可夫链阶数k
    while(true) {
        cout << "What order of analysis? (a number from 1 to 10): ";
        orderK = GetInteger();
        if (orderK >= 1 && orderK <= 10) break;
        cout << "Invalid number" << endl;
    }
    //自己输入的text
    string text;
    text="We all stood watching, smiling and laughing as they darted past the cars and. They held their shopping bags over their heads just in case. They got soaked. But they were followed by a few who screamed and laughed like children all the way to their cars. And yes, I did. I ran. I got wet. I needed washing.Circumstances or people can take away your material possessions, they can take away your money, and they can take away your health. But no one can ever take away your precious memories. So, don't forget to make time and take the opportunities to make memories every day!Now some would laugh it off and scold her for being silly. Some might even ignore what was said. But this was a moment of affirmation in a young child's life. Time when innocent trust can be nurtured so that it will bloom into faith. Honey, you are absolutely right. Let's run through the rain. If get wet, well maybe we just needed washing.Mom said. Then off they ran.The entire crowd stopped dead silent. I swear you couldn't hear anything but the rain. We all stood silently. No one came or left in the next few minutes. Mom paused and thought for a moment about what she would say.Don't you remember? When you were talking to Daddy about his cancer, you said, If God can get us through this, he can get us through anything!";
    //创建Map
    CreateMap(text);
    //选择最初始的seed
    string seed = GetSeed();
    //把seed输入,生成文章
    GenerateText(seed);
    // clear
    seedMap.clear();
    allseeds.clear();
}


void CreateMap(string &line) {
    for(int i= 0; i< line.size() - orderK ; i++){
        string seed = line.substr(i, orderK); //种子
        allseeds.add(seed); //将种子加入全部种子的vector中
        if(i< line.size() - orderK){   //在for循环里还需判断防止越界
            Vector<char> temp;   // 中间变量
            char c= line[i+orderK];  //vector 的 待加 value
            if(seedMap.containsKey(seed)){   //如果map已经有,则需要取出vector加入c进去,用temp做中间变量
                temp = seedMap.getValue(seed);
                temp.add(c);
                seedMap.add(seed,temp);
                
            }
            else{
                temp.add(c);
                seedMap.add(seed,temp);   //如果map没有,则直接加入map
            }
        }
    }
}

string GetSeed() {
    Map<int> seedcount;
    int count;
    string seed;
    //制作包含次数的map 操作不变,仍是如果有,取出加1,没有则直接加入
    for (int i = 0;i< allseeds.size ();i++){
        if (seedcount.containsKey(allseeds[i])){
            count = seedcount.getValue(allseeds[i]);
            count++;
            seedcount.add(allseeds[i],count);
        }else{
            seedcount.add(allseeds[i],1);
        }
    }
    //用迭代器,找出seedcount中value最大的key,  也可以用for循环遍历
    Map<int>::Iterator itr = seedcount.iterator();
    int max=0;
    while (itr.hasNext()) {
        string key = itr.next();
        if(seedcount.getValue(key) > max){
            max = seedcount.getValue(key);
            seed = key;
        }
    }
    return seed;
}

void GenerateText(string seed) {
    Vector<char> seedvector;  //用雨取出key的value
    cout << seed;
    for(int i=0; i<MAX_CHARS; i++){
        if(seedMap.containsKey(seed))
            seedvector = seedMap.getValue(seed);
        else break;  //说明是结尾,没有下一个值
        int size = seedvector.size();
        if (size==0) break;
        int rand = RandomInteger(0,size-1); //随机选择下一个字符
        char c = seedvector[rand];
        cout << c;
        seed.erase(0,1); //删除首字符
        seed+= c;        //添加尾字符
        
    }
    cout<< endl <<endl;
}

结果展示:
 

What order of analysis? (a number from 1 to 10): 4
 they can take time and take time and. They can take away you remember? When ignore what washing.Circumstances or people cars and scold her forget to their health. But no one can take away your health. But no one cars and take away your material possessions, their health. But no one can take time and the can ever the rain. I got soaked. But no one can take away you are absolutely right ever they got soaked. But this washing bags over their shopping bags over their shopping and scold her forget to they held they got soaked. But no one can take time and take away your health. But no one cars and. They got soaked.
What order of analysis? (a number from 1 to 10): 5
 can take away your material possessions, they can take away your material possessions, they darted past the rain. If get wet. I needed washing.Circumstances or people can get wet. I needed washing.Circumstances or people can be nurtured so that it will bloom into faith. Honey, and scold her for being silly. Some might even ignore what was said. But no one can take away your material possessions, they darted past they can take away your heads just in case. They held their shopping bags over their shopping bags over their heads just in case. They held their heads just in case. They held their heads just in case. They held their shopping bags over their shopping bags over their heads just in case.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值