gpt4 book ai didi

c++ - 如何在 C++ 中从映射创建倒排索引到映射?

转载 作者:行者123 更新时间:2023-11-28 08:02:35 25 4
gpt4 key购买 nike

我正在尝试从 map 在 map 中创建倒排索引。目前我有这段代码:

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';
}
}
}

关于这个问题我不知道,因为我是初学者使用 map 结构。

非常感谢您的帮助。

最佳答案

我认为创建第二张 map 可能会有所帮助,为 string 的列表编制索引该索引具有相同的 wordcount-index,像这样(类似于 histogram ):

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

所以当您完成创建 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);
}

}

关于c++ - 如何在 C++ 中从映射创建倒排索引到映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11043256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com