c++实现之 -- 文章TF-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值的计算的更多相关文章
- 使用solr的函数查询,并获取tf*idf值
1. 使用函数df(field,keyword) 和idf(field,keyword). http://118.85.207.11:11100/solr/mobile/select?q={!func ...
- 文本分类学习(三) 特征权重(TF/IDF)和特征提取
上一篇中,主要说的就是词袋模型.回顾一下,在进行文本分类之前,我们需要把待分类文本先用词袋模型进行文本表示.首先是将训练集中的所有单词经过去停用词之后组合成一个词袋,或者叫做字典,实际上一个维度很大的 ...
- 信息检索中的TF/IDF概念与算法的解释
https://blog.csdn.net/class_brick/article/details/79135909 概念 TF-IDF(term frequency–inverse document ...
- tf idf公式及sklearn中TfidfVectorizer
在文本挖掘预处理之向量化与Hash Trick中我们讲到在文本挖掘的预处理中,向量化之后一般都伴随着TF-IDF的处理,那么什么是TF-IDF,为什么一般我们要加这一步预处理呢?这里就对TF-IDF的 ...
- TF/IDF(term frequency/inverse document frequency)
TF/IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明. 一. TF/IDF描述单个term与特定document的相 ...
- TF/IDF计算方法
FROM:http://blog.csdn.net/pennyliang/article/details/1231028 我们已经谈过了如何自动下载网页.如何建立索引.如何衡量网页的质量(Page R ...
- tf–idf算法解释及其python代码实现(下)
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- tf–idf算法解释及其python代码实现(上)
tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...
- Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据
相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...
随机推荐
- find command in linux terminal
Say you want to search for files with keyword as "keyword". find / -name keyword 2> /de ...
- 20150203一些移动端H5小bug解决
都是一些我也不知道为什么会有的bug. 1. 在三星note2,小米2,页面加载后,页面有黑块. 那么提高被盖住的部分z-index. 2. iphone5 ,ios7.0.4,上文字显示不出 那么就 ...
- 《FLASH CC 2015 CANVAS 中文教程》——2、基本的交互(点击、触摸)事件
注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...
- NYOJ214
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...
- iOS - Photo Album 图片/相册管理
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...
- Dijkstra(迪杰斯特拉)源最短路径 小白说明
源最短路径 小白说明 Dijkstra算法,书上其实说的很简洁,仔细看,仔细思考是会理解的.但要先理解几条引论和推理. 而自己思考的思路在不需要任何推理只从贪心思路出发,和Dijkstra有所不同,但 ...
- Windows_cmd_命令
1. netstat -ano 查看端口占用情况 netstat -anp // 命令来查看一下,Linux系统是否在监听 3306 这个端口号 2.
- JavaWeb学习总结(四)—ServletConfig和ServletContext
一.ServletConfig 1. ServletConfig介绍: ServletConfig是Servlet中的init()方法的参数类型,服务器会在调用init()方法时传递ServletCo ...
- linux下,一些关于动态库的问题:
程序运行是加载动态库的几种方法: 第一种,通过ldconfig命令 ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令它,ldconfig命令通 ...
- 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小
原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...