简体   繁体   English

如何在C ++中从地图创建倒排索引到地图?

[英]How to create an inverted index from a map to map in C++?

I'm trying to create an inverted index in a map from a map .At moment I have this code: 我正在尝试从地图中的地图中创建一个反向索引。此刻,我有以下代码:

int main()
{

    char lineBuffer[200];
    typedef std::map<std::string, int> MapType;
    std::ifstream archiveInputStream("./hola");

    // map words to their text-frequency
    std::map<std::string, int> wordcounts;

    // read the whole archive...
    while (!archiveInputStream.eof())
    {
        //... line by line
        archiveInputStream.getline(lineBuffer, sizeof(lineBuffer));

        char* currentToken = strtok(lineBuffer, " ");

        // if there's a token...
        while (currentToken != NULL)
        {
            // ... check if there's already an element in wordcounts to be updated ...
            MapType::iterator iter = wordcounts.find(currentToken);
            if (iter != wordcounts.end())
            {
                // ... then update wordcount
                ++wordcounts[currentToken];
            }
            else
            {
                // ... or begin with a new wordcount
                wordcounts.insert(
                        std::pair<std::string, int>(currentToken, 1));
            }
            currentToken = strtok(NULL, " "); // continue with next token
        }

        // display the content
        for (MapType::const_iterator it = wordcounts.begin(); it != wordcounts.end();
                ++it)
        {
            std::cout << "Who(key = first): " << it->first;
            std::cout << " Score(value = second): " << it->second << '\n';
        }
    }
}

About this trouble I haven't idea, because I'm beginner using map structure. 关于这个麻烦,我不知道,因为我是初学者使用地图结构。

I'm very grateful to you your help. 我非常感谢您的帮助。

I think what might help would be to create a second map, indexing lists of string with same wordcount-index by this index, like this (similar to a histogram ): 我认为可能会帮助创建第二张地图,通过该索引为具有相同wordcount-index的string列表建立索引,如下所示(类似于histogram ):

std::map<int, std::list<std::string> > inverted;

so when you're done with creating the wordcounts -map you have to insert every string into the inverted index manually like this (be careful, this code is untested!): 因此,当您完成创建wordcounts -map时,您必须像这样手动将每个string插入反向索引中(注意,此代码未经测试!):

// wordcounts to inverted index
for (std::map<std::string, int>::iterator it = wordcounts.begin();
        it != wordcounts.end(); ++it)
{
    int wordcountOfString = it->second;
    std::string currentString = it->first;

    std::map<int, std::list<std::string> >::iterator invertedIt =
            inverted.find(wordcountOfString);
    if (invertedIt == inverted.end())
    {
        // insert new list
        std::list<std::string> newList;
        newList.push_back(currentString);
        inverted.insert(
                std::make_pair<int, std::list<std::string>>(
                        wordcountOfString, newList));
    }
    else
    {
        // update existing list
        std::list<std::string>& existingList = invertedIt->second;
        existingList.push_back(currentString);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM