本文整理汇总了C++中Lexicon::add方法的典型用法代码示例。如果您正苦于以下问题:C++ Lexicon::add方法的具体用法?C++ Lexicon::add怎么用?C++ Lexicon::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lexicon
的用法示例。
在下文中一共展示了Lexicon::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readBowFileText
//! @brief read a text Bow file and fills a lexicon
//! @param fileIn the file to read
//! @param reader the file reader
//! @param lex the lexicon to fill
//! @param propertyAccessor
//! @param referenceProperties
void readBowFileText(ifstream& fileIn,
BoWBinaryReader& reader,
Lexicon& lex,
const PropertyAccessor& propertyAccessor,
set<LinguisticCode>& referenceProperties
)
{
BoWText text;
reader.readBoWText(fileIn,text);
bool filterCategory = false;
if ( referenceProperties.size() > 0 ) {
filterCategory = true;
}
BoWTokenIterator it(text);
while (! it.isAtEnd()) {
const BoWToken& token = *(it.getElement());
if (filterCategory) {
set<LinguisticCode>::const_iterator referencePropertyIt =
referenceProperties.find(propertyAccessor.readValue(token.getCategory()));
if ( referencePropertyIt != referenceProperties.end() ) {
lex.add(getStringDecomp(&token),token.getIndexString());
}
}
else {
lex.add(getStringDecomp(&token),token.getIndexString());
}
it++;
}
}
示例2: main
int main() {
Lexicon english ("EnglishWords.dat");
Lexicon usedWords; // need to check the unique word
Queue<Vector<string>> result;
string firstWord, secondWord;
while(true){
readWords (firstWord, secondWord, english);
createFirstLadder(result, firstWord);
usedWords.add(firstWord);
while(!result.isEmpty ()){
Vector<string> firstLadder = result.dequeue ();
if(firstLadder.get (firstLadder.size () - 1) == secondWord){
display(firstLadder);
result.clear();
usedWords.clear ();
break;
}
string currentWord = firstLadder.get(firstLadder.size ()-1);
usedWords.add(currentWord);
createNextLadder(currentWord, firstLadder, usedWords, result, english);
}
/* If the ladder of the two entered words is impossible */
if(!usedWords.isEmpty ()){
cout << "Sorry, this ladder is impossible..." << endl;
}
}
return 0;
}
示例3: streamInsertionLexiconTest
void streamInsertionLexiconTest() {
Lexicon lex;
cout << "empty lexicon: " << lex << endl;
lex.add("alpher");
cout << "1-item lexicon: " << lex << endl;
lex.add("beter");
lex.add("gammer");
cout << "3-item lexicon: " << lex << endl;
cout << "looping over lexicon..." << endl;
for (string s : lex) {
cout << s << endl;
}
}
示例4: next
/* The function to solve the problem
* Parameters:
* start, dest => the two words to connect (MUST BE VALID WORDS)
* dict => Lexicon restores words
* Return: The ladder / an empty Vector (when there is no solution)
* No side effects
*/
Vector<string> find_ladder(const string & start, const string & dest, const Lexicon & dict)
{
Vector<LadderStep> steps; //keep answers
Lexicon appeared; //word that has appeared
Queue<int> queue; //the queue, saves the labels of steps
//init
steps.push_back(LadderStep(start,NO_MORE_STEP));
appeared.add(start);
queue.enqueue(0);
int last = NO_MORE_STEP; //the last step of the answer
//main loop
while(!queue.isEmpty()){
int cur = queue.dequeue();
if (steps[cur].str == dest) //arrives destination
{
last = cur; //mark position
break;
}
for (int i = 0; i < steps[cur].str.size(); ++i){
for (char c = 'a'; c <= 'z'; ++c){ //generate all possible steps
string next(steps[cur].str);
next[i] = c;
//A valid word that hasn't appeared yet?
if (dict.contains(next)&& !appeared.contains(next)){
//enqueue
appeared.add(next);
steps.push_back(LadderStep(next, cur));
queue.enqueue(steps.size() - 1);
}
}
}
}
Vector<string> tmp; //the answer, reversed
while(last != NO_MORE_STEP){ //has more steps?
tmp.push_back(steps[last].str);
last = steps[last].forward; //go forward
}
//reverse to get the answer
Vector<string> res;
for (int i = tmp.size() - 1; i>=0; --i){
res.push_back(tmp[i]);
}
return res;
}
示例5: oneHopAway
/**
* Function: oneHopAway
* This helper function loops through each letter of the alphabet
* at each position for a word passed through, checking whether
* the newly formed word is both English and hasn't been used
* previously. The latter check is maintained by a Lexicon
* passed through by reference.
* Parameters: dictionary cleared in main as constant reference, Lexicon of used words
* passed by reference since it can be altered, constant reference to the word
* looking to build off of
*/
static Lexicon oneHopAway(const Lexicon& dictionary, Lexicon& usedWords, const string& topWord) {
Lexicon oneHopAway;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
for(int i = 0; i < topWord.length(); i++) {
for(int j = 0; j < alphabet.length(); j++) {
string testWord = topWord;
testWord[i] = alphabet[j];
if(dictionary.contains(testWord) && !usedWords.contains(testWord)) {
oneHopAway.add(testWord);
usedWords.add(testWord);
}
}
}
return oneHopAway;
}
示例6: assertEqualsInt
TIMED_TEST(LexiconTests, hashCodeTest_Lexicon, TEST_TIMEOUT_DEFAULT) {
Lexicon lex;
lex.add("a");
lex.add("bc");
assertEqualsInt("hashcode of self lexicon", hashCode(lex), hashCode(lex));
Lexicon copy = lex;
assertEqualsInt("hashcode of copy lexicon", hashCode(lex), hashCode(copy));
Lexicon lex2; // empty
// shouldn't add two copies of same lexicon
HashSet<Lexicon> hashlex {lex, copy, lex2};
assertEqualsInt("hashset of lexicon size", 2, hashlex.size());
}
示例7: english
//return a string vector as the shortest ladder, if found
//return an empty vector, if not found
Vector<string> bfs(string &startingWord, string &endingWord) {
Lexicon english("EnglishWords.dat");
Lexicon wordUsed;
Queue<Vector<string>> queue;
Vector<string> ladder;
ladder.push_back(startingWord);
queue.enqueue(ladder);
while (!queue.isEmpty()) {
Vector<string> front = queue.dequeue();
if (front.get(front.size() - 1) == endingWord)
return front;
string current = front.get(front.size() - 1);
for (int i = 0; i < current.size(); i++)
for (char j = 'a'; j <= 'z'; j++)
if (current[i] != j){
string next = current;
next[i] = j;
if (!english.contains(next))
continue;
if (wordUsed.contains(next))
continue;
wordUsed.add(next);
Vector<string> nextLadder = front;
nextLadder.push_back(next);
queue.enqueue(nextLadder);
}
}
Vector<string> empty;
return empty;
}
示例8: InputGuesses
/*
* Function: InputGuesses
* -------------------------
* Allows the user to input guesses. Runs checks on user guess to see if it meets all boggle requirements.
*/
void InputGuesses(Grid<char> boggleBoard, Lexicon wordList, Lexicon &usedWords, Grid<bool> usedDice)
{
cout << endl << "Ok, take all the time you want and find all the words you can!" << endl;
cout << "Signal that you're finished by entering an empty line." << endl << endl;
while (true) {
int row= 0, col = 0;
string userGuess = CheckUserGuess(wordList, usedWords); //checks if format rules are met
if (userGuess == "") break; //checks if sentinel of user hitting return has been occured
while (true) {
if (FindUserGuessOnBoard(boggleBoard, row, col, "", userGuess, usedDice)) {
//checks if user guess can be created from boggleBoard
usedWords.add(userGuess); //records user guess in lexicon of used words
RecordWordForPlayer(userGuess, Human);
UnhighlightBoard(boggleBoard);
PlayNamedSound("excellent.wav");
break;
}
if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) {
//checks if end of board has been reached without finding appropriate dice config
cout << "You can't make that word! " << RandomizeResponse() << endl;
PlayNamedSound("whoops.wav");
break;
}
}
}
}
示例9: findWordLadder
string findWordLadder(string startWord, string endWord, Lexicon& wordsLexicon)
{
Queue<Vector<string>> ladderQueue;
Lexicon usedWords;
Vector<string> startingLadder;
startingLadder.add(startWord);
ladderQueue.enqueue(startingLadder);
usedWords.add(startWord);
while(ladderQueue.size() > 0)
{
Vector<string> wordLadder = ladderQueue.dequeue();
string lastWord = wordLadder[wordLadder.size() - 1];
if(lastWord == endWord)
{
string returnValue = "Ladder found:";
foreach(string word in wordLadder)
{
returnValue += " " + word;
}
return returnValue;
}
示例10: humanPlays
Lexicon humanPlays(Grid<char> & board, Lexicon & english, Lexicon & usedWords){
cout<<"\n\n\n\nAlright, you can go first. Type any words that you find and press Enter so I can check them. "<<
"Pressing Enter on its own will end your turn. Good luck!\n\n\n"<<endl;
//loop forever to accept words
while(true){
Grid<bool> usedLetters(board.numRows(),board.numCols()); //to flag
string word = toUpperCase(getLine());
//quit statement
if (word == "")break;
//check word validity
if (isValid(word, board, usedLetters, usedWords, english)){
//add to usedWords list and also to the scoring system
usedWords.add(word);
recordWordForPlayer(word, HUMAN);
//highlight the letters
for (int row = 0; row < usedLetters.numRows(); row++) {
for (int col = 0; col < usedLetters.numCols(); col++) {
highlightCube(row, col, usedLetters[row][col]);
}
}
pause(1000); //leave them highlighted long enough to see
//unhighlight the letters
for (int row = 0; row < usedLetters.numRows(); row++) {
for (int col = 0; col < usedLetters.numCols(); col++) {
highlightCube(row, col, false);
}
}
}
}
return usedWords;
}
示例11: compareTestHelper
TIMED_TEST(LexiconTests, compareTest_Lexicon, TEST_TIMEOUT_DEFAULT) {
Lexicon lex;
lex.add("a");
lex.add("ab");
lex.add("bc");
Lexicon lex2;
lex2.add("a");
lex2.add("b");
lex2.add("c");
Lexicon lex3;
compareTestHelper(lex, lex2, "Lexicon", /* compareTo */ -1);
compareTestHelper(lex2, lex, "Lexicon", /* compareTo */ 1);
compareTestHelper(lex, lex, "Lexicon", /* compareTo */ 0);
Set<Lexicon> slex {lex, lex2, lex3};
assertEqualsString("slex", "{{}, {\"a\", \"ab\", \"bc\"}, {\"a\", \"b\", \"c\"}}", slex.toString());
}
示例12: checkLexicon
void checkLexicon(string indexLetters, Lexicon & lex) {
string lowerCase = UpToLow(indexLetters);
foreach (string word in lex) {
Lexicon newLex;
newLex.add(word);
if(newLex.containsPrefix(indexLetters) || newLex.containsPrefix(lowerCase)) {
cout << word << endl;
}
}
示例13: assertEquals
TIMED_TEST(LexiconTests, basicTest_Lexicon, TEST_TIMEOUT_DEFAULT) {
std::initializer_list<std::string> words = {
"a",
"ab",
"aab",
"aaab",
"aardvark",
"b",
"banana"
};
std::initializer_list<std::string> badWords = {
"abb",
"ad",
"and",
"aaardvark",
"aardvarks",
};
std::initializer_list<std::string> badPrefixes = {
"aaaa",
"abb",
"aardvarz",
"bb",
"bananas",
"c",
"r",
"z"
};
Lexicon lex;
for (std::string word : words) {
lex.add(word);
}
assertEquals("Lexicon size", words.size(), lex.size());
for (std::string word : words) {
assertTrue("Lexicon contains " + word, lex.contains(word));
}
for (std::string word : badWords) {
assertFalse("Lexicon contains " + word, lex.contains(word));
}
for (std::string word : words) {
for (int i = 0; i < (int) word.length(); i++) {
std::string prefix = word.substr(0, i);
assertTrue("Lexicon containsPrefix " + word, lex.containsPrefix(word));
}
}
for (std::string word : badPrefixes) {
assertFalse("Lexicon containsPrefix " + word, lex.containsPrefix(word));
}
}
示例14: loadLexiconFromFile
void loadLexiconFromFile(Lexicon& lexicon, string filename)
{
ifstream infile;
infile.open(filename.c_str());
string line;
while(getline(infile, line))
{
lexicon.add(line);
}
infile.close();
}
示例15: main
int main() {
Queue<Vector<string> > queue;
string start, end;
Vector<string> currentLadder;
Lexicon lex("EnglishWords.dat");
Lexicon used;
cout << "Enter starting word: ";
start = getLine();
cout << "Enter ending word: ";
end = getLine();
used.add(start);
currentLadder.add(start);
queue.enqueue(currentLadder);
while (!queue.isEmpty()) {
currentLadder = queue.dequeue();
string lastWordInLadder = currentLadder[currentLadder.size() - 1];
if (lastWordInLadder == end) {
printLadder(currentLadder);
return 0;
}
Vector<string> oneHopList;
findOneHopList(lastWordInLadder, oneHopList, lex);
foreach(string word in oneHopList) {
if (!isInLexicon(word, used)) {
used.add(word);
Vector<string> newLadder = currentLadder;
newLadder.add(word);
queue.enqueue(newLadder);
}
}
}
cout << "Your word \"" << start << "\" has no match in the lexicon." << endl;
return 0;
}