如何为拼写检查器控制台程序实现二进制搜索树?

问题描述 投票:-1回答:1

我在将拼写检查器代码实现二进制搜索树时遇到了麻烦。我希望它能够允许人们复制和粘贴文本,并让控制台程序打印出拼写检查的文本。这真的是即将到期,我很恐慌...我希望能够将其作为指令。下面是到目前为止的代码。

 // Spellchecker

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstring>
#include "BST.h"

using namespace std;

struct Record
{
    string name;
    unsigned int sid;
    unsigned int score;
    bool operator<(const Record& rec) const {
        return sid < rec.sid;
    }
    bool operator==(const Record& rec) const {
        return sid == rec.sid;
    }
    Record(string tn = "", unsigned int tid = 0, unsigned int ts = 0) : name(tn), sid(tid), score(ts)
    {}
};

int main()
{
    bstree<Record> tree;
    tree.insert(Record("test", 1234, 100));
    tree.erase(Record("test", 1234, 100));
    return 0;
}

string read_file(string& filename)
{
    ifstream file(filename);
    // If there is a file
    if (file)
    {
        string data;
        // Find total number of bytes of file
        file.seekg(0, file.end);
        data.resize(file.tellg());
        file.seekg(0, file.beg);
        // Read text into data string
        file.read(&data[0], data.size());
        // Close the file
        file.close();
        // Return the string
        return data;
    }
    else return string("");
}

void split_workds(vector<string>& words, string& data)
{
    char const* delim = " .,?!:;/\"\'\n\t";
    // Set token to first word
    char* token = strtok(&data[0], delim);
    // Split rest of words
    while (token != nullptr)
    {
        // Convert each word from dictionary to lower case
        for (int i = 0; i < strlen(token); ++i)
        {
            char word = tolower(token[i]);
            token[i] = word;
        }
        // Push word to end of vector
        words.push_back(token);
        // Get the next word
        token = strtok(nullptr, delim);
    }
    // Free the memory
    token = nullptr;
    delim = nullptr;
    delete token;
    delete delim;
}

int main(int argc, char **argv)
{
    string file_to_check, file_data, word_dictionary = "dictionary.txt";
    int spell_count = 0;

    BinarySearchTree *tree = new BinarySearchTree();
    vector<string> words;

    // Loop through argument
    for (int i = 0; i < argc; ++i)
    {
        // Set file name if provided as argument
        if (string(argv[i]) == "-i" && argv[i + 1] != nullptr)
            file_to_check = argv[i + 1];
    }

    // If there was no file name as argument, prompt user
    if (file_to_check.empty())
    {
        cout << "File name: ";
        getline(cin, file_to_check);
        cout << end1;
    }

    // If file name is not empty, run spell checking methods
    if (!file_to_check.empty())
    {
        // Read words from dictionary.txt into file_data string
        file_data = read_file(word_dictionary);
        // Split the words and store into vector
        split_words(words, file_data);
        // Insert words into Binary Search Tree
        for (int i = 0; i < words.size(); ++i)
            stringstream(words[i]) >> *tree;

        // Store the data read from specified file
        file_data = read_file(file_to_check);
        // Split sentences and store each word in words vector
        split_words(words, file_data);
        cout << end1;

        // Loop through words vector and check if it exists in dictionary
        for (int i = 0; i < words.size(); ++i)
        {
            // Print out non-occurring words
            if (!tree->exists(words[i]))
            {
                spell_count++;
                cout << words[i] << end1;
            }
        }
        cout << end1;

        // Print the total number of spelling mistakes
        cout << spell_count << " spelling mistakes" << end1;
    }
    else
    {
        // If still no file specified, print message and exit
        cout << "No file specified!" << end1;
        return 0;
    }

    // Free the memory
        delete tree;

        return 0;
    }
}

// COMMA: separation of words and word groups in a simple series of three or more items

using namespace std;

int main()
{
    std::string text;
    struct Dictionary {
        Dictionary() {
            std::ifstream input("/usr/share/dict/words");
            for (std::string text; getline(input, text);) {
                _words.insert(text);
            }
        }
        bool contains(std::string const& word) const {
            return _words.count(word);
        }
        std::set<std::string> _words;
    }
    cout << "Copy & Paste or Enter Text Here: ";
    getline(cin, text);
    std::string newtext = string(text) + ",";
    cout << newtext;


    return 0;
}
c++ binary-search-tree
1个回答
0
投票

[使用拼写检查或自动完成之类的单词和任务时,要有效完成任务所需的最佳结构是Trie,您可以在https://en.wikipedia.org/wiki/Trie上了解更多信息。

另外,这是它的几个c ++实现:

https://gist.github.com/hrsvrdhn/1ae71c25ef1c620c022a544d52df8928

https://www.techiedelight.com/cpp-implementation-trie-data-structure/

© www.soinside.com 2019 - 2024. All rights reserved.