添加 'tag_cloud.cpp'
This commit is contained in:
parent
fbae063c66
commit
0fcee9bc9a
95
tag_cloud.cpp
Normal file
95
tag_cloud.cpp
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
#include "MySQLWrapper.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
#include "local_db_account.h"
|
||||||
|
#include "util.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/// tag_cloud 全国公司的城市分布
|
||||||
|
|
||||||
|
MySQLConn conn;
|
||||||
|
conn.connect("127.0.0.1",db_user,db_passwd,"bigdata3",3306);
|
||||||
|
cout<<"Connected to DB"<<endl;
|
||||||
|
conn.exec("set names gbk",nullptr);
|
||||||
|
|
||||||
|
map<string,int> mp;
|
||||||
|
conn.exec("select tags from lagou_job",[&](MySQLResult& res)
|
||||||
|
{
|
||||||
|
res.stepRow([&](char** val,unsigned long* len)
|
||||||
|
{
|
||||||
|
|
||||||
|
string tags(Trim(string(val[0])));
|
||||||
|
vector<string> tagvec=ParseTag(tags.c_str());
|
||||||
|
|
||||||
|
for(const auto& tag:tagvec)
|
||||||
|
{
|
||||||
|
auto iter=mp.find(tag);
|
||||||
|
if(iter!=mp.end())
|
||||||
|
{
|
||||||
|
iter->second += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mp.insert(make_pair(tag,1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.exec("use bigdata5",nullptr);
|
||||||
|
conn.exec("select tags from pages",[&](MySQLResult& res)
|
||||||
|
{
|
||||||
|
res.stepRow([&](char** val,unsigned long* len)
|
||||||
|
{
|
||||||
|
|
||||||
|
string tags(Trim(string(val[0])));
|
||||||
|
vector<string> tagvec=ParseTagX(tags.c_str());
|
||||||
|
|
||||||
|
for(const auto& tag:tagvec)
|
||||||
|
{
|
||||||
|
auto iter=mp.find(tag);
|
||||||
|
if(iter!=mp.end())
|
||||||
|
{
|
||||||
|
iter->second += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mp.insert(make_pair(tag,1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
vector<pair<string,int>> vec;
|
||||||
|
for(const auto& pr:mp)
|
||||||
|
{
|
||||||
|
vec.push_back(pr);
|
||||||
|
}
|
||||||
|
sort(vec.begin(),vec.end(),[](const pair<string,int>& a,const pair<string,int>& b)
|
||||||
|
{
|
||||||
|
return a.second > b.second;
|
||||||
|
});
|
||||||
|
|
||||||
|
ofstream ofs("result/tag_cloud.csv");
|
||||||
|
#define cout ofs
|
||||||
|
cout<<"标签,出现次数"<<endl;
|
||||||
|
int szVec=vec.size();
|
||||||
|
for(int i=0;i<szVec;i++)
|
||||||
|
{
|
||||||
|
cout<<vec[i].first<<","<<vec[i].second<<endl;
|
||||||
|
}
|
||||||
|
#undef cout
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user