根据输入文本生成随机文本
来源:互联网 发布:wake瑜伽软件下载 编辑:程序博客网 时间:2024/06/04 18:29
根据输入文本,通过建立马尔科夫过程,即根据前几个单词来决定下个输出的单词,该程序来源《编程珠玑》第15.3,原文用的c,本文用c++实现。
该程序使用的数据结构式“后缀数组”,一种简单且高效的数据结构。
#include "stdafx.h"#include <iostream>#include <string>#include <fstream>#include <sstream>#include <time.h>using namespace std;class RandomText{public:RandomText(string str):filename(str),nword(0){}~RandomText(){}void Input();static int WordCmp(const void *p,const void *q);static int CharCmp(const char *first, const char *second);void Sort();void Bin();const char *Skip(const char *p, int index);private:static const int approximation=1; //阶数,即根据前两个单词来推下一个单词char inputchars[5000]; //保存文件char *word[1000]; //保存每个单词的起始地址int nword; //单词的个数string filename; //文件名称};/*********************************将文件的单词保存到inputchars数组中,word数组中将保存每个单词的起始地址*********************************/void RandomText::Input(){fstream infile(filename.c_str());if(!infile)return;string line;word[0]=inputchars;while(getline(infile,line)){istringstream stream(line);string temp;while(stream>>temp){strcpy(word[nword],temp.c_str());word[nword+1]=word[nword]+strlen(word[nword])+1;nword++;}}infile.close();}/********************************比较两个字符数组*********************************/int RandomText::WordCmp(const void *p, const void *q){const char *first=*(const char**)(p);const char *second=*(const char**)(q);return CharCmp(first,second);}/***************************比较两个字符串****************************/int RandomText::CharCmp(const char *first, const char *second){int n=approximation;for(;*first==*second;first++,second++){if(*first==0&&--n==0)return 0;}return *first-*second;}/************************对word数组进行排序****************************/void RandomText::Sort(){for(int i=0;i!=approximation;i++){word[nword][i]=0;}for(int i=0;i!=approximation;i++){cout<<word[i]<<" ";}qsort(word,nword,sizeof(word[0]),WordCmp);}/*************************主要实现部分**************************/void RandomText::Bin(){srand((unsigned int)time(NULL));const char *phrase=inputchars;for(int wordleft=1000;wordleft>0;wordleft--){int small=-1;int big=nword;while(small+1!=big){int middle=(small+big)/2;if(CharCmp(word[middle],phrase)<0)small=middle;elsebig=middle;}char *p;int i=1;for(;CharCmp(phrase,word[big+i])==0;i++); p=word[big+(rand()%i)];phrase=Skip(p,approximation);if(strlen(Skip(phrase,approximation))==0)break;cout<<Skip(phrase,approximation)<<" ";}}/*********************p为字符数组返回p的第index个单词的起始地址*********************/const char *RandomText::Skip(const char *p, int index){int k=approximation;while(k!=0){while(*p!=0)p++;p++;k--;}return p;}int _tmain(int argc, _TCHAR* argv[]){RandomText rt("test.txt");rt.Input();rt.Sort();rt.Bin();return 0;}
- 根据输入文本生成随机文本
- 随机文本生成
- 随机文本生成
- 随机生成文本程序
- 生成随机文本
- 根据文本生成位图
- 随机文本生成与马尔科夫链
- 利用马尔可夫链生成随机文本
- <<编程珠玑>> 生成随机文本
- 随机文本生成技术---order-k马尔科夫链文本生成技术
- 文本随机
- GAN之根据文本描述生成图像
- GAN之根据文本描述生成图像
- 文本输入
- 文本输入
- 根据文本对输入的字符串进行加密解密操作
- 利用随机单词生成文本(《编程珠玑》第15章)
- 随机数的产生--随机文本串的生成
- USACO/nuggets 4.1.1
- 疯狂的bug【select -IO复用中】
- 几个小了的IoC容器
- OpenGl实验3
- java尝试用户协同推荐 无优化版
- 根据输入文本生成随机文本
- Autoreload of modules in IPython
- android broadcast receiver
- Ajax实例
- memento (备忘录模式)
- hdu 2602 Bone Collector(0/1背包)
- Junit学习笔记----(马士兵老师)
- ZOJ2334-Monkey King【二项堆】
- 使用chrome调试ajax加载本地文件的时候出现Failed to load resource: Origin null is not allowed by Access-Control-Allow