honey's data dinner#1 word2vec 2016總統大選新聞

33
Honey's Data Dinner #1 Word2Vec 2016 總統大選新聞 Beehive Data Group

Upload: beehivedata

Post on 07-Apr-2017

205 views

Category:

Data & Analytics


3 download

TRANSCRIPT

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

Honey's Data Dinner #1Word2Vec

2016 總統大選新聞

Beehive Data Group

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

Who am I

施旭峰 Jeffy Shih

[email protected]

數位足跡探索計畫傳播學院+資科系

水火計畫傳播學院+資科系

智慧型媒體實驗室資科系

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

Outline

1.基本觀念

2. word2vec

3.動手玩玩總統大選新聞資料

a. 語料來源

b. 資料處理

c. 中文斷詞

d. word2vec 訓練

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

基本觀念

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

Basic

Word 詞

Sentence 句子

Document 文件

Context 上下文(語境、前後關係)

Semantic 語義

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

Context

句子一:***蘋果****

句子二:****蘋果****

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

Context

句子一:樹上的蘋果又大又紅

句子二:ipad iphone 是蘋果公司的電子產品

詞彙搭配不同上下語句關係 , 會有不同意思

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

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.

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

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.

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

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.

數學模型

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

Context, n = 2

Page 12: Honey's Data Dinner#1 word2vec 2016總統大選新聞
Page 13: Honey's Data Dinner#1 word2vec 2016總統大選新聞
Page 14: Honey's Data Dinner#1 word2vec 2016總統大選新聞

word2vec

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

Word2Vec

word2vec 是 Google 2013 年年中釋出基於 Apache 2.0 的開源專案

。word2vec 是把每個詞本身用一個多維向量來表示,把詞投影到一

個向量空間裡。

word2vec 透過訓練,可以將文本的內容簡化為 K 維向量空間中的

向量運算,向量空間上的相似度,可以用來表示文本語義上的相似

度。

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

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

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

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.

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

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

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

動手玩玩總統大選新聞資料

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

語料來源

水火計畫新聞收集平臺

Elasticsearch@FloodFire + cURL

關鍵字:朱立倫、蔡英文、宋楚瑜、總統大選、陳建仁、王如玄、徐欣瑩

格式:JSON

時間:選前 100 天開始

from:1444233600000(2015-10-08)

to:1449244740000(2015-12-04)

筆數:15,998

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

資料處理

<a href=\"/search/記者邱明玉/台北報導\">記者邱明玉/台北報導</a>\n\r\n \r\n 根據媒體報導,「馬習

會」是總統府資政金溥聰操盤的畢業代表作,促成美中台聯手框住民進黨總統參選人蔡英文。對此,民進

黨立委管碧玲今(11)日表示,既然大家都在猜幕後黑手,就表示是黑幕、黑箱作業,才要讓大家到處在

猜,所有的黑箱到最後都會猜到是金溥聰,所以金溥聰已經是一個符號,代表的就是黑箱。\n\r\n民進黨立

院黨團書記長陳亭妃表示

…略

所以很多爆料聽聽就好。\n\r\n\r\n \r\n \r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n 更多NOWnews新聞都在行動

APP上:\n\r\n\t<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>

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

資料處理

1.解析 JSON,抽取訓練用的新聞內文資料

2.正規表示式(regular expression)去除內文中的

a. 換行符號

b. 連續空白

c. 網址(http)

3.一篇文章一行

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

中文斷詞

使用結巴(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/

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

中文斷詞

斷詞前內容

斷詞後內容

中時電子報許劍虹/台北報導美國總統歐巴馬於11月22日出席在吉隆坡舉行的東協加一活動時

中時⎵電子報⎵許劍虹⎵台北⎵報導⎵美國⎵總統⎵歐巴馬⎵於⎵11⎵月⎵22⎵日⎵出席⎵在⎵吉隆坡⎵舉行⎵的⎵東協加⎵一⎵活動⎵時

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

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

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

Word2Vec 訓練

我的訓練環境

OS: Ubuntu Server 14.04.3 LTS (64-bit)

CPU: Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz

RAM: 8GB

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

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 是訓練後詞的向量文件

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

結果與測試

distance(詞的相似性)

詞和詞之間的距離,把詞看成向量空間中的一個點,distance是向量空間中點與點

之間的距離。./distance vectors.bin

Enter word or sentence (EXIT to break): 蔡英文

Word: 蔡英文 Position in vocabulary: 9Word Cosine distance

------------------------------------------------------------------------民進黨

0.831650蔡 0.742174

參選人0.706721

總統0.692282

進步黨0.604589

日由前

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

結果與測試

線性關係推理

./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

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

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 :要截斷的低詞頻閾值

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

Demo

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

Word2Vec

還可以做什麼?

1.將詞分群並依照詞群排序

2.短句分析

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

嗝~Q & Ahttps://goo.gl/ZFdEpf