12
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

Thumtack跪经

地里匿名用户
🔗
匿名用户-YH8B9  2016-11-17 00:40:38
zfrancica 发表于 2016-11-15 21:59
tf_idf coding这题到底是什么样子的。。。+1...

鉴于不少地里的朋友问这题到底啥样子,其实这题很简单,不是什么算法题,就是把一个数学公式简单的用coding来实现而已。
首先他会问你知不知道tf_idf,我说基本了解(其实烂熟于胸了),他说不知道也没关系,然后他会把wiki上的tf_idf解释给你听,装着仔细听就行了,注意他的定义可能会稍微不一样,不过他会把公式写出来的。可以参考:https://zh.wikipedia.org/zh-hans/TF-IDF
他当时给的定义是输入要查询的几个关键字,每个文档的tf_idf的定义是:
要查询的几个关键字在该文档各自的tf_idf之和。
每个关键字的tf_idf的定义基本和wiki上的一样,稍微有点不同的是分母不是该文档中所有词出现的次数之和,而是频率最高的词出现的次数,其他一样:
tf_idf=keyCount/maxCount * log(n/docCount),这里keyCount是要查询的词在该文档中出现的次数,maxCount是该文档中频率最高的词出现的次数,n是总文档数,docCount是要查询的词出现在几个文档中。
最后的要求就是输入要查询的几个关键字,输出最高tf_idf的几个文档的序号,自己随便输入一些文档的词(他要考察你给的例子好不好),然后和他一起计算结果对不对。反正onsite都写了代码,这里就顺便贴在下边:
  1. #include <unordered_set>
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <iostream>

  7. using namespace std;

  8. /*
  9. input: vector<string> keys, vector<vector<string>> docs
  10. keys: a few key words to query
  11. docs: a list of docs, each doc is a list of strings
  12. return: highest ranking docs for query keys
  13. tf_idf definition:
  14. tf_idf=sum of each key's tf_idf
  15. key's tf_idf=keyCount/maxCount * log(n/docCount)
  16. */

  17. vector<int> tf_idf(vector<string> & keys, vector<vector<string>> & docs) {
  18.     int n=docs.size(); // total number of the docs
  19.     vector<unordered_map<string, int>> keyCount(n); //each word's count in each doc
  20.     vector<int> maxCount(n); //the most freq word's count in each doc

  21.     for(int i=0; i<n; ++i) {
  22.         for(auto & word:docs[i]) {
  23.             ++keyCount[i][word];
  24.             maxCount[i]=max(maxCount[i],keyCount[i][word]);
  25.         }
  26.     }

  27.     unordered_map<string, int> docCount; //each word appears in how many docs
  28.     for (int i=0; i<n; ++i) {
  29.         for(string & key:keys) {
  30.             if(keyCount[i].count(key)) ++docCount[key];
  31.         }
  32.     }

  33.     vector<pair<double, int>> tfidf(n); //each doc's tf_idf, pair.first: tf_idf, pair.second: file's index, pair is used for sorting
  34.     for (int i=0; i<n; ++i) {
  35.         for(string & key:keys) {
  36.             tfidf[i].first+=(keyCount[i][key]+0.0)/maxCount[i]*log((n+0.0)/docCount[key]);
  37.         }
  38.         tfidf[i].second=i;
  39.     }
  40.     sort(tfidf.begin(),tfidf.end(),greater<pair<double,int>>());
  41.     int m=2; //output first 2 highest tf_idf doc's index
  42.     vector<int> ret(m);
  43.     for(int i=0; i<m; ++i) {
  44.         ret[i]=tfidf[i].second;
  45.     }
  46.     return ret;
  47. }

  48. int main() {
  49.     vector<string> keys={"dog","cat"};
  50.     vector<vector<string>> docs={ //testing case, given by myself
  51.         {"dog", "dog", "cat", "a", "big"}, // 2/2*log(4/2)+1/2*log(4/2)
  52.         {"cat", "dog", "a", "big"}, // 1/1*log(4/2)+1/1*log(4/2)
  53.         {"cow", "a", "the"},
  54.         {"the", "a"}
  55.     };
  56.     vector<int> ret;
  57.     ret=tf_idf(keys,docs);
  58.     for(auto & r:ret) cout << r << endl; //output doc's index
  59. }
复制代码

评分

参与人数 1大米 +3 收起 理由
CrescentGG + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表