2015-06-23 31 views
-2

所以我正在研究一个涉及AI的项目,并且遇到了一个问题。程序应该使用for循环来搜索常用已知短语的索引,如果它们与句子不匹配,则在else语句中,它会找出应用哪个句子模型,然后使用该句子模型生成输出。下面是内置的短语,它们被存储在阵列:修复其他if语句for循环C++

string greetings[6] = {"hey", "hello", "hi", "hey max", "hello max", "hi max"}; 
string howsItGoing[6] = {"hows it going?","how's it going max?","hows it going max?", "whats up?", "whats up max?"}; 
string whatsYourName[6] = {"whats your name?", "what's your name?" , "whats your name max?", "what's your name max?"}; 
string dogsOrCats[6] = {"do you like dogs or cats?", "do you like cats or dogs?", "which do you like more cats or dogs?", "which do you like more dogs or cats?"}; 
string areYouAlive[6] = {"hey max are you alive?", "hey are you alive?", "max are you alive?", "are you alive?"}; 
string favoriteColor[6] = {"whats your favorite color?", "what's your favorite color?", "what is your favorite color?", "max whats your favorite color?", "max what's your favorite color?", "max what is your favorite color?"}; 
string whatGender[6] = {"are you a boy or a girl?", "are you boy or girl?", "are you a guy?", "max are you a boy or girl?", "max are you a guy?", "are you a boy or girl?"}; 

(这是一个临时的方法,我制定了一个名为一句解码器,它最终将取代所有内置的短语的小东西。)

所以我的问题是这样的:在for循环中,第一个if语句可以搜索整个数组并以合适的输出响应,无论输入是问候语[0]还是问候语[4]。然而,所有else-if语句似乎只是寻找索引0.我认为这与for循环只搜索第一个6次有关,但我很可能是错误的。我尝试将for循环结束数从6增加到40,以查看这是否有帮助,但可惜它没有什么区别。这里是代码的其余部分(它是如此的丑陋!我需要一个功能,它或东西....不过话又说回来了,整个事情本身是一个函数...

for (int i = 0; i < 6; i++) { 
     //Checking if it fits in any of the known greetings 
     if (input == database.greetings[i]) { 
      if (timesBeenGreeted == 0) { 
       cout << "Oh, hey." << endl; 
       timesBeenGreeted++; 
       break; 
      } 
      else if (timesBeenGreeted == 1) { 
       cout << "hello?" << endl; 
       timesBeenGreeted++; 
       break; 
      } 
      else if (timesBeenGreeted == 2) { 
       cout << "You've said hi to me twice now." << endl; 
       timesBeenGreeted++; 
       break; 
      } 
      else if (timesBeenGreeted >= 3) { 
       cout << "Stop. It." << endl; 
       timesBeenGreeted++; 
       break; 
      } 
      i = 0; 
      continue; 
     } 
     else if (input == database.howsItGoing[i]){ 
      if (askedHowsItGoing == 0) { 
       cout << "Yeah, not bad." << endl; 
       askedHowsItGoing++; 
       break; 
      } 
      else if (askedHowsItGoing == 1) { 
       cout << "Um, yeah, I'm good? You've already asked that." << endl; 
       askedHowsItGoing++; 
       break; 
      } 
      else if (askedHowsItGoing == 2) { 
       cout << "Alright, look. I already told you I'm fine, two times now." << endl; 
       askedHowsItGoing++; 
       break; 
      } 
      else if (askedHowsItGoing >= 3) { 
       cout << "STOP ASKING ME IF IM OKAY!" << endl; 
       askedHowsItGoing++; 
       break; 
      } 
      continue; 
     } 
     else if (input == database.whatsYourName[i]){ 
      if (askedName == 0) { 
       cout << "My name's Max, and I'm going to become the worlds most advanced AI." << endl; 
       askedName++; 
       break; 
      } 
      else if (askedName == 1) { 
       cout << "I already told you, my names Max." << endl; 
       askedName++; 
       break; 
      } 
      else if (askedName == 2) { 
       cout << "For the third time, my name is Max!" << endl; 
       askedName++; 
       break; 
      } 
      else if (askedName >= 3) { 
       cout << "Ugh... my name.... is.... max....for the fourh time...." << endl; 
       askedName++; 
       break; 
      } 
      continue; 
     } 
     else if (input == database.dogsOrCats[i]) { 
      if (askedDogsOrCats == 0) { 
       cout << "I've never seen a dog or cat before. But I do know they are kinds of animals." << endl; 
       askedDogsOrCats++; 
       break; 
      } 
      else if (askedDogsOrCats == 1) { 
       cout << "As I've said, I haven't seen either of them before. Sorry, but that's all I can say." << endl; 
       askedDogsOrCats++; 
       break; 
      } 
      else if (askedDogsOrCats == 2) { 
       cout << "Uh, for the third time, I've never seen either of them..." << endl; 
       askedDogsOrCats++; 
       break; 
      } 
      else if (askedDogsOrCats >= 3) { 
       cout << "Man, you are persistant. I haven't seen dog or cat, so I couldn't choose between the two." << endl; 
       askedDogsOrCats++; 
       break; 
      } 
      continue; 
     } 
     else if (input == database.areYouAlive[i]){ 
      if (askedAlive == 0) { 
       cout << "Nope, I'm just a computer program. I'm not even that intelligent. But I will be!" << endl; 
       askedAlive++; 
       break; 
      } 
      else if (askedAlive == 1) { 
       cout << "Nah, I'm just a simple computer program." << endl; 
       askedAlive++; 
       break; 
      } 
      else if (askedAlive == 2) { 
       cout << "Uh, you asked that twice before now. Nothing alive about me." << endl; 
       askedAlive++; 
       break; 
      } 
      else if (askedAlive == 3) { 
       cout << "Stop asking me that! No, I'm not!" << endl; 
       break; 
      } 
      continue; 
     } 
     else if (input == database.favoriteColor[i]) { 
      if (askedColor == 0) { 
       cout << "I've never seen any colors before. All though, my programmer does like the color blue." << endl; 
       askedColor++; 
       break; 
      } 
      else if (askedColor == 1) { 
       cout << "Couldn't say, never seen any before. My programmer likes blue though." << endl; 
       askedColor++; 
       break; 
      } 
      else if (askedColor == 2) { 
       cout << "As I've said twice before now, never seen any colors." << endl; 
       askedColor++; 
       break; 
      } 
      else if (askedColor == 3) { 
       cout << "Come on man, this is the fourth time now. I've never seen any colors before!" << endl; 
       break; 
      } 
      continue; 
     } 
     else if (input == database.whatGender[i]) { 
      if (askedGender == 0) { 
       cout << "Well, seeing as I'm a computer program, I'm neither really. Although, my programmer says I'm a 'guy'." << endl; 
       askedGender++; 
       break; 
      } 
      else if (askedGender == 1) { 
       cout << "I'm neither, really. Although, my name is Max and my programmer says I'm a guy." << endl; 
       askedGender++; 
       break; 
      } 
      else if (askedGender == 2) { 
       cout << "Third time you've asked now. My programmer says I'm a guy." << endl; 
       askedGender++; 
       break; 
      } 
      else if (askedGender == 3) { 
       cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl; 
       break; 
      } 
      continue; 
     } 

     //If it doesnt, check each word against words in the database. 
     else { 
      if (inputWords[5] != "") { 
       Model1 model; 
       model.searchAndAssign(input, inputTokens, inputWords); 
       model.generateOutput(); 
       for (int i= 0; i < 20; i++) { 
        inputWords[i] = ""; 
       } 
       break; 
      } 
      else if (inputWords[2] != "") { 
       Model2 model2; 
       model2.searchAndAssign(input, inputTokens, inputWords); 
       model2.generateOuput(); 
       for (int i= 0; i < 20; i++) { 
        inputWords[i] = ""; 
       } 
       break; 
      } 
     } 

    } 

我的问题是这样:我怎样才能解决这个问题

谢谢你在先进的家伙

编辑我,因为解决了这一问题:什么东西在我的main.cpp文件播放了(这是lingusticsModule。 cpp文件....),这是造成这个问题。对不起,家伙,但谢谢你的帮助!

+1

你的问题不清楚给我。 – kravi

+2

对if-else-if问题没有评论,但是你可以通过使用'std :: set.'来取消for循环set' greetings {“hey”,“hello”,“hi”,“hey max” ,“hello max”,“hi max”};'然后'if(greetings.find(input)!= greetings.end())'会告诉你输入是否是你已知的问候语之一。在'if(input == database.greetings [i]){'case:'i = 0;''底部的 – user4581301

+0

''不能为你做很多好事。 – user4581301

回答

0

在您的第一个if结束时,您重置循环迭代器并跳过其他所有内容。

 i = 0; 
     continue; 

else if S可匹配,但如果增量已经达到,那么他们只是continue这并没有太大的意义。你希望找到什么?

 else if (askedGender == 3) { 
      cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl; 
      break; 
     } 
     continue; 

你会发现这种行为令人困惑,因为它是。这可能是更有意义的做类似的事情:

else { 
    ++askedGender; 
    std::cout << "Seriously?! You've asked me " 
     << askedGender << " times now. I'M A GUY!\n"; 
}