-
Notifications
You must be signed in to change notification settings - Fork 4
/
wikipedias.py
47 lines (41 loc) · 1.04 KB
/
wikipedias.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
47
# gets all Wikipedias, their language codes, and languages
# needs kb
import gzip
count = 0
linecount = 0
wikipedias = set()
codes = {}
languages = {}
output = open('data/wikipedias.txt', 'w')
for line in gzip.open('kb.txt.gz') :
linecount += 1
if (linecount % 1000000) == 0 : print linecount / 1000000
if line.startswith('#') :
output.write(line)
continue
if line.startswith(' ') : continue
parts = line.split(' ')
if len(parts) != 4 : continue
s = parts[0]
p = parts[1]
o = parts[2]
if p == 'P31' and o == 'Q52' :
print 'Wikipedia', s
wikipedias.add(s)
if p == 'P424' :
print 'Code', s, o
codes[s] = o[1:-1]
if p == 'P407' :
#print 'Language', s, o
languages[s] = o
print linecount, 'lines'
for wikipedia in wikipedias :
language = languages[wikipedia] if wikipedia in languages else ''
code = codes[wikipedia] if wikipedia in codes else ''
language = ' ' + language
code = ' ' + code
output.write(wikipedia + code + language + "\n")
count += 1
output.write('# ' + str(count) + ' entries')
print count, 'results'
output.close()