Accelerated C++ 7 generate sentence
来源:互联网 发布:网络双刃剑政治论文 编辑:程序博客网 时间:2024/06/05 02:42
split.h
see Note 6
grammar.txt
<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>
main.cpp
#include <iostream>#include <map>#include <string>#include <vector>#include <stdlib.h>#include <stdexcept>#include <fstream>#include "split.h"using std::cout; using std::endl; using std::domain_error;using std::string; using std::vector; using std::istream;using std::map; using std::ifstream; using std::logic_error;using std::cerr;typedef vector<string> Rule;typedef vector<Rule> Rule_collection;typedef map<string, Rule_collection> Grammar;Grammar read_grammar(ifstream& infile) { Grammar ret; string line; while(getline(infile, line)) { vector<string> entry = split(line); if(!entry.empty()) { Rule rules(entry.begin() + 1, entry.end()); ret[entry[0]].push_back(rules); } } return ret;}int nrand(int n) { if (n < 0 || n > RAND_MAX) throw domain_error("Argument to nrand is out of range"); const int bucket_size = RAND_MAX / n; int r; do r = rand() / bucket_size; while (r > n); return r;}bool bracketed(const string& s) { return s[0] == '<' && s[s.size() - 1] == '>';}void gen_aux(const Grammar& g, const string& s, vector<string>& v) { if (bracketed(s)){ Grammar::const_iterator it = g.find(s); if (it == g.end()) throw logic_error("empty error"); const Rule_collection& rc = it->second; const Rule r = rc[nrand(rc.size())]; for (Rule::size_type i = 0; i != r.size(); ++i) gen_aux(g, r[i], v); } else v.push_back(s);}vector<string> gen_sentence(const Grammar& g) { vector<string> ret; gen_aux(g, "<sentence>", ret); return ret;}int main() { string ifile = "grammar.txt"; ifstream infile(ifile.c_str()); // check that the open succeede if (!infile) { cerr << "error: unable to open input file: " << ifile << endl; return -1; } // generate the sentence Grammar g = read_grammar(infile); vector<string> sentence = gen_sentence(g); // write the first word, if any vector<string>::const_iterator it = sentence.begin(); if(!sentence.empty()) { cout << *it; ++it; } // write the rest of the word, each preceded by a space while (it != sentence.end()) { cout << " " << *it; ++it; } cout << endl; return 0;}
阅读全文
0 0
- Accelerated C++ 7 generate sentence
- Accelerated C++<4-7>
- 读《Accelerated C++》有感
- 【Accelerated C++】读书笔记(一)
- 【Accelerated C++】读书笔记(二)
- 【Accelerated C++】读书笔记(三)
- 【Accelerated C++】读书笔记(四)
- 【Accelerated C++】读书笔记(五)
- 【Accelerated C++】读书笔记(六)
- 【Accelerated C++】重点回顾
- 《Accelerated C++》 笔记摘录
- 《accelerated c++》---------第六章
- Accelerated C++Exercises' Resolutions
- 【Accelerated C++】重点回顾
- Accelerated C++<3-2>
- Accelerated C++<4-1>
- Accelerated C++<4-2>
- Accelerated C++<4-3>
- HDOJ4512 最长公共上升子序列
- C#日志方法
- leetcode 501. Find Mode in Binary Search Tree
- mysql的使用
- EventBus知识收集
- Accelerated C++ 7 generate sentence
- allegro学习之如何给过大电流的元件开窗散热
- __cdecl与__stdcall用途
- 远程登录管理工具SecureCRT连接VMWare下的ubantu
- Java8 lambda表达式常用方法
- Office超链接跳出错误信息由于本机的限制打不开的解决办法
- 将CentOSyum源改为国内的阿里云源
- 我在测试移动弱网时踩过的坑
- mysql 插入更新一条sql 搞定