bag of words in c++
#include <iostream>
#include <vector>
#include <cstddef>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <set>
#include <limits>
#include <functional>
#include <numeric> template <class DataType>
void ReadMatFromFile(std::string &filename, std::vector<std::vector<DataType> > &lines_feat) {
std::ifstream vm_info(filename.c_str());
std::string lines;
DataType var;
std::vector<DataType> row; lines_feat.clear(); while(!vm_info.eof()) {
getline(vm_info, lines);
if(lines.empty())
break;
std::stringstream stringin(lines);
row.clear(); while(stringin >> var) {
row.push_back(var);
}
lines_feat.push_back(row);
}
} void ReadStringFromFile(std::string &filename, std::vector<std::string> &in_string) {
std::ifstream vm_info(filename.c_str());
std::string lines, var; while(!vm_info.eof()) {
getline(vm_info, lines);
if(lines.empty())
break;
std::stringstream stringin(lines); while(stringin >> var) {
in_string.push_back(var);
}
}
} std::string lowerCase(const std::string& s) {
std::string lower(s);
for(size_t i=;i<s.length();++i) {
lower[i]=tolower(lower[i]);
}
return lower;
} std::string letters(const std::string& s) {
std::string letter;
for(size_t i=;i<s.length();++i) {
char ch=s.at(i);
bool flag=false;
if((ch>= && ch<=)) {
ch=ch+;
flag=true;
}
else if((ch>= && ch<=) || (ch>= && ch<=)) {
flag=true;
}
else {
;
}
if(flag) {
letter.push_back(ch);
}
}
letter.push_back('\0');
return letter;
} template <class T1, class T2>
int MatMultiply(const std::vector<std::vector<T1> > &Mata, const std::vector<std::vector<T2> > &Matb, std::vector<std::vector<T1> > &MatOut) {
if(Mata.at().size() != Matb.size()) {
std::cout<<"not match!\n";
return -;
}
for(size_t i=; i<Mata.size(); ++i) {
for(size_t j=; j<Matb.at().size(); ++j) {
std::vector<T2> col;
col.clear();
for(size_t k=; k<Matb.size(); ++k) {
col.push_back(Matb.at(k).at(j));
}
MatOut.at(i).at(j)=inner_product(Mata.at(i).begin(), Mata.at(i).end(), col.begin(), );
}
}
return ;
} template <class T1, class T2, class T3>
void outer_product(const std::vector<T1> &inst1, const std::vector<T2> &inst2, std::vector<std::vector<T3> > &out) {
std::vector<T3> temp_row(inst2.size()); for(typename::std::vector<T1>::const_iterator it=inst1.begin();it!=inst1.end();++it) {
transform(inst2.begin(), inst2.end(), temp_row.begin(), bind2nd(std::multiplies<T1>(), *it));
out.push_back(temp_row);
}
} void ReadDataFromFile(std::string &filename, std::vector<std::string> &lines_feat) {
std::ifstream vm_info(filename.c_str());
std::string lines; lines_feat.clear(); while(!vm_info.eof()) {
getline(vm_info, lines);
if(lines.empty())
break; lines_feat.push_back(lines);
}
} std::vector<std::string> split(const std::string& s, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s); while(std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
} int stringtoint(const std::string& s) {
std::istringstream iss(s);
int num;
return iss>>num?num:;
} void printip(const std::string& s) {
std::vector<std::string> temp, ip_segment; temp=split(s, '-');
ip_segment=split(temp.front(), '.'); std::string ip_start=ip_segment.back(), ip_end=temp.back();
int start, end;
start=stringtoint(ip_start);
end=stringtoint(ip_end); for(size_t i=start;i<=end;++i) {
std::cout<<ip_segment[]<<"."<<ip_segment[]<<"."<<ip_segment[]<<"."<<i<<"\n";
}
} template <class T>
void Display2DVector(std::vector<std::vector<T> > &vv) {
for(size_t i=;i<vv.size();++i) {
for(typename::std::vector<T>::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) {
std::cout<<*it<<" ";
}
std::cout<<"\n";
}
std::cout<<"--------the total of the 2DVector is "<<vv.size()<<std::endl;
} int main() {
std::string filename("data");
std::vector<std::string> v_string;
std::string words;
std::set<std::string> s_string; ReadStringFromFile(filename, v_string); for(std::vector<std::string>::const_iterator it=v_string.begin(); it!=v_string.end(); ++it) {
std::cout<<*it<<" ";
words=letters(*it);
s_string.insert(words);
}
std::cout<<std::endl;
for(std::set<std::string>::const_iterator it=s_string.begin(); it!=s_string.end(); ++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
return ;
}
The bag of words model ignores grammar and order of words.
运行结果如下,第一行为原始数据,第二行为提取后的数据:

bag of words in c++的更多相关文章
- 【ros】.bag文件
Bags are typically created by a tool like rosbag They store the serialized message data in a file as ...
- MATLAB 图像分类 Image Category Classification Using Bag of Features
使用MATLAB实现图像的识别,这是MATLAB官网上面的例子,学习一下. http://cn.mathworks.com/help/vision/examples/image-category-cl ...
- Bag标签之中的一个行代码实行中文分词实例2
例1: 分词(返回以逗号隔开每一个词带上引號的词组.gap=",",quotes="'"或quotes='"') 单引號 <bag id=pPa ...
- Bag Problem
Bag Problem Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others) Total ...
- Bag of mice(CodeForces 148D )
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Hibernate的集合映射(Set、List、Array、Map、Bag)
POJOs如下: Customer类------>customer表 Order类对应---------->orders表 customer(1)<-------------- ...
- Bag of Words/Bag of Features的Matlab源码发布
2010年11月19日 ⁄ 技术, 科研 ⁄ 共 1296字 ⁄ 评论数 26 ⁄ 被围观 4,150 阅读+ 由于自己以前发过一篇文章讲bow特征的matlab代码的优化的<Bag-Of-Wo ...
- Bag of Words(BOW)模型
原文来自:http://www.yuanyong.org/blog/cv/bow-mode 重复造轮子并不是完全没有意义的. 这几天忙里偷闲看了一些关于BOW模型的知识,虽然自己做图像检索到目前为止并 ...
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
Set类型的使用: <hibernate-mapping package="cn.model"> <class name="Department&quo ...
- CF 148D. Bag of mice (可能性DP)
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- Angular ZoneJS 原理
Zone.js到底是如何工作的? 原文链接: blog.kwintenp.com 如果你阅读过关于Angular 2变化检测的资料,那么你很可能听说过zone.Zone是一个从Dart中引入的特性并被 ...
- java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...
- * format-- set command window output display format
the displayed number may not match the input number due to display format default: 4 decimal syntax: ...
- BZOJ 1565 Luogu P2805 [NOI2009]植物大战僵尸 (Tarjan判环、最小割)
我: "立个flag 14点之前调完这题" 洛谷AC时间: 2019-06-24 14:00:16 实力打脸... 网络流板子从来写不对系列 题目链接: (BZOJ) https: ...
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- noip模拟赛 立方数2
题目描述LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数.LYK还定义了一个数叫“立方差数”,若一个数可以被写作是两个立 ...
- scrapy的User-Agent中间件、代理IP中间件、cookies设置、多个爬虫自定义settings设置
在scrapy的反爬中,常用的几个配置,简单总结了下: User-Agent中间件: from fake_useragent import UserAgent class RandomUserAgen ...
- [K/3Cloud]ksql翻译札记
2011-11-16 又学一招,集合转化临时表的方法: var sql = string.Format(@"select b.FENTRYID,a.{2} from {0} a inner ...
- 终于又弄完一个DJANGO BY EXAMPLE的测试
感觉学到了很多东东,都是以前开发不曾用到的. 知识的作用是到用时,知道到哪里去查相关的功能..:) 这个bookmarks就有很多功能点值得学习呢...