// implementing the 'sentence transposition' feature void CBot::transpose( std::string &str ) { bool bTransposed = 0; for(int i = 0; i < transposListSize; ++i) { std::string first = transposList[i].first; insert_space(first); std::string second = transposList[i].second; insert_space(second); size_t pos = replace(str, first, second); if(pos != std::string::npos) { bTransposed = 1; } } if( !bTransposed ) { for( i = 0; i < transposListSize; ++i ) { std::string first = transposList[i].first; insert_space(first); std::string second = transposList[i].second; insert_space(second); size_t pos = replace(str, second, first); } } }
static void XMLCALL end_element(void *userData, const XML_Char *name) { user_data_t *user_data = (user_data_t *) userData; --user_data->depth; insert_space(user_data); int ret = snprintf(user_data->output + user_data->output_off, sizeof(user_data->output) - user_data->output_off, "</%s>", name); TEST_ASSERT_EQUAL(strlen(name) + 3, ret); // 3 are the tag characters: "</>" user_data->output_off += ret; }
static void XMLCALL start_element(void *userData, const XML_Char *name, const XML_Char **atts) { user_data_t *user_data = (user_data_t *) userData; insert_space(user_data); const int ret = snprintf(user_data->output + user_data->output_off, sizeof(user_data->output) - user_data->output_off, "<%s>", name); TEST_ASSERT_EQUAL(strlen(name) + 2, ret); // 2 are the tag characters: "<>" user_data->output_off += ret; ++user_data->depth; }
// make a search for the user's input // inside the database of the program void CBot::find_match() { response_list.clear(); // introduce thse new "string variable" to help // support the implementation of keyword ranking // during the matching process std::string bestKeyWord; std::vector<int> index_vector; for(int i = 0; i < nKnowledgeBaseSize; ++i) { vstring keyWordList; // declaring variable for holding keywords // puting keywords from the current record to the keyWordList variable copy(KnowledgeBase[i].keyword, keyWordList, MAX_INP); for(int j = 0; j < keyWordList.size(); ++j) { std::string keyWord(keyWordList[j]); // we inset a space character // before and after the keyword to // improve the matching process insert_space(keyWord); // there has been some improvements made in // here in order to make the matching process // a littlebit more flexible if( m_sInput.find(keyWord) != std::string::npos ) { if(keyWord.length() > bestKeyWord.length()) { bestKeyWord = keyWord; index_vector.clear(); index_vector.push_back(i); } else if(keyWord.length() == bestKeyWord.length()) { index_vector.push_back(i); } } } } if(index_vector.size() > 0) { m_sKeyWord = bestKeyWord; shuffle(index_vector, index_vector.size()); copy( KnowledgeBase[index_vector[0]].response, response_list, MAX_RESP ); } }
static void data_handler(void *userData, const XML_Char *s, int len) { user_data_t *user_data = (user_data_t *) userData; insert_space(user_data); // s is not zero-terminated char tmp_str[len+1]; strlcpy(tmp_str, s, len+1); int ret = snprintf(user_data->output + user_data->output_off, sizeof(user_data->output) - user_data->output_off, "%s", tmp_str); TEST_ASSERT_EQUAL(strlen(tmp_str), ret); user_data->output_off += ret; }
void CBot::handle_event(std::string str) { save_prev_event(); set_event(str); save_input(); insert_space(str); set_input(str); if(!same_event()) { find_match(); } restore_input(); }
void process_small_equation(char stringx[] , char left[] , char right[] , int leftI , int rightI , char token) { if(testing){ printf("left: |%s| Right: |%s| token:%c\n" , left ,right , token); printf("s: |%s|\n\n" , stringx); } //char replace[N] ="XHelloX"; char replace[N]; //printf("s: |%s|\n" , s ); math_token_process(left , right, replace, token); // printf("output: |%s|\n", replace); /* solve the - or + problem when the return result doesn't have any token * -3 -3 always minus - * -3 +7 could have minus or plus : have to solve * -1 * 2 minus * -1 * -1 plus : have to solve * 1 / -1 minus * -1 / 1 minus * -1 / -1 plus : have to solve */ double left_double = strtod (left, NULL); //double right_double = strtod (right, NULL); double result_double = strtod (replace, NULL); //(minus plus) return problem solved if(left_double < 0 && leftI!=0) { if(result_double>0) { insert_space(replace,0,1); replace[0] = '+'; // printf("ase"); } } // printf("\nreplace count:%d\n" , getLen(replace) ); //printf("s: |%s|(before repalced) lIndex:%d ,rIndex:%d\n\n" , stringx , leftI ,rightI); substring_replace(stringx,replace ,leftI, rightI); if(testing){ printf("repalce objs :: |%s|\n" , replace); printf("s: |%s|(repalced) lIndex:%d ,rIndex:%d\n\n" ,stringx, leftI ,rightI); } }
int input_line(char *string, int length) { char curline[2000]; /* edit buffer */ int noline; unsigned c; int more; int i; if (first) { poolinit(); /* build line pool */ first = 0; } noline = 1; /* no line fetched yet */ for (cl=cp=0; cl<length && cl<(int)sizeof(curline); ) { if (usrbrk()) { clrbrk(); break; } switch (c=input_char()) { case F_RETURN: /* CR */ t_sendl("\r\n", 2); /* yes, print it and */ goto done; /* get out */ case F_CLRSCRN: /* clear screen */ asclrs(); t_sendl(curline, cl); ascurs(0, cp); break; case F_CSRUP: if (noline) { /* no line fetched yet */ getnext(); /* getnext so getprev gets current */ noline = 0; /* we now have line */ } bstrncpy(curline, getprev(), sizeof(curline)); prtcur(curline); break; case F_CSRDWN: noline = 0; /* mark line fetched */ bstrncpy(curline, getnext(), sizeof(curline)); prtcur(curline); break; case F_INSCHR: insert_space(curline, sizeof(curline)); break; case F_DELCHR: delchr(1, curline, sizeof(curline)); /* delete one character */ break; case F_CSRLFT: /* Backspace */ backup(curline); break; case F_CSRRGT: forward(curline, sizeof(curline)); break; case F_ERSCHR: /* Rubout */ backup(curline); delchr(1, curline, sizeof(curline)); if (cp == 0) { t_char(' '); t_char(0x8); } break; case F_DELEOL: t_clrline(0, t_width); if (cl > cp) cl = cp; break; case F_NXTWRD: i = next_word(curline); while (i--) { forward(curline, sizeof(curline)); } break; case F_PRVWRD: i = prev_word(curline); while (i--) { backup(curline); } break; case F_DELWRD: delchr(next_word(curline), curline, sizeof(curline)); /* delete word */ break; case F_NXTMCH: /* Ctl-X */ if (cl==0) { *string = EOS; /* terminate string */ return(c); /* give it to him */ } /* Note fall through */ case F_DELLIN: case F_ERSLIN: while (cp > 0) { backup(curline); /* backup to beginning of line */ } t_clrline(0, t_width); /* erase line */ cp = 0; cl = 0; /* reset cursor counter */ t_char(' '); t_char(0x8); break; case F_SOL: while (cp > 0) { backup(curline); } break; case F_EOL: while (cp < cl) { forward(curline, sizeof(curline)); } while (cp > cl) { backup(curline); } break; case F_TINS: /* toggle insert mode */ mode_insert = !mode_insert; /* flip bit */ break; default: if (c > 255) { /* function key hit */ if (cl==0) { /* if first character then */ *string = EOS; /* terminate string */ return c; /* return it */ } t_honk_horn(); /* complain */ } else { if ((c & 0xC0) == 0xC0) { if ((c & 0xFC) == 0xFC) { more = 5; } else if ((c & 0xF8) == 0xF8) { more = 4; } else if ((c & 0xF0) == 0xF0) { more = 3; } else if ((c & 0xE0) == 0xE0) { more = 2; } else { more = 1; } } else { more = 0; } if (mode_insert) { insert_space(curline, sizeof(curline)); } curline[cp++] = c; /* store character in line being built */ t_char(c); /* echo character to terminal */ while (more--) { c= input_char(); insert_hole(curline, sizeof(curline)); curline[cp++] = c; /* store character in line being built */ t_char(c); /* echo character to terminal */ } if (cp > cl) { cl = cp; /* keep current length */ curline[cp] = 0; } } break; } /* end switch */ } /* If we fall through here rather than goto done, the line is too long simply return what we have now. */ done: curline[cl++] = EOS; /* terminate */ bstrncpy(string,curline,length); /* return line to caller */ /* Save non-blank lines. Note, put line zaps curline */ if (curline[0] != EOS) { putline(curline,cl); /* save line for posterity */ } return 0; /* give it to him/her */ }
void CBot::preprocess_input() { cleanString(m_sInput); UpperCase(m_sInput); insert_space(m_sInput); }