-
Notifications
You must be signed in to change notification settings - Fork 6
/
wiki.py
46 lines (37 loc) · 1.4 KB
/
wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import jieba
import logging
import math
import os
import requests
from gensim.corpora.wikicorpus import WikiCorpus
from tqdm import tqdm
def download_file(url, file):
r = requests.get(url, stream=True)
total_size = int(r.headers.get('content-length', 0))
block_size = 1024
wrote = 0
logging.info('Downloading %s to %s', url, file)
with open(file, 'wb') as f:
for data in tqdm(r.iter_content(block_size), total=math.ceil(total_size / block_size), unit='KB',
unit_scale=True):
wrote = wrote + len(data)
f.write(data)
logging.info('Done')
def download_wiki_dump(lang, path):
url = 'https://dumps.wikimedia.org/{lang}wiki/latest/{lang}wiki-latest-pages-articles-multistream.xml.bz2'
if not os.path.exists(path):
download_file(url.format(lang=lang), path)
else:
logging.info('%s exists, skip download', path)
class WikiSentences:
# reference: https://github.com/LasseRegin/gensim-word2vec-model/blob/master/train.py
def __init__(self, wiki_dump_path, lang):
logging.info('Parsing wiki corpus')
self.wiki = WikiCorpus(wiki_dump_path)
self.lang = lang
def __iter__(self):
for sentence in self.wiki.get_texts():
if self.lang == 'zh':
yield list(jieba.cut(''.join(sentence), cut_all=False))
else:
yield list(sentence)