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 ...
随机推荐
- install Matlab2016b on Ubuntu 14.04
From Download Download the install file from Download MATLAB, Simulink, Stateflow, and Other MathWor ...
- C++中的一些定义
PS: 这篇博客用来记录一些一般的C++书中草草掠过的一些概念. 或者一些不太容易理解的概念的详细解释. 欢迎新手进入,欢迎高手指正! Orz . 引用: 为对象起了另外一个名字, 引用类型引用(re ...
- Spring的自动扫描与管理
通过在classpath自动扫描方式把组件纳入spring容器中管理 前面的例子我们都是使用XML的bean定义来配置组件.在一个稍大的项目中,通常会有上百个组件,如果这些这组件采用xml的bean定 ...
- ASP.NET常用正则表达式
验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9][0-9]*)$ 验证 ...
- c++ 爬虫
这是一个简单的c++爬虫,效率并不是很高... #include<stdio.h> ],s2[]; void fun(int a, int b) { int i,ii; bool t1,t ...
- caffe的data_reader.cpp分析一下干了点什么
首先说明:下面的内容不一定对 类body: 变量:LayerParameter param_ :它里面放的是:body传进来的layerparameter的参数: BlockingQueue<s ...
- JQuery $()后面的括号里的内容什么时候加引号,什么时候不加
一.如果是已经声明存在的变量或者对象,就不用加引号. 比如var name=document.getElementById("name"); $(name)或者$(this). 二 ...
- Freemarker 入门示例(zhuan)
http://cuisuqiang.iteye.com/blog/2031768 ************************************ 初步学习freemarker ,先做一个简单 ...
- MVC HtmlHelper用法大全
MVC HtmlHelper用法大全HtmlHelper用来在视图中呈现 HTML 控件.以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ·Actio ...
- 转!!为什么要java环境变量配置?
1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin目录增加到现有的PATH ...