【问题标题】:inverted index node addword倒排索引节点加词
【发布时间】:2017-03-26 22:34:36
【问题描述】:

我是 C++ 新手。我正在尝试制作倒排索引,但我无法理解绳索。我想计算这个词的频率。你能解释一下这段代码以及如何计算单词的频率吗?请帮我解决这个问题。

class node{
public:
node() { 
    clear(); 
}
node(char z) {
    clear(); 
}
~node() {
    for (int x = 0; x < MAX_NODES; x++) {
        if (next[x]) {
            delete next[x];
        }
    }
}
void clear() {
    for (int x = 0; x < MAX_NODES; x++){
        next[x] = 0;
        isWord = false;
    }
}
bool isWord;
int count;//frq
std::vector<std::string> files;
node* next[MAX_NODES];
map<string, int> counts;
};
class index {
public:
void add(std::string s, std::string fileName) {
    std::transform(s.begin(), s.end(), s.begin(), tolower);
    std::string h;
    int freq=0;
    for (std::string::iterator i = s.begin(); i != s.end(); i++) {
        if (*i == 32) {
            pushFileName(addWord(h), fileName);
            h.clear();
            continue;
        }
        h.append(1, *i);
    }
    if (h.length()){
        pushFileName(addWord(h), fileName);
    }
}
void findWord(std::string s, map<string, int> counts) {
    std::vector<std::string> v = find(s);
    if (!v.size()) {
        std::cout <<"'"<< s + "' is not found!\n";
        return;
    }
    std::cout << "'" << s << "' is found in:\n";
    for (std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++) {
        std::cout << *i << "\n";

    }
    std::cout << "frequency is : ";

}
private:
void pushFileName(node* n, std::string fn) {
    std::vector<std::string>::iterator i = std::find(n->files.begin(), n->files.end(), fn);
    if (i == n->files.end()){
        n->files.push_back(fn);
        n->count;
    }
}

const std::vector<std::string>& find(std::string s) {
    size_t idx;
    std::transform(s.begin(), s.end(), s.begin(), tolower);
    node* rt = &root;
    for (std::string::iterator i = s.begin(); i != s.end(); i++) {
        idx = _CHARS.find(*i);
        if (idx < MAX_NODES) {
            if (!rt->next[idx]){
                return std::vector<std::string>();
            }
            rt = rt->next[idx];
        }
    }
    if (rt->isWord) return rt->files;
    return std::vector<std::string>();
}
node* addWord(std::string s) {
    size_t idx;
    node *rt = &root, *n;
    for (std::string::iterator i = s.begin(); i != s.end(); i++) {
        idx = _CHARS.find(*i);
        if (idx < MAX_NODES) {
            n = rt->next[idx];
            if (n){
                rt = n;
                continue;
            }
            n = new node(*i);
            rt->next[idx] = n;
            rt = n;
        }
    }
    rt->isWord = true;
    rt->count++;
    return rt;
}
node root;
};

class index {
public:
void add(std::string s, std::string fileName) {
    std::transform(s.begin(), s.end(), s.begin(), tolower);
    std::string h;
    int freq=0;
    for (std::string::iterator i = s.begin(); i != s.end(); i++) {
        if (*i == 32) {
            pushFileName(addWord(h), fileName);
            h.clear();
            continue;
        }
        h.append(1, *i);
    }
    if (h.length()){
        pushFileName(addWord(h), fileName);
    }
}
void findWord(std::string s, map<string, int> mFilesFreq) {
    std::vector<std::string> v = find(s);
    if (!v.size()) {
        std::cout <<"'"<< s + "' is not found!\n";
        return;
    }
    std::cout << "'" << s << "' is found in:\n";
    for (std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++) {
        std::cout << *i << "\n";

    }
    std::cout << "frequency is : ";

}

【问题讨论】:

    标签: c++ word-frequency inverted-index


    【解决方案1】:

    如果您计算给定单词调用add 的次数,您可能希望将rt-&gt;isWord = true; 替换为rt-&gt;count++;,并在struct node 中将bool isWord 替换为int count

    【讨论】:

    • 我会使用map&lt;string, int&gt; counts(而不是filescount),其中string是文件名,int是计数。要更新计数,请使用 rt-&gt;counts[filename]++;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2014-11-12
    相关资源
    最近更新 更多