-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikidata_normalize.py
45 lines (33 loc) · 1.1 KB
/
wikidata_normalize.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
#!~/data/anaconda3/bin/python
'''
Program: Processing Wikipedia Data for Word2Vec
Author: Abhishek Sharma
Last Modified: Nov 4, 2017
'''
import os
import sys
import logging
from gensim.corpora import WikiCorpus
class Wikidata(object):
def __init__(self):
program = os.path.basename(sys.argv[0])
self.logger = logging.getLogger(program)
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
logging.root.setLevel(level=logging.INFO)
self.logger.info("running %s" % ' '.join(sys.argv))
self.wiki_input_file = sys.argv[1]
self.wiki_output_file = sys.argv[2]
self.execute()
def execute(self):
i = 0
output_file_object = open(self.wiki_output_file, 'w')
wiki = WikiCorpus(self.wiki_input_file, lemmatize=False, dictionary={})
for text in wiki.get_texts():
output_file_object.write(bytes(' '.join(text), 'utf-8').decode('utf-8') + '\n')
i = i + 1
if (i % 1000 == 0):
self.logger.info("Saved " + str(i) + " articles")
output_file_object.close()
self.logger.info("Finished Saved " + str(i) + " articles")
if __name__ == '__main__':
Wikidata()