예제 #1
0
bool Dictionary::SpellChecking(const string& filename)
{
    // Purpose: Check the file contents of filename to see if the words in it
    //          are in the dictionary or not already, thus "spell-checking" the file.

    fstream fileToCheck(filename.c_str(), ios::in); // Try to get the requested file
    if (!fileToCheck) // If we can't access it for whatever reason..
    {
        cerr << "Error: file \"" << filename << "\" does not exist, and thus spell-checking could not be performed." << endl;
        return failure; // .. break out of the function
    }

    string currentWord;
    while (fileToCheck >> currentWord) // Get a word
    {
        SEARCH_RESULT wordSearchResult = SearchForWord(currentWord);
        if (wordSearchResult == WORD_IS_NOT_VALID) // Invalid word, don't even consider it.
        {
            continue;
        }
        else if (wordSearchResult == FILE_DOES_NOT_EXIST || // If the file isn't there, it's not in the dictionary, or ..
                 wordSearchResult == WORD_NOT_FOUND)        // If the word was not found, it's also not in the dictionary
        {
            cout << "Word \"" << currentWord << "\" is not spelled correctly." << endl;
        }
        else // Word correct, this isn't necessary but I'll keep it here for now.
        {
            continue;
        }
    }
    return success;
} // END SpellChecking
예제 #2
0
void Dictionary::ProcessTransactionFile(const string& filename)
{
    // Purpose: To process a transaction file and perform what it specifies.

    ifstream transactFile(filename.c_str());
    if (!transactFile)
    {
        cerr << "Error: Transaction file \"" << filename << "\" does not exist." << endl;
        return;
    }

    string command, data;

    while (transactFile >> command >> data)
    {
        if (command == "AddW")
        {
            this->AddAWord(data);
        }
        else if (command == "DelW")
        {
            this->DeleteAWord(data);
        }
        else if (command == "SearchW")
        {
            SEARCH_RESULT wordSearchResult = SearchForWord(data);
            if (wordSearchResult == WORD_NOT_FOUND ||
                    wordSearchResult == FILE_DOES_NOT_EXIST)
            {
                cout << "Word \"" << data << "\" was not found." << endl;
            }
            else if (wordSearchResult == WORD_FOUND)
            {
                cout << "Word \"" << data << "\" was found." << endl;
            }
            else
            {
                cout << "Word \"" << data << "\" is not valid." << endl;
            }
        }
        else if (command == "InsertF")
        {
            this->InsertWordsIntoDict(data);
        }
        else if (command == "PrintF")
        {
            this->PrintAFileInDict(data);
        }
        else if (command == "SpellCheck")
        {
            this->SpellChecking(data);
        }
    }

    transactFile.close();
}
예제 #3
0
bool Dictionary::DeleteAWord(const string& word)
{
    // Purpose: Delete a word from the dictionary, including all of its duplicates, if it has any.
    //          The various things it does before ultimately performing that task
    //          however, aren't as simple as this function's name would suggest.

    switch (SearchForWord(word)) // Search for word in the dictionary.
    {
    case FILE_DOES_NOT_EXIST: // If the file doesn't exist
    case WORD_NOT_FOUND:      // or the word wasn't found
    case WORD_IS_NOT_VALID:   // Or the word wasn't valid:
        return failure;         // Failure

    case WORD_FOUND:          // Otherwise the word was found.
    default:                  // This should never be tripped unless I make a wrong return (hehe) in SearchForWord
        break;
    }

    const string wordToDel = toLowerCase(word);                      // Properly lowercased word
    const string filename = getFilenameByWord(wordToDel);            // Filename to use
    const unsigned int corrIndex = getCorrespondingIndex(wordToDel); // corresponding index for this word
    unsigned int nTimesWordAppeared = 0;                             // The number of times the certain word appeared
    fstream fileToModify(filename.c_str(), ios::in);                 // Fstream used to read in the words then write the ones that weren't matched

    string currentWord;                                              // Stores the current word
    vector<string> WordsToRetain;                                    // Stores all words that didn't match wordToDel

    while ( (fileToModify >> currentWord) )// && (words.size() < maxWordsPerFile) )
    {
        currentWord = toLowerCase(currentWord); // Convert to lowercase
        if (currentWord == wordToDel)           // If it matches, we don't need it
        {
            nTimesWordAppeared++;
            continue;                             // Skip to the next word (although there may not be one)
        }
        else                                    // Otherwise, add the word to the vector, to keep
        {
            WordsToRetain.push_back(currentWord);
        }
    }

    fileToModify.close();
    fileToModify.open(filename.c_str(), ios::out | ios::trunc); // Open and truncate

    for (unsigned int i = 0; i < WordsToRetain.size(); i++) // Iterate through the words vector
    {
        fileToModify << WordsToRetain[i] << " "; // Write the current word and a space to the file
    }

    fileToModify.close();
    numOfWordsInFile[corrIndex] -= nTimesWordAppeared; // Decrement by the number of words that weren't read into the vector.
    totalWordsInDict -= nTimesWordAppeared;            // Ditto

    return success;
} // END DeleteAWord
예제 #4
0
bool Dictionary::AddAWord(string word){
  ofstream fout;
  string file = fileName(word);
  word = to_lower(word);

  //check if file or dictionary is full
  if(isFull(file))
    return(failure);

  fout.open(file.c_str(), fstream::app);

  if(!SearchForWord(word)){
    //output word to file
    fout << word << " ";
    //increment word counts
    numOfWordsInFile[toupper(file[0]-65)]++;
    totalWordsInDict++;
    return(success);
  }
  fout.close();
  return(failure);
}
예제 #5
0
bool Dictionary::AddAWord(const string& word)
{
    // Purpose: to add a word to the corresponding file, making sure
    // that the word is valid and the word isn't already in the dictionary.

    switch (SearchForWord(word)) // IF the word is already there, f**k that shit.
    {
    case FILE_DOES_NOT_EXIST:
    case WORD_NOT_FOUND:
        break; // Continue to the rest of the func. if the word isn't there.

    case WORD_IS_NOT_VALID: // If word is invalid or
    case WORD_FOUND:        // the word is already there
    default:                // or something else gets returned.. somehow.
        return failure;       // exit, failure.
    }

    const string wordToAdd = toLowerCase(word); // Convert the word to lower-case.

    unsigned int corrIndex = getCorrespondingIndex(wordToAdd); // Get correspond. index.

    if ( numOfWordsInFile[corrIndex] >= maxWordsPerFile ||
            totalWordsInDict >= maxWordsInDict) // If we're at the limit for words in this file, or total words in dictionary
    {
        cerr << "Error: maximum number of words in file \"" << getFilenameByWord(wordToAdd) << ".txt\" reached, or " <<
             "maximum number of words in dictionary reached. No more words can be added." << endl;
        return failure;
    }
    string outFilename = getFilenameByWord(wordToAdd);
    fstream outFstream( outFilename.c_str(), ios::out | ios::app ); // Open the corresponding file

    outFstream << wordToAdd << " "; // Add the word
    outFstream.close(); // Done with the file

    totalWordsInDict++; // We've added a word so increment totalWordsInDict
    numOfWordsInFile[corrIndex]++; // And the number of words in the file.

    return success;
} // END AddAWord