首先,是关键词的选取:
好吧这个我这模型实在是太简单了,但还是讲一讲比较好呢。。。
我们现在手头有的是一堆百度百科词条w的DF(w, c)值,c是整个百科词条。。。原因是。。。方便嘛~(而且人家现成的只有介个了啦~)
我们发现有830W+的词条数目,都存下来显然是不理智、不科学、不魔法的。所以选取一部分作为关键词。
如何选取关键词呢?我选择了DF值在[100, 5000]之间的词。虽然也很不理智、不科学、不魔法,但是比直接存下来理智、科学、魔法多了,恩!
于是就全读进来,然后找到需要的词语,顺便计算下IDF值什么的输出到新的文件里去。
 
 #include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm> using namespace std;
typedef double lf;
const int cnt_id = ;
const lf tot_file = ;
const lf eps = 1e-; struct data {
int id;
lf IDF;
string st; data() {}
data(int _id, lf _IDF, string _st) : id(_id), IDF(_IDF), st(_st) {} inline bool operator < (const data &a) const {
return IDF > a.IDF;
}
} a[cnt_id];
inline bool cmp_id(data a, data b) {
return a.id < b.id;
} string st;
int id, cnt;
int St, Ed;
lf NUM_max, NUM_min; inline lf calc(int x) {
return (lf) log((lf) tot_file / (x + eps));
} int main() {
int i, DF;
freopen("data", "r", stdin);
freopen("data_new", "w", stdout);
ios::sync_with_stdio(true);
while (cin >> id >> st >> DF)
a[++cnt] = data(id, (lf) calc(DF), st);
sort(a + , a + cnt + ); NUM_max = calc(), NUM_min = calc();
for (i = ; i <= cnt; ++i)
if (a[i].IDF < NUM_max) break;
St = i;
for ( ; i <= cnt; ++i)
if (a[i].IDF < NUM_min) break;
Ed = i; sort(a + St, a + Ed, cmp_id);
cout << Ed - St << endl;
for (i = St; i < Ed; ++i)
cout << a[i].id << ' ' << a[i].st << ' ' << setprecision() << a[i].IDF << endl;
return ;
}

这样子我们就选出来了339,896个数作为关键词,占全部词条的4.1%,数量的减少,可以大幅之后的程序提高效率。

(p.s. 这里使用了一个小技巧,就是setprecision(x),表示在cout里,小数输出多少位关键字)

好了,关键词选选取完毕,接下来就是读入文章(已分词),并且计算出TF-IDF值啦!

我们可以边读边做,顺便达到节省空间且提高效率的目的。(data和passage两个map可以只剩下一个)

具体实现甚是蛋疼,各种搞不定。最后搞定了也不知道是怎么搞定的。。。反正现在是没什么问题,以后有没有就布吉岛了

 #include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <map> using namespace std;
typedef double lf;
const int mod1 = ;
const int mod2 = ;
const int bin = << ; struct TF_IDF {
int TF;
lf IDF, TF_IDF;
}; struct Word {
string st;
int h1, h2; inline bool operator < (const Word &x) const {
return h1 == x.h1 ? h2 < x.h2 : h1 < x.h1;
}
inline bool operator == (const Word &x) const {
return h1 == x.h1 && h2 == x.h2;
} #define x (int) st[i]
#define Weight 3001
inline void calc_hash() {
int len = st.length(), tmp, i;
for (i = tmp = ; i < len; ++i)
((tmp *= Weight) += (x < ? x + bin : x)) %= mod1;
h1 = tmp;
for (i = tmp = ; i < len; ++i)
((tmp *= Weight) += (x < ? x + bin : x)) %= mod2;
h2 = tmp;
}
#undef x
#undef Weight
} w; typedef map <Word, TF_IDF> map_for_words;
typedef map_for_words :: iterator iter_for_words; map_for_words passage; void read_in_passage() {
Word w;
freopen("E:\\test\\test.in", "r", stdin);
while (cin >> w.st) {
w.calc_hash();
passage[w].TF += ;
}
fclose(stdin);
} void read_in_IDF_and_work() {
int id, tot = , i;
lf IDF;
string st;
Word w;
iter_for_words it;
freopen("E:\\test\\new.dat", "r", stdin);
ios::sync_with_stdio(false);
cin >> tot;
for (i = ; i <= tot; ++i) {
cin >> id >> w.st >> IDF;
w.calc_hash();
it = passage.find(w);
if (it != passage.end()) {
it -> second.IDF = IDF;
it -> second.TF_IDF = (lf) it -> second.TF * it -> second.IDF;
}
}
fclose(stdin);
} void print() {
iter_for_words it;
cout << passage.size() << endl;
for (it = passage.begin(); it != passage.end(); ++it)
cout << it -> first.st << ' ' << it -> second.TF << ' ' << it -> second.IDF << ' ' << it -> second.TF_IDF << endl;
} int main() {
freopen("E:\\test\\test.out", "w", stdout);
read_in_passage();
read_in_IDF_and_work();
print();
return ;
}

特别被坑死的点:

第一次打开test.in不能加上"ios::sync_with_stdio(false);",但是第二次必须加上"ios::sync_with_stdio(false);"

否则第二次是可以打开文件的,但是什么都读不到= =

谁能告诉我这是什么坑货?、、、跪求巨神解答。。。

c++实现之 -- 文章TF-IDF值的计算的更多相关文章

  1. 使用solr的函数查询,并获取tf*idf值

    1. 使用函数df(field,keyword) 和idf(field,keyword). http://118.85.207.11:11100/solr/mobile/select?q={!func ...

  2. 文本分类学习(三) 特征权重(TF/IDF)和特征提取

    上一篇中,主要说的就是词袋模型.回顾一下,在进行文本分类之前,我们需要把待分类文本先用词袋模型进行文本表示.首先是将训练集中的所有单词经过去停用词之后组合成一个词袋,或者叫做字典,实际上一个维度很大的 ...

  3. 信息检索中的TF/IDF概念与算法的解释

    https://blog.csdn.net/class_brick/article/details/79135909 概念 TF-IDF(term frequency–inverse document ...

  4. tf idf公式及sklearn中TfidfVectorizer

    在文本挖掘预处理之向量化与Hash Trick中我们讲到在文本挖掘的预处理中,向量化之后一般都伴随着TF-IDF的处理,那么什么是TF-IDF,为什么一般我们要加这一步预处理呢?这里就对TF-IDF的 ...

  5. TF/IDF(term frequency/inverse document frequency)

    TF/IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明. 一. TF/IDF描述单个term与特定document的相 ...

  6. TF/IDF计算方法

    FROM:http://blog.csdn.net/pennyliang/article/details/1231028 我们已经谈过了如何自动下载网页.如何建立索引.如何衡量网页的质量(Page R ...

  7. tf–idf算法解释及其python代码实现(下)

    tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...

  8. tf–idf算法解释及其python代码实现(上)

    tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...

  9. Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据

    相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...

随机推荐

  1. Android——android必看 各个控件属性(网上看到的文字,觉得挺好的,珍藏了)

    属性 值 说明 Android:orientation horizontal/vertical 设置布局水平还是垂直,默认是垂直 android:checked true/false 标记默认选中,如 ...

  2. bzoj 1202: [HNOI2005]狡猾的商人 并查集好题

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2946  Solved: 1384[Submit][Sta ...

  3. 《Linux内核设计的艺术》学习笔记(二)INT 0x13中断

    参考资料: 1. <IBM-PC汇编语言程序设计> 2. http://blog.sina.com.cn/s/blog_5028978101008wk2.html 3. http://ww ...

  4. jQuery的Deferred

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. C#垃圾回收机制(GC)

    GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  6. php xml 操作。

    参考 文章:http://www.cnblogs.com/zcy_soft/archive/2011/01/26/1945482.html DOMDocument相关的内容. 属性: Attribut ...

  7. 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误

    原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...

  8. poj1673EXOCENTER OF A TRIANGLE

    链接 据说这题是垂心..数学太弱没有看出来,写了分朴实无华的代码.. 旋转三边得到图中的外顶点,然后连接三角形顶点求交点,交上WA..觉得没什么错误就去看了下discuss,发现都在说精度问题,果断开 ...

  9. Jdbc入门

    JDBC入门 l  导jar包:驱动! l  加载驱动类:Class.forName(“类名”); l  给出url.username.password,其中url背下来! l  使用DriverMa ...

  10. Android手机分辨率基础知识(DPI,DIP计算)二

    dp = dip : device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...