我如何在std :: string中随机创建拼写错误?

时间:2013-11-04 19:52:07

标签: c++ string random chatbot

好的,所以我正在使用聊天机器人,我有一个名为m_sResponse的私有std :: string。该字符串使用

输出
void print_response() const {
    if(m_sResponse.length() > 0) {
        std::cout << m_sResponse << std::endl;
    }
}

我想创建一个错误拼写m_sRensponse的函数,让我们说5%的时间让聊天机器人更像人。我该如何做到这一点?

3 个答案:

答案 0 :(得分:5)

为了使它看起来更逼真,我会根据键盘布局(例如QWERTY)制作map<char,vector<char>>个合适的'替换'键。基本上,如果你的拼写错误是“响应”而不是“责任”似乎更真实,因为“w”和“e”是彼此相邻的。您还需要随机删除或插入字母。我将频率分配给“错误”,然后分配每种错误的频率。

现在你已经得到了这个以及处理随机性方面(if(rand(100)<5))的其他答案,你应该能够复制所需的拼写错误处理程序。

答案 1 :(得分:0)

伪代码:

if rand(100) < 5
    randomIndex = rand(string.length())
    randomChar = rand(26)
    string[randomIndex] = randomChar

答案 2 :(得分:0)

您可以使用随机种子并使用%5,就像20%的时间一样。

if((rand() % 5) == 0) {
          int t = rand() & m_sResponse.length();
          char a = m_sResponse[t];
          m_sResponse[t] = m_sResponse[t+1];
          m_sResponse[t+1] = a;
}