Accelerated C++ 7 generate sentence

本文介绍了一个基于文本的语法生成器,该生成器能够从定义好的文法文件中读取规则并随机生成符合这些规则的句子。通过使用C++实现,文章详细展示了如何解析文法文件、构建语法树以及生成句子的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值