سؤال

Ok so I'm working on a chatbot and I have a private std::string called m_sResponse. This string is outputted using

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

I want to create a function that will misspell m_sRensponse let's say 5% of the time so the chatbot seams more human like. How would I accomplish this?

هل كانت مفيدة؟

المحلول

To make it seem more realistic, I'd make a map<char,vector<char>> of appropriate 'substitution' keys based off of keyboard layouts (e.g. QWERTY). Basically, it seems more real if your typo is "responsw" than "responsl" since "w" and "e" are next to each other. You'll also want to randomly delete or insert letters too. I'd assign a frequency to "errors" and then a frequency of each kind of error.

Now that you've got this and the other answers handling the randomness aspect (if(rand(100)<5)), you should be able to replicate the desired typo handler.

نصائح أخرى

Pseudocode:

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

You can use a random seed and use %5 for like 20% of the time ish.

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top