[繁體中文/NLP] 從word2vec到 情感分析

完整版請至GitHub Page

本專案主旨為台灣繁體中文建置自然語言理解之服務,以下介紹大致內容:

實作主題 :
1. 為jieba匯入台灣繁體區之字詞典並添加台灣區常見口語詞彙 – jieba for tradition Chinese
2. 透過網路爬蟲取得文字來源,包含正負面評價、報章雜誌等 – Website Crawler
3. 修改word2vec以適用中文訓練 – word2vec
4. 情感分析之實作 – sentiment analysis

主要使用的有 :
> TensorFlow
> jieba
> jieba.analysis
> requests

為jieba匯入台灣繁體區之字詞典並添加台灣區常見口語詞彙

這個主題旨在是在匯入台灣當地字詞,並調用jieba.lcut(sentence)、jieba.analyse.tfidf()和jieba.analyse.textrank()
0. 調用服務實例

> NLPLab/jiebaService.py

以下紀錄重點工作;
1. 調整預設自設字典 並 匯入台灣字詞

> 在 NLPLab/jieba_zn/jieba/__init__.py 調整 "DEFAULT_DICT_NAME" 變數
> 在 NLPLab/jieba_zn/jieba/__init__.py 調用 "dt.load_userdict(get_module_res('tw.dict.all'))" 方法

2. 調整路徑: 由於最初下載時部分程式並為相對路徑,故稍作修正。
3. 结巴中文分词來源: https://github.com/fxsjy/jieba

temp

透過網路爬蟲取得文字來源,包含正負面評價、報章雜誌等 – Website Crawler

Github: https://github.com/Chunshan-Theta/NLPLab/tree/master/Crawler

這個主題旨在是在蒐集訓練所需要的資料來源、分類與轉成繁體。
1. 於booking.com取得約34萬則正負面評價: 這個部分主要使用 selenium 進行爬蟲

> 先使用 NLPLab/Crawler/CrawlerToBooking/crawler_selenium-GetUrl.py 蒐集台灣各飯店的id
> 在使用 NLPLab/Crawler/CrawlerToBooking/crawler_selenium-GetContent.py 分類整理正負面評價

2. 於scitechvista.nat.gov.tw取得2000多篇科學專刊: 這個部分主要使用 selenium 進行爬蟲

> 先使用 NLPLab/Crawler/CrawlerForScitechvista/crawler_selenium-GetUrl.py 蒐集科學文章的各個連結
> 在使用 NLPLab/Crawler/CrawlerForScitechvista/crawler_selenium-GetContent.py 爬下內容儲存

3. 於wiki取得中文文章約70000篇 – 參考來源 (https://github.com/Alex-CHUN-YU/Word2vec)
主要以 pages-articles.xml.bz2 結尾之檔案類型,這邊使用 zhwiki-latest-pages-articles.xml.bz2。

> 維基資料集:
https://zh.wikipedia.org/wiki/Wikipedia:%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%8B%E8%BD%BD

> zhwiki-latest-pages-articles.xml.bz2 下載網址:
https://drive.google.com/file/d/0B4rlWa2S_JMBUmlMSG5IRVRMbnc/view?usp=sharing

> 程式參考網址:
https://radimrehurek.com/gensim/corpora/wikicorpus.html
https://radimrehurek.com/gensim/models/word2vec.html

4. 轉簡體為繁體: 這個部分主要使用 NLPLab/nstools/ 進行轉換

> 工具來源: https://github.com/skydark/nstools

 

 

 


Word2vec

GitHub: https://github.com/Chunshan-Theta/NLPLab/tree/master/word2vec
temp-1.png
1. loading the train data
the main step:
* clear special character:only chinese
* simplified to traditional (../nstools/)

> loading stop words ( word2vec/stop_words.txt.py )
> loading training article ( word2vec/wiki/ or word2vec/TextForTrain/ )

2. Build the dictionary and replace rare words with UNKNOWWORD token.
the main step:
* Setting the size of the word set for the training model

> Build the dictionary
> rare words processed
> using function: collections.Counter().most_common()

3. Function to generate a training batch for the skip-gram model.
4. Build and train a skip-gram model.

> Loss: tf.nn.nce_loss()
> Optimizer: tf.train.AdamOptimizer(learning_rate=1.0).minimize()

5. Begin training

> training stage
> TensorBoard (will output to word2vec/TB/)
> output to Json txt file :result_Json

GitHub: https://github.com/Chunshan-Theta/NLPLab/tree/master/sentiment/py3/zn

使用word2vec為向量基礎,透過旅館業正負面評論資料34萬筆來訓練評論正反面情緒預測。

訓練內容:
1.讀取word2vec 向量 (字詞維度300)
2.讀取正負面資訊
3.建築lstm神經網路
4.輸出model

reference:
> https://github.com/adeshpande3/LSTM-Sentiment-Analysis

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。