首先,是关键词的选取:
好吧这个我这模型实在是太简单了,但还是讲一讲比较好呢。。。
我们现在手头有的是一堆百度百科词条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. Apache-Shiro+Zookeeper系统集群安全解决方案之缓存管理

    上篇[Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理],解决了Shiro在系统集群开发时安全的会话共享问题,系统在使用过程中会有大量的权限检查和用户身份检验动作,为了不频繁 ...

  2. python_way ,day22 tonardo

    python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...

  3. JS学习笔记(二) 数据类型

    参考资料: 1. http://www.w3school.com.cn/js/js_datatypes.asp 2. http://blog.sina.com.cn/s/blog_85c1dc1001 ...

  4. XAF学习笔记之 Upcasting

    通常,我们会定义继承层次结构,假设有类型,CustomerBase,CustomerTrialed,CustomerRegistered三个类型,并且继承结构如下: 业务对象代码定义如下: using ...

  5. jQuery里面的普通绑定事件和on委托事件

    以click事件为例: 普通绑定事件:$('.btn1').click(function(){}绑定 on绑定事件:$(document).on('click','.btn2',function(){ ...

  6. iOS - UITextField

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding> @ava ...

  7. 批量创建客户主数据函数SD_CUSTOMER_MAINTAIN_ALL

    分享一下批创建客户主数据函数:SD_CUSTOMER_MAINTAIN_ALL TABLES:T077D,ZCITY,T005S,BNKA,ADRC,KNA1.  DATA: TMP_KTOKD(4) ...

  8. python 将页面保存为word

    将博客或者留言页面保存为word文档 -----------2016-5-11 14:40:04-- source:http://blog.csdn.net/how8586/article/detai ...

  9. nodejs学习笔记<一>安装及环境搭建

    零零散散学了几天nodejs,进度一直停滞不前,今天沉下心来好好看了下nodejs的介绍和代码.自己也试着玩了下,算是有点入门了. 这里来做个学习笔记. ——————————————————————— ...

  10. RedHat 7配置yum源

    卸载自带的yum软件包 rpm -e yum-utils--.el7.noarch --nodeps rpm -e yum-rhn-plugin--.el7.noarch --nodeps rpm - ...