{"action":"created","comment":{"url":"https://api.github.com/repos/seomoz/qdr/pulls/comments/22966822","id":22966822,"diff_hunk":"@@ -0,0 +1,417 @@\n+\n+#include <math.h>\n+#include <string>\n+#include <vector>\n+#include <unordered_map>\n+#include <stdexcept>\n+\n+\n+typedef std::unordered_map<std::string, std::pair<uint64_t, uint64_t> >\n+    counts_t;\n+\n+\n+// count of word occurances in a doc\n+typedef std::unordered_map<std::string, uint64_t> word_counts_t;\n+\n+// a tokenized document\n+typedef std::vector<std::string> doc_t;\n+\n+word_counts_t count_words(const doc_t& document)\n+{\n+    /// Given a vector of tokens, return a map token -> count in vector\n+    // heuristic to set final hash sparsity.  goal = 10%\n+    // number of keys in ret is numher unique tokens in document\n+    // in avg web page this is about 1/3 the number of total tokens\n+    // so this gives hash sparsity of about 1/9 or about 10%\n+    word_counts_t ret(document.size() * 3);\n+    for (doc_t::const_iterator it = document.begin();\n+        it != document.end(); ++it)\n+    {\n+        word_counts_t::iterator got = ret.find(*it);\n+        if (got == ret.end())\n+        {\n+            // first time we've seen this word\n+            ret[*it] = 1;\n+        }\n+        else\n+            ret[*it] += 1;\n+    }\n+    return ret;\n+}\n+\n+struct scores_t {\n+    double tfidf;\n+    double bm25;\n+    double lm_jm;\n+    double lm_dirichlet;\n+    double lm_ad;\n+};\n+\n+struct lm_scores_t {\n+    double jm;\n+    double dirichlet;\n+    double ad;\n+};\n+\n+class QDR\n+{\n+    public:\n+        QDR(counts_t& counts, uint64_t total_docs);\n+        ~QDR();\n+\n+        // compute the similarity scores\n+        scores_t score(doc_t& document, doc_t& query);\n+\n+        // compute similiary scores for a single doc but list of queries\n+        std::vector<scores_t> score_batch(doc_t& document,\n+            std::vector<doc_t>& queries);\n+\n+        // get the IDF for a given word\n+        double get_idf(const std::string& word);\n+\n+    private:\n+        counts_t counts;\n+        uint64_t total_docs;\n+        uint64_t nwords_vocab;\n+        uint64_t nwords;\n+\n+        // useful constant for language model\n+        double n2p1;\n+\n+        // compute the TF-IDF similarity\n+        double tfidf(const word_counts_t& doc_counts,\n+            const word_counts_t& query_counts);\n+\n+        // BM25\n+        double okapi_bm25(const word_counts_t& doc_counts,\n+            const word_counts_t& query_counts,\n+            const double& Ld);\n+\n+        // language model\n+        lm_scores_t lm(const word_counts_t& doc_counts,\n+            const word_counts_t& query_counts,\n+            const double& sum_count_w_given_doc);\n+\n+        // low level function used by public methods\n+        scores_t score_single(const word_counts_t& doc_counts,\n+            const word_counts_t& query_counts,\n+            const double& nwords_document);\n+\n+        // disable some default constructors\n+        QDR();\n+        QDR& operator= (const QDR& other);\n+        QDR(const QDR& other);\n+\n+};\n+\n+QDR::QDR(counts_t& counts_in, uint64_t total_docs) : total_docs(total_docs)\n+{\n+    // copy the counts data to private member and add up the total\n+    // number of words and words in vocab\n+    nwords_vocab = 0;\n+   nwords = 0;\n+\n+    for (counts_t::iterator it = counts_in.begin(); it != counts_in.end();\n+        ++it)\n+    {\n+        counts[it->first] = it->second;\n+        ++nwords_vocab;\n+        nwords += it->second.first;\n+    }\n+\n+    n2p1 = double(nwords) + double(nwords_vocab) + 1.0;\n+}\n+\n+QDR::~QDR() {}\n+\n+double QDR::get_idf(const std::string& word)\n+{\n+    /// Get log(total docs / doc count for word) == IDF\n+    double doc_freq;\n+    counts_t::iterator got = counts.find(word);\n+    if (got != counts.end())\n+    {\n+        // this word is in the corpus\n+        doc_freq = got->second.second;\n+    }\n+    else\n+        doc_freq = 1.0;\n+    return log((double) total_docs / doc_freq);\n+}\n+\n+scores_t QDR::score(doc_t& document, doc_t& query)\n+{\n+    if (document.empty() || query.empty())\n+        throw std::invalid_argument(\n+            \"Document and query both need to be non-empty\");\n+\n+    // compute counts, doc length, etc now since they are used\n+    // in multiple components\n+    word_counts_t query_counts = count_words(query);\n+    word_counts_t doc_counts = count_words(document);\n+\n+    double nwords_document = 0.0;\n+    for (word_counts_t::const_iterator it_doc = doc_counts.begin();\n+        it_doc != doc_counts.end(); ++it_doc)\n+        nwords_document += it_doc->second;\n+\n+    // now the scores\n+    return score_single(doc_counts, query_counts, nwords_document);\n+}\n+\n+std::vector<scores_t> QDR::score_batch(\n+    doc_t& document, std::vector<doc_t>& queries)\n+{\n+    if (document.empty())\n+        throw std::invalid_argument(\n+            \"Document and query both need to be non-empty\");\n+\n+    // compute document level counts once, then iterate through queries\n+    word_counts_t doc_counts = count_words(document);\n+\n+    double nwords_document = 0.0;\n+    for (word_counts_t::const_iterator it_doc = doc_counts.begin();\n+        it_doc != doc_counts.end(); ++it_doc)\n+        nwords_document += it_doc->second;\n+\n+    // iterate through queries, compute scores\n+    std::vector<scores_t> ret;\n+    ret.reserve(queries.size());\n+    for (std::vector<doc_t>::iterator it = queries.begin();\n+        it != queries.end(); ++it)\n+    {\n+        if (it->size() == 0)\n+            throw std::invalid_argument(\n+                \"Document and query both need to be non-empty\");\n+        word_counts_t query_counts = count_words(*it);\n+        ret.push_back(score_single(doc_counts, query_counts, nwords_document));\n+    }\n+\n+    return ret;\n+}\n+\n+scores_t QDR::score_single(const word_counts_t& doc_counts,\n+        const word_counts_t& query_counts,\n+        const double& nwords_document)\n+{\n+    scores_t scores;\n+    scores.tfidf = tfidf(doc_counts, query_counts);\n+    scores.bm25 = okapi_bm25(doc_counts, query_counts, nwords_document);\n+\n+    lm_scores_t lm_scores = lm(doc_counts, query_counts, nwords_document);\n+    scores.lm_jm = lm_scores.jm;\n+    scores.lm_dirichlet = lm_scores.dirichlet;\n+    scores.lm_ad = lm_scores.ad;\n+\n+    return scores;\n+}\n+\n+double QDR::tfidf(\n+    const word_counts_t& doc_counts, const word_counts_t& query_counts)\n+{\n+    /// Compute TF-IDF similarity between the document and query","path":"qdr/_ranker.cc","position":212,"original_position":212,"commit_id":"ea3047c0ca1a32aa1ef86c3bb48f0ccd44beacca","original_commit_id":"ea3047c0ca1a32aa1ef86c3bb48f0ccd44beacca","user":{"login":"b4hand","id":708767,"avatar_url":"https://avatars.githubusercontent.com/u/708767?v=3","gravatar_id":"","url":"https://api.github.com/users/b4hand","html_url":"https://github.com/b4hand","followers_url":"https://api.github.com/users/b4hand/followers","following_url":"https://api.github.com/users/b4hand/following{/other_user}","gists_url":"https://api.github.com/users/b4hand/gists{/gist_id}","starred_url":"https://api.github.com/users/b4hand/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/b4hand/subscriptions","organizations_url":"https://api.github.com/users/b4hand/orgs","repos_url":"https://api.github.com/users/b4hand/repos","events_url":"https://api.github.com/users/b4hand/events{/privacy}","received_events_url":"https://api.github.com/users/b4hand/received_events","type":"User","site_admin":false},"body":"If this is documentation, in C++ it typically precedes the function signature, and we've generally used the javadoc style comment delimiter of `/**`.","created_at":"2015-01-14T20:21:42Z","updated_at":"2015-01-14T20:21:42Z","html_url":"https://github.com/seomoz/qdr/pull/1#discussion_r22966822","pull_request_url":"https://api.github.com/repos/seomoz/qdr/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/comments/22966822"},"html":{"href":"https://github.com/seomoz/qdr/pull/1#discussion_r22966822"},"pull_request":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/seomoz/qdr/pulls/1","id":27238001,"html_url":"https://github.com/seomoz/qdr/pull/1","diff_url":"https://github.com/seomoz/qdr/pull/1.diff","patch_url":"https://github.com/seomoz/qdr/pull/1.patch","issue_url":"https://api.github.com/repos/seomoz/qdr/issues/1","number":1,"state":"open","locked":false,"title":"TF-IDF, BM25 and language model scoring","user":{"login":"matt-peters","id":619844,"avatar_url":"https://avatars.githubusercontent.com/u/619844?v=3","gravatar_id":"","url":"https://api.github.com/users/matt-peters","html_url":"https://github.com/matt-peters","followers_url":"https://api.github.com/users/matt-peters/followers","following_url":"https://api.github.com/users/matt-peters/following{/other_user}","gists_url":"https://api.github.com/users/matt-peters/gists{/gist_id}","starred_url":"https://api.github.com/users/matt-peters/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matt-peters/subscriptions","organizations_url":"https://api.github.com/users/matt-peters/orgs","repos_url":"https://api.github.com/users/matt-peters/repos","events_url":"https://api.github.com/users/matt-peters/events{/privacy}","received_events_url":"https://api.github.com/users/matt-peters/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-13T00:07:38Z","updated_at":"2015-01-14T20:21:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"09efdcab0a0775c8ee73fca261fac32acfa9e7a7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/seomoz/qdr/pulls/1/commits","review_comments_url":"https://api.github.com/repos/seomoz/qdr/pulls/1/comments","review_comment_url":"https://api.github.com/repos/seomoz/qdr/pulls/comments/{number}","comments_url":"https://api.github.com/repos/seomoz/qdr/issues/1/comments","statuses_url":"https://api.github.com/repos/seomoz/qdr/statuses/ea3047c0ca1a32aa1ef86c3bb48f0ccd44beacca","head":{"label":"seomoz:ranking","ref":"ranking","sha":"ea3047c0ca1a32aa1ef86c3bb48f0ccd44beacca","user":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"repo":{"id":28982768,"name":"qdr","full_name":"seomoz/qdr","owner":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/seomoz/qdr","description":"Query-Document Relevance","fork":false,"url":"https://api.github.com/repos/seomoz/qdr","forks_url":"https://api.github.com/repos/seomoz/qdr/forks","keys_url":"https://api.github.com/repos/seomoz/qdr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/seomoz/qdr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/seomoz/qdr/teams","hooks_url":"https://api.github.com/repos/seomoz/qdr/hooks","issue_events_url":"https://api.github.com/repos/seomoz/qdr/issues/events{/number}","events_url":"https://api.github.com/repos/seomoz/qdr/events","assignees_url":"https://api.github.com/repos/seomoz/qdr/assignees{/user}","branches_url":"https://api.github.com/repos/seomoz/qdr/branches{/branch}","tags_url":"https://api.github.com/repos/seomoz/qdr/tags","blobs_url":"https://api.github.com/repos/seomoz/qdr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/seomoz/qdr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/seomoz/qdr/git/refs{/sha}","trees_url":"https://api.github.com/repos/seomoz/qdr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/seomoz/qdr/statuses/{sha}","languages_url":"https://api.github.com/repos/seomoz/qdr/languages","stargazers_url":"https://api.github.com/repos/seomoz/qdr/stargazers","contributors_url":"https://api.github.com/repos/seomoz/qdr/contributors","subscribers_url":"https://api.github.com/repos/seomoz/qdr/subscribers","subscription_url":"https://api.github.com/repos/seomoz/qdr/subscription","commits_url":"https://api.github.com/repos/seomoz/qdr/commits{/sha}","git_commits_url":"https://api.github.com/repos/seomoz/qdr/git/commits{/sha}","comments_url":"https://api.github.com/repos/seomoz/qdr/comments{/number}","issue_comment_url":"https://api.github.com/repos/seomoz/qdr/issues/comments/{number}","contents_url":"https://api.github.com/repos/seomoz/qdr/contents/{+path}","compare_url":"https://api.github.com/repos/seomoz/qdr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/seomoz/qdr/merges","archive_url":"https://api.github.com/repos/seomoz/qdr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/seomoz/qdr/downloads","issues_url":"https://api.github.com/repos/seomoz/qdr/issues{/number}","pulls_url":"https://api.github.com/repos/seomoz/qdr/pulls{/number}","milestones_url":"https://api.github.com/repos/seomoz/qdr/milestones{/number}","notifications_url":"https://api.github.com/repos/seomoz/qdr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/seomoz/qdr/labels{/name}","releases_url":"https://api.github.com/repos/seomoz/qdr/releases{/id}","created_at":"2015-01-08T19:44:16Z","updated_at":"2015-01-08T19:44:16Z","pushed_at":"2015-01-14T19:54:29Z","git_url":"git://github.com/seomoz/qdr.git","ssh_url":"git@github.com:seomoz/qdr.git","clone_url":"https://github.com/seomoz/qdr.git","svn_url":"https://github.com/seomoz/qdr","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"seomoz:master","ref":"master","sha":"d0ebbdb3d7c669d2c91d738820b0b21a697fc5c4","user":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"repo":{"id":28982768,"name":"qdr","full_name":"seomoz/qdr","owner":{"login":"seomoz","id":104895,"avatar_url":"https://avatars.githubusercontent.com/u/104895?v=3","gravatar_id":"","url":"https://api.github.com/users/seomoz","html_url":"https://github.com/seomoz","followers_url":"https://api.github.com/users/seomoz/followers","following_url":"https://api.github.com/users/seomoz/following{/other_user}","gists_url":"https://api.github.com/users/seomoz/gists{/gist_id}","starred_url":"https://api.github.com/users/seomoz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seomoz/subscriptions","organizations_url":"https://api.github.com/users/seomoz/orgs","repos_url":"https://api.github.com/users/seomoz/repos","events_url":"https://api.github.com/users/seomoz/events{/privacy}","received_events_url":"https://api.github.com/users/seomoz/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/seomoz/qdr","description":"Query-Document Relevance","fork":false,"url":"https://api.github.com/repos/seomoz/qdr","forks_url":"https://api.github.com/repos/seomoz/qdr/forks","keys_url":"https://api.github.com/repos/seomoz/qdr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/seomoz/qdr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/seomoz/qdr/teams","hooks_url":"https://api.github.com/repos/seomoz/qdr/hooks","issue_events_url":"https://api.github.com/repos/seomoz/qdr/issues/events{/number}","events_url":"https://api.github.com/repos/seomoz/qdr/events","assignees_url":"https://api.github.com/repos/seomoz/qdr/assignees{/user}","branches_url":"https://api.github.com/repos/seomoz/qdr/branches{/branch}","tags_url":"https://api.github.com/repos/seomoz/qdr/tags","blobs_url":"https://api.github.com/repos/seomoz/qdr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/seomoz/qdr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/seomoz/qdr/git/refs{/sha}","trees_url":"https://api.github.com/repos/seomoz/qdr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/seomoz/qdr/statuses/{sha}","languages_url":"https://api.github.com/repos/seomoz/qdr/languages","stargazers_url":"https://api.github.com/repos/seomoz/qdr/stargazers","contributors_url":"https://api.github.com/repos/seomoz/qdr/contributors","subscribers_url":"https://api.github.com/repos/seomoz/qdr/subscribers","subscription_url":"https://api.github.com/repos/seomoz/qdr/subscription","commits_url":"https://api.github.com/repos/seomoz/qdr/commits{/sha}","git_commits_url":"https://api.github.com/repos/seomoz/qdr/git/commits{/sha}","comments_url":"https://api.github.com/repos/seomoz/qdr/comments{/number}","issue_comment_url":"https://api.github.com/repos/seomoz/qdr/issues/comments/{number}","contents_url":"https://api.github.com/repos/seomoz/qdr/contents/{+path}","compare_url":"https://api.github.com/repos/seomoz/qdr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/seomoz/qdr/merges","archive_url":"https://api.github.com/repos/seomoz/qdr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/seomoz/qdr/downloads","issues_url":"https://api.github.com/repos/seomoz/qdr/issues{/number}","pulls_url":"https://api.github.com/repos/seomoz/qdr/pulls{/number}","milestones_url":"https://api.github.com/repos/seomoz/qdr/milestones{/number}","notifications_url":"https://api.github.com/repos/seomoz/qdr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/seomoz/qdr/labels{/name}","releases_url":"https://api.github.com/repos/seomoz/qdr/releases{/id}","created_at":"2015-01-08T19:44:16Z","updated_at":"2015-01-08T19:44:16Z","pushed_at":"2015-01-14T19:54:29Z","git_url":"git://github.com/seomoz/qdr.git","ssh_url":"git@github.com:seomoz/qdr.git","clone_url":"https://github.com/seomoz/qdr.git","svn_url":"https://github.com/seomoz/qdr","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/1"},"html":{"href":"https://github.com/seomoz/qdr/pull/1"},"issue":{"href":"https://api.github.com/repos/seomoz/qdr/issues/1"},"comments":{"href":"https://api.github.com/repos/seomoz/qdr/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/seomoz/qdr/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/seomoz/qdr/statuses/ea3047c0ca1a32aa1ef86c3bb48f0ccd44beacca"}}}}