SlideShare a Scribd company logo
1 of 33
Download to read offline
Honey's Data Dinner #1
Word2Vec
2016 總統大選新聞
Beehive Data Group
Who am I
施旭峰 Jeffy Shih
bee.me@ninthday.info
數位足跡探索計畫
傳播學院+資科系
水火計畫
傳播學院+資科系
智慧型媒體實驗室
資科系
Outline
1. 基本觀念
2. word2vec
3. 動手玩玩總統大選新聞資料
a. 語料來源
b. 資料處理
c. 中文斷詞
d. word2vec 訓練
基本觀念
Basic
Word 詞
Sentence 句子
Document 文件
Context 上下文(語境、前後關係)
Semantic 語義
Context
句子一:*** 蘋果****
句子二: **** 蘋果 ****
Context
句子一:樹上 的 蘋果 又 大 又 紅
句子二:ipad iphone 是 蘋果 公司 的 電子產品
詞彙搭配不同 上下語句 關係 , 會有不同意思
Semantic
用 banana 搜尋這三份文件 ...
1. Yellow banana peels.
2. A banana is a long yellow fruit.
3. This mystery fruit is long and yellow and has a peel.
Semantic
善用 context 的資訊,能改善搜尋結果(或尋找同義字)
1. Yellow banana peels.
2. A banana is a long yellow fruit.
3. This mystery fruit is long and yellow and has a peel.
Mathematical model
It is the New York City driver’s public shame — a sentence of solitary
front-seat confinement levied against those for whom subways, buses and
taxis are insufficient.For at least 90 minutes each week, residents move
their vehicles from their curbside berths, slide into formation behind a row
of double-parked neighbors and moor together in a singular urban traffic
jam, beholden to a hulking contraption whose distinguishing feature
appears to be this: It swirls plastic bags and cigarette stubs briefly before
returning them to the earth.But the ignominy of alternate-side-of-the-street
parking, which allows city workers to clean roadways without the
obstruction of parked cars, could soon be eased. A bill that will have a
hearing before the City Council on Monday would allow drivers to return to
parking spaces once the street sweepers pass, causing a potentially
significant reduction in wait times for those doomed to mornings in their
cars.Councilman Ydanis Rodriguez, a Democrat from Manhattan and the
bill’s sponsor, said the legislation would prevent accidents by reducing the
duration of double-parking; help the environment, with fewer cars idling or
driving in search of spaces; and save New Yorkers “millions of dollars” in
lost time.
數學
模型
Context, n = 2
word2vec
Word2Vec
word2vec 是 Google 2013 年年中釋出基於 Apache 2.0 的開源專案
。word2vec 是把每個詞本身用一個多維向量來表示,把詞投影到一
個向量空間裡。
word2vec 透過訓練,可以將文本的內容簡化為 K 維向量空間中的
向量運算,向量空間上的相似度,可以用來表示文本語義上的相似
度。
Word2Vec
語言學的規律:
C('KING') - C('QUEEN') ≈ C('MAN') - C('WOMAN')
C('KING') - C('MAN') + C('WOMAN') ≈ C('QUEEN')
組合運算:
vector('Paris') - vector('France') + vector('Italy') ≈ vector('Rome')
vector('King') - vector('Man') + vector('Woman') ≈ vector('Queen')
KING
QUEEN
WOMAN
MAN
Word2Vec - Reference
Paper
1. Tomas Mikolov, Kai Chen, Greg Corrado, and Jeffrey Dean. Efficient Estimation of
Word Representations in Vector Space. In Proceedings of Workshop at ICLR, 2013.
2. Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, and Jeffrey Dean. Distributed
Representations of Words and Phrases and their Compositionality. In Proceedings of
NIPS, 2013.
3. Tomas Mikolov, Wen-tau Yih, and Geoffrey Zweig. Linguistic Regularities in Continuous
Space Word Representations. In Proceedings of NAACL HLT, 2013.
Word2Vec - Reference
Other
1. Deep Learning实战之word2vec
http://techblog.youdao.com/?p=915
2. 用中文資料測試 word2vec
http://city.shaform.com/blog/2014/11/04/word2vec.html
3. 用中文把玩Google开源的Deep-Learning项目word2vec
http://www.cnblogs.com/wowarsenal/p/3293586.html
動手玩玩總統大選新聞資料
語料來源
水火計畫新聞收集平臺
Elasticsearch@FloodFire + cURL
關鍵字:朱立倫、蔡英文、宋楚瑜、總統大選、陳建仁、王如玄、徐欣瑩
格式:JSON
時間:選前 100 天開始
from:1444233600000(2015-10-08)
to:1449244740000(2015-12-04)
筆數:15,998
資料處理
<a href="/search/記者邱明玉/台北報導">記者邱明玉/台北報導</a>nrn rn 根據媒體報導,「馬習
會」是總統府資政金溥聰操盤的畢業代表作,促成美中台聯手框住民進黨總統參選人蔡英文。對此,民進
黨立委管碧玲今(11)日表示,既然大家都在猜幕後黑手,就表示是黑幕、黑箱作業,才要讓大家到處在
猜,所有的黑箱到最後都會猜到是金溥聰,所以金溥聰已經是一個符號,代表的就是黑箱。nrn民進黨立
院黨團書記長陳亭妃表示
…略
所以很多爆料聽聽就好。nrnrn rn rn rnrn rn rn rn rn rn 更多NOWnews新聞都在行動
APP上:nrnt<a href="https://goo.gl/AWVNkD">》》Android APP請點這裡下載</a> <a
href="https://goo.gl/M49ADW">》》iOS APP請點這裡下載</a> <a
href="http://legacy.nownews.com/events/adtips/mobile_app/NowNews_Mobile.apk">》》大陸地區請點此
直接下載APK安裝</a>
資料處理
1. 解析 JSON,抽取訓練用的新聞內文資料
2. 正規表示式(regular expression)去除內文中的
a. 換行符號
b. 連續空白
c. 網址(http)
3. 一篇文章一行
中文斷詞
使用結巴(Jieba)中文斷詞
結巴參見:https://github.com/fxsjy/jieba
繁體中文字庫:dict.txt.big
使用者自訂字庫:userdict.txt
朱立倫 20 nr
蔡英文 20 nr
宋楚瑜 20 nr
柯文哲 20 nr
馬英九 20 nr
王金平 20 nr
國民黨 20 nt
民進黨 20 nt
親民黨 20 nt
陳建仁 20 nr
王如玄 20 nr
徐欣瑩 20 nr
副總統 5 n
資料參考:如何使用 JIEBA 結巴中文分詞程式
http://blog.fukuball.com/ru-he-shi-yong-jieba-jie-ba-zhong-wen-fen-ci-cheng-shi/
中文斷詞
斷詞前內容
斷詞後內容
中時電子報許劍虹/台北報導美國總統歐巴馬於11月22日出席在吉隆坡舉
行的東協加一活動時
中時⎵電子報⎵許劍虹⎵台北⎵報導⎵美國⎵總統⎵歐巴馬⎵於⎵11⎵月⎵22⎵日⎵
出席⎵在⎵吉隆坡⎵舉行⎵的⎵東協加⎵一⎵活動⎵時
Word2Vec
Google word2vec
Code & paper: https://code.google.com/p/word2vec/
Gensim word2vec (Python)
Gensim: https://radimrehurek.com/gensim/index.html
Gensim word2vec: https://radimrehurek.com/gensim/models/word2vec.html
Word2Vec 訓練
我的訓練環境
OS: Ubuntu Server 14.04.3 LTS (64-bit)
CPU: Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
RAM: 8GB
Word2Vec 訓練詞向量
Google word2vec
1. 下載 word2vec 原始程式
https://code.google.com/p/word2vec/
2. make 編譯 word2vec
3. 以斷好詞的文本做訓練
./word2vec -train 2016PE_news_cuted.txt -output vectors.bin -cbow 0 -size 200 -window 10 -
negative 5 -hs 0 -sample 1e-4 -threads 8 -binary 1 -iter 20 -min-count 1vectors.bin 是訓練後詞的向量文件
結果與測試
distance(詞的相似性)
詞和詞之間的距離,把詞看成向量空間中的一個點,distance是向量空間中點與點
之間的距離。
./distance vectors.bin
Enter word or sentence (EXIT to break): 蔡英文
Word: 蔡英文 Position in vocabulary: 9
Word Cosine distance
------------------------------------------------------------------------
民進黨
0.831650
蔡 0.742174
參選人
0.706721
總統
0.692282
進步黨
0.604589
結果與測試
線性關係推理
./word-analogy vectors.binEnter three words (EXIT to break): 蔡英文 陳建仁 宋楚瑜
Word: 蔡英文 Position in vocabulary: 9
Word: 陳建仁 Position in vocabulary: 147
Word: 宋楚瑜 Position in vocabulary: 109
Word Distance
------------------------------------------------------------------------
徐欣瑩 0.605165
親民黨 0.565923
宋瑩配 0.564386
昨也用 0.536368
Word2Vec
可調整的參數
./word2vec -train 2016PE_news_cuted.txt -output vectors.bin -cbow 0 -size 200 -window 10 -
negative 5 -hs 0 -sample 1e-4 -threads 8 -binary 1 -iter 20 -min-count 1
-cbow :使用 CBOW 方法
-size :向量的維度數量
-window :上下文的範圍大小
-negative :負例的數量
-hs :是否要採用 Hierarchical
Softmax
-sample :高詞頻的閾值
-min-count :要截斷的低詞頻閾值
Demo
Word2Vec
還可以做什麼?
1. 將詞分群並依照詞群排序
2. 短句分析
嗝~Q & A
https://goo.gl/ZFdEpf

More Related Content

Viewers also liked

Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)
Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)
Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)beehivedata
 
大數據的獲利模式
大數據的獲利模式大數據的獲利模式
大數據的獲利模式Chang Chiao Hui
 
Minesweeper專題報告
Minesweeper專題報告Minesweeper專題報告
Minesweeper專題報告?? ?
 
平台經濟模式心得分享
平台經濟模式心得分享平台經濟模式心得分享
平台經濟模式心得分享建夆 陳
 
蒙地卡羅模擬與志願運算
蒙地卡羅模擬與志願運算蒙地卡羅模擬與志願運算
蒙地卡羅模擬與志願運算Yuan CHAO
 
Building a Data-Driven Culture
Building a Data-Driven CultureBuilding a Data-Driven Culture
Building a Data-Driven CultureLucas Neo
 
Operating and supporting HBase Clusters
Operating and supporting HBase ClustersOperating and supporting HBase Clusters
Operating and supporting HBase Clustersenissoz
 
社交媒體研究與鉅量資料分析
社交媒體研究與鉅量資料分析社交媒體研究與鉅量資料分析
社交媒體研究與鉅量資料分析Yuchung Cheng
 
Lecture on Data Science in a Data-Driven Culture
Lecture on Data Science in a Data-Driven Culture Lecture on Data Science in a Data-Driven Culture
Lecture on Data Science in a Data-Driven Culture Johan Himberg
 
Building a data-driven culture
Building a data-driven cultureBuilding a data-driven culture
Building a data-driven cultureOlof Hoverfält
 
Dubbo - 高性能分布式 RPC 框架
Dubbo - 高性能分布式 RPC 框架Dubbo - 高性能分布式 RPC 框架
Dubbo - 高性能分布式 RPC 框架Chen Wu
 
How big data tranform your business? Data Science Thailand Meet up #6
How big data tranform your business? Data Science Thailand Meet up #6How big data tranform your business? Data Science Thailand Meet up #6
How big data tranform your business? Data Science Thailand Meet up #6Data Science Thailand
 
Paxos building-reliable-system
Paxos building-reliable-systemPaxos building-reliable-system
Paxos building-reliable-systemYanpo Zhang
 
How to reach a Data Driven culture
How to reach a Data Driven cultureHow to reach a Data Driven culture
How to reach a Data Driven cultureMark Beekman
 
Cloudera 助力台灣大數據產業的發展
Cloudera 助力台灣大數據產業的發展Cloudera 助力台灣大數據產業的發展
Cloudera 助力台灣大數據產業的發展Etu Solution
 
揭開台灣音樂審查黑箱
揭開台灣音樂審查黑箱揭開台灣音樂審查黑箱
揭開台灣音樂審查黑箱季康 彭
 

Viewers also liked (18)

Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)
Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)
Honey's Data Dinner#13 跨領域專案開發經驗談(User Story Mapping)
 
大數據的獲利模式
大數據的獲利模式大數據的獲利模式
大數據的獲利模式
 
Minesweeper專題報告
Minesweeper專題報告Minesweeper專題報告
Minesweeper專題報告
 
平台經濟模式心得分享
平台經濟模式心得分享平台經濟模式心得分享
平台經濟模式心得分享
 
蒙地卡羅模擬與志願運算
蒙地卡羅模擬與志願運算蒙地卡羅模擬與志願運算
蒙地卡羅模擬與志願運算
 
20160419 CCASA
20160419 CCASA20160419 CCASA
20160419 CCASA
 
Building a Data-Driven Culture
Building a Data-Driven CultureBuilding a Data-Driven Culture
Building a Data-Driven Culture
 
Operating and supporting HBase Clusters
Operating and supporting HBase ClustersOperating and supporting HBase Clusters
Operating and supporting HBase Clusters
 
社交媒體研究與鉅量資料分析
社交媒體研究與鉅量資料分析社交媒體研究與鉅量資料分析
社交媒體研究與鉅量資料分析
 
Lecture on Data Science in a Data-Driven Culture
Lecture on Data Science in a Data-Driven Culture Lecture on Data Science in a Data-Driven Culture
Lecture on Data Science in a Data-Driven Culture
 
141119
141119141119
141119
 
Building a data-driven culture
Building a data-driven cultureBuilding a data-driven culture
Building a data-driven culture
 
Dubbo - 高性能分布式 RPC 框架
Dubbo - 高性能分布式 RPC 框架Dubbo - 高性能分布式 RPC 框架
Dubbo - 高性能分布式 RPC 框架
 
How big data tranform your business? Data Science Thailand Meet up #6
How big data tranform your business? Data Science Thailand Meet up #6How big data tranform your business? Data Science Thailand Meet up #6
How big data tranform your business? Data Science Thailand Meet up #6
 
Paxos building-reliable-system
Paxos building-reliable-systemPaxos building-reliable-system
Paxos building-reliable-system
 
How to reach a Data Driven culture
How to reach a Data Driven cultureHow to reach a Data Driven culture
How to reach a Data Driven culture
 
Cloudera 助力台灣大數據產業的發展
Cloudera 助力台灣大數據產業的發展Cloudera 助力台灣大數據產業的發展
Cloudera 助力台灣大數據產業的發展
 
揭開台灣音樂審查黑箱
揭開台灣音樂審查黑箱揭開台灣音樂審查黑箱
揭開台灣音樂審查黑箱
 

Honey's Data Dinner#1 word2vec 2016總統大選新聞