Training a Chinese Wikipedia Word2Vec Model by Gensim and Jieba
We have posted two methods for training a word2vec model based on English wikipedia data: “Training Word2Vec Model on English Wikipedia by Gensim” and “Exploiting Wikipedia Word Similarity by Word2Vec“. Based on the pipeline and related scripts: Wikipedia_Word2vec,we can train a Chinese wikipedia word2vec model quickly, the only difference is that Chinese text need word segmentation. This article will use Jieba for Chinese word segmentation, which is a pure Python Chinese word segmentation module. For users who didn’t familiar with word2vec, we still recommended the article “Getting started with Word2Vec” by Text Process.
First, download the latest Chinese Wikipedia dump data: https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2. You can use “wget https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2” or download it directly. The Chinese wikipedia dump data is about 1.3G, and about one tenth of the English wikipedia dump data.
Then you can use the process_wiki.py script to get the text from the bzip and xml wikipedia data based on Gensim wikipedia process script, which use pyhton multiprocessing module to accelerated the process speed:
python process_wiki.py zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text
2017-07-06 15:49:48,398: INFO: running process_wiki.py data/zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text 2017-07-06 15:50:35,378: INFO: Saved 10000 articles 2017-07-06 15:51:15,334: INFO: Saved 20000 articles 2017-07-06 15:51:55,697: INFO: Saved 30000 articles 2017-07-06 15:52:31,467: INFO: Saved 40000 articles 2017-07-06 15:53:10,841: INFO: Saved 50000 articles 2017-07-06 15:53:49,884: INFO: Saved 60000 articles ...... ...... 2017-07-06 16:13:09,799: INFO: Saved 260000 articles 2017-07-06 16:14:04,062: INFO: Saved 270000 articles 2017-07-06 16:14:52,100: INFO: Saved 280000 articles 2017-07-06 16:15:47,138: INFO: Saved 290000 articles 2017-07-06 16:15:59,102: INFO: finished iterating over Wikipedia corpus of 292315 documents with 65734584 positions (total 2974751 articles, 78416037 positions before pruning articles shorter than 50 words) 2017-07-06 16:15:59,107: INFO: Finished Saved 292315 articles |
It extracted about 290000 articles texts, which saved in the wiki.zh.text, one article per line, about 964M. We can view it by “head wiki.zh.text”, and got some sample text:
一些人例如尼可拉斯 塔雷伯 nassim nicholas taleb 則批評經濟學的理論方式 然而如同其他的批評 例如諾貝爾經濟學獎得主丹尼爾 卡內曼便對塔雷伯的理論讚譽有嘉 而儘管這些批評的存在 參見 经济 经济学家 经济学学士 經濟政策 社會資本 無條件基本收入 政治經濟學 家庭經濟學 參考文獻 外部連結 一般資訊 網路上的經濟學期刊 economics 大英百科有關經濟學的條目 經濟學家資源 美國經濟學會維持的資料庫 機構和組織 世界各地的經濟學系所 部門 和研究中心 oecd statistics united nations statistics division 聯合國經濟數據資料 world bank data 世界銀行經濟資料 學習資源 social science research network merlot learning materials economics 美國的經濟學資源網站 麻省理工 開放式課程 經濟學首頁 麻省理工學院的開放式經濟學課程 麻省理工 開放式課程 經濟學首頁 中文版本 online learning and teaching materials 英國的經濟學線上學習資料庫 the library of economics and liberty econlib 經濟學和自由圖書館 mercatus center at george mason university 喬治梅森大學的市場研究中心 延伸閱讀 經濟學家的魔鬼式思考 think mccann charles robert jr the elgar dictionary of economic quotations edward elgar preview pokrovskii vladimir econodynamics the theory of social production springer berlin fabian lindner investment accounting as an indispensable guide to economic theory institut für makroökonomie
The data combined with simplified Chinese, traditional Chinese and English Text. We can convert the traditional Chinese to Simplified Chinese by OpenCC, and certainly, you should Install OpenCC first, which depends the system you use. After installed opencc, then use the following command to convert traditional Chinese to simplified Chinese text:
opencc -i wiki.zh.text -o wiki.zh.text.simple -c zht2zhs.ini
Then view it by “head wiki.zh.text.simple”:
一些人例如尼可拉斯 塔雷伯 nassim nicholas taleb 则批评经济学的理论方式 然而如同其他的批评 例如诺贝尔经济学奖得主丹尼尔 卡内曼便对塔雷伯的理论赞誉有嘉 而尽管这些批评的存在 参见 经济 经济学家 经济学学士 经济政策 社会资本 无条件基本收入 政治经济学 家庭经济学 参考文献 外部连结 一般资讯 网路上的经济学期刊 economics 大英百科有关经济学的条目 经济学家资源 美国经济学会维持的资料库 机构和组织 世界各地的经济学系所 部门 和研究中心 oecd statistics united nations statistics division 联合国经济数据资料 world bank data 世界银行经济资料 学习资源 social science research network merlot learning materials economics 美国的经济学资源网站 麻省理工 开放式课程 经济学首页 麻省理工学院的开放式经济学课程 麻省理工 开放式课程 经济学首页 中文版本 online learning and teaching materials 英国的经济学线上学习资料库 the library of economics and liberty econlib 经济学和自由图书馆 mercatus center at george mason university 乔治梅森大学的市场研究中心 延伸阅读 经济学家的魔鬼式思考 think mccann charles robert jr the elgar dictionary of economic quotations edward elgar preview pokrovskii vladimir econodynamics the theory of social production springer berlin fabian lindner investment accounting as an indispensable guide to economic theory institut für makroökonomie
Continue, we can clean the ASCII characters by clean_assii.py and got most of simplified Chinese text data:
python clean_assii.py < wiki.zh.text.simple > wiki.zh.text.simple.clean
一些人例如尼可拉斯塔雷伯则批评经济学的理论方式然而如同其他的批评例如诺贝尔经济学奖得主丹尼尔卡内曼便对塔雷伯的理论赞誉有嘉而尽管这些批评的存在参见经济经济学家经济学学士经济政策社会资本无条件基本收入政治经济学家庭经济学参考文献外部连结一般资讯网路上的经济学期刊大英百科有关经济学的条目经济学家资源美国经济学会维持的资料库机构和组织世界各地的经济学系所部门和研究中心联合国经济数据资料世界银行经济资料学习资源美国的经济学资源网站麻省理工开放式课程经济学首页麻省理工学院的开放式经济学课程麻省理工开放式课程经济学首页中文版本英国的经济学线上学习资料库经济学和自由图书馆乔治梅森大学的市场研究中心延伸阅读经济学家的魔鬼式思考üö
“head wiki.zh.text.simple.clean” and we found that some non asscii and non Chinese texts are kept.
Now it’s time to process Chinese text by Word Segmentation tool Jieba:
sudo pip install jieba
python -m jieba wiki.zh.text.simple.clean > wiki.zh.text.simple.clean.seg -d ' '
Note that the word segmentation process time is very slow:
一些 人 例如 尼可 拉斯 塔雷 伯则 批评 经济学 的 理论 方式 然而 如同 其他 的 批评 例如 诺贝尔经济学奖 得主 丹尼尔 卡内 曼便 对 塔雷 伯 的 理论 赞誉 有嘉而 尽管 这些 批评 的 存在 参见 经济 经济学家 经济学 学士 经济 政策 社会 资本 无条件 基本 收入 政治经济学 家庭 经济学 参考文献 外部 连结 一般 资讯网 路上 的 经济学 期刊 大英 百科 有关 经济学 的 条目 经济学家 资源 美国 经济学 会 维持 的 资料库 机构 和 组织 世界各地 的 经济学 系所 部门 和 研究 中心 联合国 经济 数据资料 世界银行 经济 资料 学习 资源 美国 的 经济学 资源 网站 麻省理工 开放式 课程 经济学 首页 麻省理工学院 的 开放式 经济学 课程 麻省理工 开放式 课程 经济学 首页 中文 版本 英国 的 经济学 线上 学习 资料库 经济学 和 自由 图书馆 乔治 梅森 大学 的 市场 研究 中心 延伸 阅读 经济学家 的 魔鬼 式 思考 ü ö
Now we train the Chinese Word2Vec Model by train_word2vec_model.py
python train_word2vec_model.py wiki.zh.text.simple.clean.seg wiki.zh.text.model wiki.zh.text.vector
python train_word2vec_model.py wiki.zh.text.simple.clean.seg wiki.zh.text.model wiki.zh.text.vector 2017-07-08 10:58:42,708: INFO: running train_word2vec_model.py wiki.zh.text.simple.clean.seg wiki.zh.text.model wiki.zh.text.vector 2017-07-08 10:58:42,713: INFO: collecting all words and their counts 2017-07-08 10:58:42,725: INFO: PROGRESS: at sentence #0, processed 0 words, keeping 0 word types 2017-07-08 10:58:49,299: INFO: PROGRESS: at sentence #10000, processed 11125612 words, keeping 529234 word types 2017-07-08 10:58:54,217: INFO: PROGRESS: at sentence #20000, processed 19054511 words, keeping 768565 word types 2017-07-08 10:58:58,327: INFO: PROGRESS: at sentence #30000, processed 26132426 words, keeping 941084 word types ...... ...... ...... 2017-07-08 11:00:06,561: INFO: PROGRESS: at sentence #270000, processed 144779032 words, keeping 3105570 word types 2017-07-08 11:00:08,906: INFO: PROGRESS: at sentence #280000, processed 148671270 words, keeping 3160412 word types 2017-07-08 11:00:12,426: INFO: PROGRESS: at sentence #290000, processed 152424222 words, keeping 3210542 word types 2017-07-08 11:00:13,110: INFO: collected 3223602 word types from a corpus of 153331911 raw words and 292608 sentences 2017-07-08 11:00:13,110: INFO: Loading a fresh vocabulary 2017-07-08 11:00:24,468: INFO: min_count=5 retains 612651 unique words (19% of original 3223602, drops 2610951) 2017-07-08 11:00:24,468: INFO: min_count=5 leaves 149621746 word corpus (97% of original 153331911, drops 3710165) 2017-07-08 11:00:28,063: INFO: deleting the raw counts dictionary of 3223602 items 2017-07-08 11:00:28,679: INFO: sample=0.001 downsamples 17 most-common words 2017-07-08 11:00:28,679: INFO: downsampling leaves estimated 141082537 word corpus (94.3% of prior 149621746) 2017-07-08 11:00:28,679: INFO: estimated required memory for 612651 words and 200 dimensions: 1286567100 bytes 2017-07-08 11:00:32,407: INFO: resetting layer weights 2017-07-08 11:00:49,796: INFO: training model with 4 workers on 612651 vocabulary and 200 features, using sg=0 hs=0 sample=0.001 negative=5 window=5 2017-07-08 11:00:49,796: INFO: expecting 292608 sentences, matching count from corpus used for vocabulary survey 2017-07-08 11:00:50,867: INFO: PROGRESS: at 0.01% examples, 127080 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:00:51,887: INFO: PROGRESS: at 0.02% examples, 252045 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:00:52,908: INFO: PROGRESS: at 0.04% examples, 297778 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:00:53,935: INFO: PROGRESS: at 0.06% examples, 326141 words/s, in_qsize 7, out_qsize 0 ...... ...... ...... 2017-07-08 11:29:08,561: INFO: PROGRESS: at 99.45% examples, 413718 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:29:09,587: INFO: PROGRESS: at 99.54% examples, 413720 words/s, in_qsize 7, out_qsize 0 2017-07-08 11:29:10,627: INFO: PROGRESS: at 99.62% examples, 413701 words/s, in_qsize 7, out_qsize 0 2017-07-08 11:29:11,629: INFO: PROGRESS: at 99.70% examples, 413712 words/s, in_qsize 7, out_qsize 0 2017-07-08 11:29:12,678: INFO: PROGRESS: at 99.79% examples, 413691 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:29:13,681: INFO: PROGRESS: at 99.89% examples, 413710 words/s, in_qsize 7, out_qsize 0 2017-07-08 11:29:14,684: INFO: PROGRESS: at 99.99% examples, 413742 words/s, in_qsize 8, out_qsize 0 2017-07-08 11:29:14,746: INFO: worker thread finished; awaiting finish of 3 more threads 2017-07-08 11:29:14,752: INFO: worker thread finished; awaiting finish of 2 more threads 2017-07-08 11:29:14,767: INFO: worker thread finished; awaiting finish of 1 more threads 2017-07-08 11:29:14,773: INFO: worker thread finished; awaiting finish of 0 more threads 2017-07-08 11:29:14,773: INFO: training on 766659555 raw words (705412162 effective words) took 1704.9s, 413754 effective words/s 2017-07-08 11:29:14,773: INFO: saving Word2Vec object under wiki.zh.text.model, separately None 2017-07-08 11:29:14,774: INFO: not storing attribute syn0norm 2017-07-08 11:29:14,774: INFO: storing np array 'syn0' to wiki.zh.text.model.wv.syn0.npy 2017-07-08 11:29:22,803: INFO: storing np array 'syn1neg' to wiki.zh.text.model.syn1neg.npy 2017-07-08 11:29:30,626: INFO: not storing attribute cum_table 2017-07-08 11:29:42,667: INFO: saved wiki.zh.text.model 2017-07-08 11:29:42,667: INFO: storing 612651x200 projection weights into wiki.zh.text.vector |
Cause the train data is small(about 800M), the word2vec model tarining time is just about 30 minutes on my macbook. Now we can test the word2vec model by ipython:
Python 2.7.6 (default, Jun 3 2014, 07:43:23) Type "copyright", "credits" or "license" for more information. IPython 3.1.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import gensim In [2]: model = gensim.models.Word2Vec.load("wiki.zh.text.model") In [3]: model.most_similar(u"中国") Out[3]: [(u'\u6211\u56fd', 0.5960298776626587), (u'\u4e2d\u56fd\u653f\u5e9c', 0.5468955636024475), (u'\u5317\u4eac', 0.532685399055481), (u'\u4e9a\u6d32', 0.514533519744873), (u'\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd', 0.5048998594284058), (u'\u5168\u56fd', 0.5001850128173828), (u'\u4e2d\u534e\u6c11\u56fd', 0.4909243881702423), (u'\u6b27\u7f8e\u56fd\u5bb6', 0.48412859439849854), (u'\u6b27\u6d32', 0.48193493485450745), (u'\u97e9\u56fd', 0.48001396656036377)] In [4]: for element in model.most_similar(u"中国"): print element[0], element[1] ...: 我国 0.596029877663 中国政府 0.546895563602 北京 0.532685399055 亚洲 0.514533519745 中华人民共和国 0.504899859428 全国 0.500185012817 中华民国 0.49092438817 欧美国家 0.484128594398 欧洲 0.481934934855 韩国 0.48001396656 |
You can view it online by Word Similarity: 中国
Word Similarity for 北京:
In [5]: for element in model.most_similar(u"北京"): print element[0], element[1] ...: 上海 0.707935512066 天津 0.677978992462 北京市 0.667232036591 南京 0.628929018974 哈尔滨 0.581048846245 杭州 0.569507956505 沈阳 0.565321862698 武汉 0.560948610306 北平 0.560232043266 重庆 0.556844830513 |
Word Similarity for 足球:
In [6]: for element in model.most_similar(u"足球"): print element[0], element[1] ...: 国际足球 0.625163078308 篮球 0.614441275597 排球 0.605642318726 足球运动 0.587682127953 足球队 0.581415295601 橄榄球 0.574485719204 足球联赛 0.572779536247 体育 0.553898692131 国脚 0.552846908569 冰球 0.551464557648 |
Word Similarity for 男人:
In [7]: for element in model.most_similar(u"男人"): print element[0], element[1] ...: 女人 0.838114380836 家伙 0.703078508377 傻瓜 0.615101218224 女孩 0.581586837769 有钱人 0.580834746361 小伙子 0.578685224056 陌生人 0.559026062489 胆小鬼 0.555587291718 老公 0.552567601204 爸爸 0.549184322357 |
Word Similarity for 女人:
In [8]: for element in model.most_similar(u"女人"): print element[0], element[1] ...: 男人 0.838114380836 陌生人 0.589283287525 家伙 0.571405649185 女孩 0.562768340111 真爱 0.555871486664 傻瓜 0.552365720272 爱情 0.530165553093 情人 0.526921749115 老婆 0.52318751812 孩子 0.523067653179 |
Word Similarity for 狗:
In [9]: for element in model.most_similar(u"狗"): print element[0], element[1] ...: 老鼠 0.728063583374 猫 0.673563241959 兔子 0.639185011387 猴子 0.638433337212 流浪狗 0.612280488014 小狗 0.608574151993 鸡 0.599590480328 犬 0.595648407936 鳄鱼 0.59441614151 青蛙 0.590279698372 |
Word Similarity for 父亲:
In [10]: for element in model.most_similar(u"父亲"): print element[0], element[1] ....: 母亲 0.888669133186 舅舅 0.765486001968 独生子 0.756587922573 继父 0.756323695183 祖父 0.753659427166 弟弟 0.740678429604 兄长 0.717780351639 哥哥 0.714742064476 双亲 0.713911414146 祖母 0.713838636875 |
Other examples:
In [11]: for element in model.most_similar(u"衣服"): print element[0], element[1] ....: 鞋子 0.825856983662 衣物 0.807349562645 裤子 0.761347055435 内裤 0.739698946476 外套 0.738429844379 衬衫 0.734301269054 大衣 0.733580827713 围裙 0.732656478882 裙子 0.722471475601 假发 0.719616174698 In [12]: for element in model.most_similar(u"皇帝"): print element[0], element[1] ....: 君主 0.647822260857 太后 0.625437617302 太上皇 0.613322913647 皇太后 0.603848576546 摄政王 0.60162371397 帝 0.600255727768 大汗 0.598622083664 太皇太后 0.591811478138 国王 0.591526985168 登基 0.584462404251 In [13]: for element in model.most_similar(u"公主"): print element[0], element[1] ....: 王妃 0.603117823601 王后 0.5905418396 郡主 0.589420795441 黑雪 0.555929780006 白雪公主 0.550322949886 侍女 0.548328816891 王女 0.548197209835 下嫁 0.53687363863 碧姬 0.529789149761 树雷星 0.527166485786 In [14]: for element in model.most_similar(u"北京大学"): print element[0], element[1] ....: 清华大学 0.815926253796 复旦大学 0.812699556351 燕京大学 0.80637717247 武汉大学 0.804839968681 中国人民大学 0.789432823658 南开大学 0.780162930489 北京师范大学 0.760734438896 浙江大学 0.753832280636 华东师范大学 0.750696480274 上海大学 0.745433807373 In [15]: for element in model.most_similar(u"习近平"): print element[0], element[1] ....: 胡锦涛 0.857445538044 江泽民 0.826745271683 温家宝 0.763680458069 邓小平 0.746642589569 李克强 0.735500335693 朱镕基 0.726185798645 赵紫阳 0.715296566486 胡耀邦 0.699508190155 曾庆红 0.663562893867 王岐山 0.662009716034 In [16]: for element in model.most_similar(u"计算机"): print element[0], element[1] ....: 电子计算机 0.747832536697 计算器 0.727036595345 计算机网络 0.717607796192 集成电路 0.710234284401 图形学 0.708823502064 计算机技术 0.701996028423 软件工程 0.700498580933 信号处理 0.699350774288 人工智能 0.694405376911 计算机科学 0.692531824112 In [17]: for element in model.most_similar(u"防火墙"): print element[0], element[1] ....: 路由器 0.7505402565 代理服务器 0.746256113052 客户端 0.744421362877 应用程序 0.738845169544 网关 0.737333416939 用户端 0.735817670822 管理器 0.734896183014 恶意软件 0.731451034546 应用层 0.727778911591 固件 0.725310862064 |
You can browse other Chinese word2vec model examples on Chinese Word Similarity, just enjoy it.
Posted by Text Mining
Comments
Training a Chinese Wikipedia Word2Vec Model by Gensim and Jieba — No Comments