经过运行抓取程序,抓到的数据网站数量为4305个,接下来,需要进行分词处理,分词后,再统计词出现的次数,词出现的次数一部分提现了本类网站中该次的竞争情况。分词使用的hanlp开源项目,关于该开源项目的引用与使用,此处不详细介绍,读者可以访问 https://github.com/hankcs/HanLP了解详情。本篇博客涵盖的内容包括:分词、统计词频、结果保存数据库。表结构和相关代码如下:
表名:relative_hotwords |
表中文名:相关热词信息表 |
字段名称 | 字段类型 | 字段解释 |
keywords | varchar(100) | 关键词 |
rh_times | int | 出现次数 |
rh_title_times | int | 在title中出现的次数 |
rh_keyword_tiems | int | 在keywords中出现的次数 |
rh_description_times | int | 在description中出现的次数 |
rh_other_times | int | 在网页其他地方出现的次数 |
rh_hot_score | int | 词热度(百度指数) |
rh_pc_score | int | 词在PC端的热度(百度指数) |
rh_wise_score | int | 词在移动端的热度(百度指数) |
建表语句:
Create table relative_hotwords(keywords varchar(100),rh_times int,rh_title_times int,
rh_keyword_tiems int,rh_description_times int, rh_other_times int,rh_hot_score int, rh_pc_score int, rh_wise_score int) character set utf8mb4 collate utf8mb4_bin;
java代码:
/**
* 分词
*/
public void segWordsAndSave(){
Sort sort = new Sort();
Sqlca sqlca = null;
try {
sqlca = sort.getSqlca();
sqlca.execute("select web_html from web_detail limit 1220,5000");
FileTool ft=new FileTool();
int k=0;
while (sqlca.next()){
ft.saveRowToFile(sqlca.getString("web_html"),"C:\\temp\\hotwords\\t.txt");
System.out.println(k++);
}
ComputeHis ch=new ComputeHis();
ch.segFileWord("C:\\temp\\hotwords\\t.txt","C:\\temp\\hotwords\\t2.txt",0,0);
}catch (Exception e){
e.printStackTrace();
} finally {
if (sqlca != null) sqlca.closeAll();
}
}
/**
* 统计词频
*/
public void wordCoutn(){
List<String> ls=new ArrayList<>();
NlpTool nt= new NlpTool();
List<String> res=new ArrayList<>();
FileTool ft=new FileTool();
ComputeHis ch=new ComputeHis();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\temp\\hotwords\\t2.txt"),"utf-8"));
int x=0;
String line=null;
String content="";
Map<String ,Integer> mp=new HashMap <String ,Integer> ();
while ((line = reader.readLine()) != null) {
x++;
if(x%100000==0) {
System.out.println(x+" "+new Date());
}
String[] words=line.split(" ");
for(String word:words){
if(mp.containsKey(word)){
mp.put(word,mp.get(word) +1);
}else{
mp.put(word, 1);
}
}
}
reader.close();
Set<String> set=mp.keySet();
java.util.SortedMap<String, String> topN = new java.util.TreeMap<String, String>();
for(String key:set){
topN.put((10000000 - mp.get(key))+"-"+key,"");
}
set=topN.keySet();
System.out.println(set.size());
int i=0;
for(String key:set){
String[] words=key.split("-");
if(words==null){
continue;
}
if(words.length<2){
continue;
}
words[1]=words[1].replace("\r","").replace("\t","").replace("\n","");
if(words[1]!=null&&!words[1].isEmpty()&&!ch.isStop(words[1])&&words[1].length()>1){
res.add(key);
i++;
}
// if(i>=20) break;
}
ft.saveListToFile("C:\\temp\\hotwords\\wdcount.txt",res);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 多线程解析html,抽取关键词信息
* @param fileName
*/
public void getKeyWordsFromHtml(String fileName){
FileTool ft=new FileTool();
Sort sort = new Sort();
NlpTool nlpTool=new NlpTool();
Sqlca sqlca = null;
Sqlca sqlcah = null;
try {
sqlca = sort.getSqlca();
sqlcah=sort.getSqlcaH();
int webNums=0;
//获得网页数量
sqlca.execute("select count(*) ct from web_detail");
if(sqlca.next()){
webNums=sqlca.getInt("ct");
}
if(webNums==0){ //没有网页数据
sqlca.closeAll();
return;
}
System.out.println(webNums);
for(int i=0;i<8;i++){
String parm=fileName;
int step=webNums/8;
int begin=i*step;
int end=(i+1)*step;
if(end<webNums && end >(webNums - step)) end=webNums;
parm+=","+begin+","+end;
// System.out.println(parm);
final ActorRef ta = system.actorOf(Props.create(AnalyseHtmlActor.class));
ta.tell(parm, ActorRef.noSender());
if(end>=webNums) break;
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(sqlca!=null) sqlca.closeAll();
if(sqlcah!=null) sqlcah.closeAll();
}
}
分享到:
相关推荐
基于机器学习语言Python实现中文文本的jieba分词、词频统计。
需要下载数据库可视工具(SQLyog)将text文件复制粘贴到数据库中,将压缩文件导入eclipse
4.先对五百多条数据进行人工标记作为训练集,再将所有数据都进行标记以便计算准确率,随后运行程序对所有数据进行贝叶斯情感倾向分析,根据分析结果和人工标记结果进行对比,计算准确率。 5.对实验方法进行分析和...
Python 分词,词频统计,寻找公共词
python毕设设计-基于情感分析与词频的网络舆情分析+源代码+文档说明 以知乎平台为数据来源,针对知乎文本与知乎评论的特点,实现基于情感词典的情感分析,包括情感倾向和情感波动程度。对于不同情感倾向的评论内容...
分类词频是文本分析中非常重要的内容。本资源把常用的分类词收集起来,方便大家在做文本分析时使用。有了本资源,大家可以提高文本分析的效率
数据存储实验5-编写MapReduce程序实现词频统计.doc(实验报告)
IK 中文分词 版本 5.0,IkAnalyzer分词、词频、内链优化,可以自定义分词词库 看了这个项目你就可以熟练运用ik的分词了
主要是读取文本,然后进行分词、词干提取、去停用词、计算词频,有界面,很实用
IKAnalyzer 实现分词 词频统计,用Eclipse实现,调用IKanalyzer的类,可以用于IKanalyzer的二次开发
PHP简易中文分词,免组件分词 $ca = new cls_analysis(); //把一段短文本进行拆分 $str = "把一段短文本进行拆分"; $ca->SetSource( $str, 'utf-8', 'utf-8'); $ca->StartAnalysis(); $okstr = $ca->...
一个小小的英文分词并统计词频程序 并用Jfreechart实现了图表化
分词统计词频.exe
【例 2-1】WordCount(词频统计).ipynb
2001-2024年绿色环保词频统计数据,结合政府工作报告文本的词频分析法,可以 进行一系列深入而富有意义的研究。以下是四点可能的研究方向:绿色环保政策演变与趋势 分析:通过对历年政府工作报告中的绿色环保词汇...