1. 程式人生 > >中文維基數據處理 - 1. 下載與清洗

中文維基數據處理 - 1. 下載與清洗

下載 open shell title -m 實體 code 選擇 html

1. 數據下載

一些重要的鏈接:

  1. 最新轉儲
    需要 zhwiki-latest-pages-articles.xml.bz2 這個文件
  2. 中文維基的頁面統計信息
    目前內容頁面數大約是 978K

2. 數據處理

選擇了 Gensim 這個主題工具包進行數據預處理。

2.1 xml 轉 json

scripts.segment_wiki

python -m gensim.scripts.segment_wiki -f zhwiki-latest-pages-articles.xml.bz2 | gzip > zhwiki-latest.json.gz

然後就轉換成了可被 Python 直接讀取的 json 文檔。

2.2 測試數據

from smart_open import smart_open
import json
x = 0

for line in smart_open(‘zhwiki-latest.json.gz‘):
     article = json.loads(line)

     print("Article title: %s" % article[‘title‘])
     for section_title, section_text in zip(article[‘section_titles‘], article[‘section_texts‘]):
         print("Section title: %s" % section_title)
         print("Section text: %s" % section_text)

     x += 1
     if x == 5:
         break

運行如上代碼可以輸出中文維基中的前 5 篇文檔。

2.3 分詞 / 命名實體識別 / 關系抽取

沒寫。

中文維基數據處理 - 1. 下載與清洗