void Dictionary::printInteractiveSuggestions(Words const& active_words, unsigned const show) const { assert(!active_words.empty()); assert(show > 0); vector<pair<double, Word>> suggestions; suggestions.reserve(active_words.size()); puts("Suggestions:"); { DividedWordList dl(wordLength()); auto it = keys_.cbegin(); do { dl.build(*this, active_words, *it); suggestions.push_back(make_pair(-dl.entropy(), *it)); } while (++it != keys_.cend()); } sort(suggestions.begin(), suggestions.end()); { auto it = suggestions.cbegin(); unsigned i = 0; do { printf("\t%2u: ", ++i); describeWord(stdout, it->second); putchar('\n'); } while (++it != suggestions.cend() && i < show); } putchar('\n'); }
void test_sentence(PersonRecog &pr, const char *sentence) { Words words; pr.recog(sentence, words); for (int i=0; i<words.size(); i+=1) printf("%s\n", words[i].c_str()); }
virtual void onServerResponse(const Words& words) { if (words.size() == 3 && words[0] == "OK") printf("Server version: Game %s, build ID %s\n", words[1].c_str(), words[2].c_str()); else printf("Invalid response to version query\n"); }
bool submissionsSequence(const Words& words, unsigned int& i) { if (i + 1 >= words.size()) return false; if (words[i] == "From" || words[i] == "from" || words[i] == "From:" || words[i] == "from:" || words[i] == "Merged" || words[i] == "Integrated") return true; if (i + 2 >= words.size()) return false; if (words[i] == "submitted" && words[i + 1] == "by") { i += 1; return true; } if (words[i] == "Folded" && words[i + 1] == "in") { i += 1; return true; } if (words[i] == "Rolled" && words[i + 1] == "in") { i += 1; return true; } if (words[i] == "Checked" && words[i + 1] == "in") { i += 1; return true; } if (i + 3 >= words.size()) return false; if (words[i] == "sent" && words[i + 1] == "in" && words[i + 2] == "by") { i += 2; return true; } return false; }
void Dictionary::recursiveInteractive(Words const& active_words) const { if (active_words.empty()) { puts("No choices remain.\n"); } else if (active_words.size() == 1) { fputs("Only choice remaining is: ", stdout); describeWord(stdout, active_words.front()); puts("\n"); } else { printf("%u choices remain", static_cast<unsigned>(active_words.size())); if (active_words.size() <= 5) { // If we're near the end, print a list fputs(": ", stdout); auto it = active_words.cbegin(); describeWord(stdout, *it); do { fputs(", ", stdout); describeWord(stdout, *it); } while (++it != active_words.cend()); putchar('\n'); } else { puts("."); } putchar('\n'); string cmd; for (;;) { printInteractiveSuggestions(active_words); fputs("Give your move in form \"<word> <result>\" like \"TAXI 2\"\n\n" "> ", stdout); fflush(stdout); if (!read_uppercase_word(stdin, &cmd)) break; // EOF Word tried; unsigned matched; if (parse_move(cmd, wordLength(), &tried, &matched)) { DividedWordList dl(wordLength()); dl.build(*this, active_words, tried); recursiveInteractive(dl[matched].choices()); break; } puts("Invalid move.\n"); } } }
bool CheckWord( const char *word ) { #if 1 return wordSet.count( std::string( word ) ) == 1; #else const int count = words.size(); for( int i = 0; i < count; ++i ) { const char *s = words[i].c_str(); if( strcmp( s, word ) == 0 ) { return true; } } return false; #endif }
virtual void onServerResponse(const Words& words) { if (words.size() == 1) { if (words[0] == "OK") printf("Logged in successfully\n"); else if (words[0] == "InvalidPassword") printf("Invalid password\n"); else printf("Invalid response to login query\n"); } else printf("Invalid response to login query\n"); }
void test_file(PersonRecog &pr, const char *file) { ifstream fi(file); string line; Words words; if (!fi) { printf("can not open file: %s\n", file); return; } while (getline(fi, line)) { pr.recog(line.c_str(), words); //printf("%s\n", line.c_str()); for (int i=0; i<words.size(); i+=1) printf("%s\n", words[i].c_str()); } fi.close(); }
Words GatherAngrams( const char * from ) { int wild = 0; int find_freq[26] = {0}; uint32_t find_have = 0; for( const char *s = from; *s; ++s ) { if( *s == '?' ) { wild += 1; } else { const int ord = *s - 'a'; find_freq[ord] += 1; find_have |= (1<<ord); } } uint32_t find_dont = ~find_have; Words matchList; const int count = words.size(); for( int i = 0; i < count; ++i ) { const char *s = words[i].c_str(); if( strlen( s ) < min_length ) continue; int freq[26] = {0}; uint32_t have = 0; for( ; *s; ++s ) { const int ord = *s - 'a'; freq[ord] += 1; have |= (1<<ord); } if( wild == 0 && find_dont & have ) continue; int over = 0; for( int c = 0; c < 26; ++c ) { if( freq[c] > find_freq[c] ) over += freq[c] - find_freq[c]; } if( over <= wild ) { logf( 1, RED "From (%s) -> (%s)\n", from, words[i].c_str() ); matchList.push_back( words[i] ); } } return matchList; }
SDataForSolver CSolver::ExtractData(const std::string & inputString) { SDataForSolver result; Words arguments = SplitWords(inputString); if (arguments.size() == AMOUNT_ARGUMENTS) { result.firstPoint.x = stof(arguments[0]); result.firstPoint.y = stof(arguments[1]); result.secondPoint.x = stof(arguments[2]); result.secondPoint.y = stof(arguments[3]); result.radiusCircle = stof(arguments[4]); } else { throw std::invalid_argument(MESSAGE_INCORRECT_AMOUNT_ARGUMENTS); } return result; }
void Dictionary::recursiveSolve(AbstractSolutionAcceptor * const acceptor, Words const& active_words, AnswerSequence * const runningAnswersp) const { assert(!active_words.empty()); if (active_words.size() == 1) { // We found an end node acceptor->acceptSolution(*runningAnswersp, active_words.front()); } else { DividedWordList dl(wordLength()); double best_entropy = -1.0; Word best_word = 0; // Of all of the words in active_words, find the one that // will gives us the most even split (and thus the highest entropy) { auto it = keys_.cbegin(); do { dl.build(*this, active_words, *it); double const entropy = dl.entropy(); if (entropy > best_entropy) { best_entropy = entropy; best_word = *it; } } while (++it != keys_.cend()); } assert(popcount32(best_word) == wordLength()); // Now that we have found our best word, recompute the split dl.build(*this, active_words, best_word); assert(dl.entropy() == best_entropy); acceptor->acceptBranch(*runningAnswersp, best_word, best_entropy, dl); // Now recursively descend for (unsigned i = 0; i < wordLength() + 1; i++) { const WordsWithTotalChoices& wwtc = dl[i]; if (wwtc.count() > 0) { runningAnswersp->push_back(i); recursiveSolve(acceptor, wwtc.choices(), runningAnswersp); runningAnswersp->pop_back(); } } } }
void readContributors(NameMap& names, const string& file) { osgDB::ifstream fin(file.c_str()); Words words; while(fin) { string keyword; fin >> keyword; words.push_back(keyword); } string blank_string; for(unsigned int i = 0; i < words.size(); ++i) { if (submissionsSequence(words, i)) { if (i + 2 < words.size() && validName(words[i + 1])) { NamePair name = createName(words[i + 1], words[i + 2]); nameCorrection(name); if (!name.first.empty()) ++names[name]; i += 2; } else if (i + 1 < words.size() && validName(words[i + 1])) { NamePair name = createName(words[i + 1], blank_string); nameCorrection(name); if (!name.first.empty()) ++names[name]; i += 1; } } else { if (words[i] == "robert") { ++names[NameRobertOsfield]; } else if (words[i] == "don") { ++names[NameDonBurns]; } } } // reassign fisrt name entries to their full names entries if (names.size() > 1) { for (NameMap::iterator itr = names.begin(); itr != names.end(); ) { if (itr->first.second.empty()) { NameMap::iterator next_itr = itr; ++next_itr; if (next_itr != names.end() && itr->first.first == next_itr->first.first) { next_itr->second += itr->second; names.erase(itr); itr = next_itr; } else { ++itr; } } else { ++itr; } } } // remove the double entries from Robert's contributions if (names.size() > 1) { for (NameMap::iterator itr = names.begin(); itr != names.end(); ++itr) { if (itr->first != NameRobertOsfield && itr->first != NameDonBurns) { names[NameRobertOsfield] -= itr->second; } } } }