用中文把玩Google开源的Deep-Learning项目word2vec
google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算term之间的相似度,对term聚类等,该项目也支持phrase的自动识别,以及与term等同的计算。
word2vec项目首页:https://code.google.com/p/word2vec/,文档比较详尽,很容易上手。可能对于不同的系统和gcc版本,需要稍微改一下代码和makefile。具体到我的mac系统,源代码中所有#include <malloc.h>的地方都需要改成#include <stdlib.h>,makefile编译选项中的-Ofast要更改为-O2,-march=native -Wno-unused-result这两个编译选项都不认,使用予以删除。直接make,按照它的文档提示运行即可。
本文主要说说如何使用word2vec处理中文语料。首先我们打开demo-word.sh看看,脚本开始其实就是去下载语料并解压成text8文件,这份文件约96M大小,less看看其实就是纯文本的英文,每个单词之间有空格隔开:
anarchism originated as a term of abuse first used against early working class radicals including the diggers of the english revolution and the sans culottes of the french revolution whilst the term is still used in a pejorative way to describe any act that used violent means to destroy the organization of society it has also been taken up as a positive label by self defined anarchists the word anarchism is derived from the greek without archons ruler chief king anarchism as a political philosophy is the belief that rulers are unnecessary and should be abolished although there...
所以如果我们有一份分过词的中文语料,每个词(term)之间用空格隔开,就可以用word2vec来处理了。
分词我们使用开源的ansj_seg项目,该项目是用java实现中科院ictclas中的算法(下载ictclas没有源码,且linux 64bit的版本在64位mac下链接库报错,应该是不兼容,ictclas官方并未提供mac 64bit的版本)。ansj_seg的官方主页在:https://github.com/ansjsun/ansj_seg,运行:
git clone https://github.com/ansjsun/ansj_seg
下载该项目会报类似下面的错误:
error: RPC failed; result=, HTTP code = | KiB/s
fatal: The remote end hung up unexpectedly
Writing objects: % (/), 449.61 MiB | 4.19 MiB/s, done.
Total (delta ), reused (delta )
fatal: The remote end hung up unexpectedly
在stackoverflow上搜了下解决办法,需要执行下面的命令,配置git的缓冲区大小:
git config --global http.postBuffer
如果仍然失败的话,可以在ansj_seg主页直接下载项目的.zip文件,解压即可。
如果用eclipse打开该项目,还需要依赖一个tree-split-word的项目,这是一个Trie树实现用来查词表的项目,ansj_seg主页目前给出的链接已经失效,在github搜索treesplitword可以找到这个项目,下载后打成jar包,加入到ansj_seg的项目中,发现仍然有错,原因是当前的tree-split-word的很多接口都与ansj_seg中使用的不兼容了。
这时发现ansj_seg是一个maven项目,直接使用mvn compile命令编译,会自动下载其所需依赖,整个编译过程没有报错,最终取得成功。从中提取出项目使用的tree_split-1.0.1.jar,加入到eclipse项目中,重新build一下,eclipse中的红叉消失。
到ansj_seg项目中的src/demo/java/下的org.ansj.demo包中跑一跑每一个demo文件,会遇到以下问题:
1. 报错找不到library.properties文件,将项目根目录下的library.properties.bak copy成library.properties,并注意添加eclipse项目中的classpath,可以解决这个问题;
2. 初始化词典时会报找不到nature/nature.map文件(词性映射文件,ansj_seg不仅有分词的功能,还能词性标注),find . -iname nature.map会发现其实这个文件是存在的,可以直接加eclipse的classpath指向ansj_seg/src/main/resources目录即可;
3. 跑demo时可能会报OutOfMemory的错误,加载词典可能超出了eclipse的默认jvm大小,可以在run as时,设定argument,-Xmx512M -Xms512M即可。
解决上述问题后,除了demo中需要读文件的没有,其他demo都能成功运行。ansj_seg的接口也非常简洁易用,很容易编程读取自己的语料文件进行分词。
最后我们需要解决的就是语料问题。搜狗实验室公布了许多有用的语料,我使用其中的全网新闻数据:http://www.sogou.com/labs/dl/ca.html,填写姓名邮箱申请得到用户名密码,可以获得一个ftp下载地址。首先下载一个迷你语料news_tensite_xml.smarty.tar.gz做做简单实验,解压文件可以看到文本的格式,很容易理解,该文件是gbk编码的,可以转成utf-8编码再进行处理。基于这个迷你语料和ansj_seg编程分词,生成word2vec的输入文件。代码如下:
package org.ansj.demo; import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import love.cq.util.IOUtil; import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis; public class MyFileDemo { public static final String TAG_START_CONTENT = "<content>";
public static final String TAG_END_CONTENT = "</content>"; public static void main(String[] args) {
String temp = null ; BufferedReader reader = null;
PrintWriter pw = null;
try {
reader = IOUtil.getReader("corpus.txt", "UTF-8") ;
ToAnalysis.parse("test 123 孙") ;
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream
("resultbig.txt"), "UTF-8"), true);
long start = System.currentTimeMillis() ;
int allCount =0 ;
int termcnt = 0;
Set<String> set = new HashSet<String>();
while((temp=reader.readLine())!=null){
temp = temp.trim();
if (temp.startsWith(TAG_START_CONTENT)) {
int end = temp.indexOf(TAG_END_CONTENT);
String content = temp.substring(TAG_START_CONTENT.length(), end);
//System.out.println(content);
if (content.length() > 0) {
allCount += content.length() ;
List<Term> result = ToAnalysis.parse(content);
for (Term term: result) {
String item = term.getName().trim();
if (item.length() > 0) {
termcnt++;
pw.print(item.trim() + " ");
set.add(item);
}
}
pw.println();
}
}
}
long end = System.currentTimeMillis() ;
System.out.println("共" + termcnt + "个term," + set.size() + "个不同的词,共 "
+allCount+" 个字符,每秒处理了:"+(allCount*1000.0/(end-start)));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != pw) {
pw.close();
}
}
}
}
生成的resultbig.txt即为分好词用空格隔开的语料,使用word2vec,运行:
./word2vec -train resultbig.txt -output vectors.bin -cbow -size -window -negative -hs -sample 1e- -threads -binary
./distance vectors.bin
vectors.bin是word2vec处理resultbig.txt生成的term的向量文件,./distance命令加载该文件计算term之间的距离。mini文件能够跑通,但距离没有什么意义,因为语料实在太少了(word2vec中说语料越多效果越好)。
这时可以放心的下载700+M的全网新闻语料了,最好做下预处理只取出有content的行并转码,限于本人的机器,我只取出前20W行进行分词:
cat news_tensite_xml.dat | iconv -f gbk -t utf- -c | grep "<content>" | head -n > corpus.txt
corpus.txt大小是200+M,分词后有4000W+的term,使用word2vec处理,最后得到的结果还有点意思:
200+M的语料,这个结果是不是相当凑合了?小伙伴们,快玩起来吧!剩下phrase,classify的功能各位可以自行探索:)
用中文把玩Google开源的Deep-Learning项目word2vec的更多相关文章
- Deep Learning基础--word2vec 中的数学原理详解
word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了很多人的关注.由于 word2vec 的作者 Tomas Miko ...
- 利用中文数据跑Google开源项目word2vec
一直听说word2vec在处理词与词的相似度的问题上效果十分好,最近自己也上手跑了跑Google开源的代码(https://code.google.com/p/word2vec/). 1.语料 首先准 ...
- Google开源的Deep-Learning项目word2vec
用中文把玩Google开源的Deep-Learning项目word2vec google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算te ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- 机器学习(Machine Learning)&深入学习(Deep Learning)资料
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林. ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料汇总 (上)
转载:http://dataunion.org/8463.html?utm_source=tuicool&utm_medium=referral <Brief History of Ma ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008 ...
- 机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
随机推荐
- 在现有 SharePoint 服务器上安装 PowerPivot for SharePoint
步骤1: 检查 SQL Server 2008 R2 Analysis Services 实例的“程序”文件夹.如果您找到了现有安装或之前安装的证据,则执行剩余步骤.否则,直接执行步骤 2:安装 Po ...
- spring aop编程
1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代 ...
- js控制html元素的readonly属性
html元素假设为只读,那么其readonly="readonly",我们现在想通过js来改变readonly属性为可以输入. 初始时,两个输入框都是只读.点击change按钮后, ...
- [Js]焦点图轮播效果
一.所用到的知识点 1.DOM操作 2.定时器 3.事件运用 4.Js动画 5.函数递归 6.无限滚动大法 二.结构和样式 <div id="banner" class=&q ...
- ubnutu安装sougou 输入法
先安百度经验安装fcitx 1.首先下载sogoupinyin_2.0.0.0068_amd64.deb,点击安装后,会通过ubuntu软件中心安装,安装玩成后,任然是无法使用.然后: 2.然后执行下 ...
- not use jquery
document.getElementById('myElement');document.querySelector('#myElement'); document.getElementsByCla ...
- IT公司100题-11-求二叉树中节点的最大距离
问题描述: 写程序,求一棵二叉树中相距最远的两个节点之间的距离. 10/ \6 14/ \ / \4 8 12 16 分析: 二叉树中最远的两个节点,要么是根 ...
- poj 1797 Heavy Transportation(最短路径Dijkdtra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26968 Accepted: ...
- android textview 跑马灯
<TextView android:layout_width="match_parent" android:layout_height="48dp" an ...
- ajax上传组件
BJUI框架的异步上传组件功能. 初始化: 1.Data属性:div添加属性data-toggle="upload"后可触发上传组件. 示例代码: <div class=&q ...