[LintCode][System Design] Inverted Index

Problem

Create an inverted index with given documents.

Example
Given a list of documents with id and content. (class
Document)

[
  {
    "id": 1,
    "content": "This is the content of document 1, it's very short"
  },
  {
    "id": 2,
    "content": "This is the content of document 2, it's very long. bilabial bilabial heheh hahaha ..."
  },
]

Return an inverted index (HashMap with key is the word and value is a list of document ids).

{
   "This": [1, 2],
   "is": [1, 2],
   ...
}

Solution

/**
 * Definition of Document:
 * class Document {
 * public:
 *     int id;
 *     string content;
 * }
 */
class Solution {
public:
    /**
     * @param docs a list of documents
     * @return an inverted index
     */
    map<string, vector<int>> invertedIndex(vector<Document>& docs) {
        map<string, vector<int>> ret;
        for(int i = 0; i < docs.size(); i++) {
            set<string> words = parseWords(docs[i].content);
            for(set<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
                ret[*iter].push_back(docs[i].id);
            }
        }
        
        return ret;
    }
    
    set<string> parseWords(string &s) {
        s = s + " ";
        set<string> ret;
        int start = -1;
        for(int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                if (start != -1) {
                    string word = s.substr(start, i - start);
                    ret.insert(word);
                    start = -1;
                }
            } else {
                if (start == -1) {
                    start = i;
                }
            }
        }
        
        return ret;
    }
};
    原文作者:楷书
    原文地址: https://www.jianshu.com/p/da059475b548
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞