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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/usr/bin/python3
import sys, re
import sqlite3
usageInfo = f"usage: {sys.argv[0]}\n"
usageInfo += "Reads Wikimedia enwiki data from enwiki/, along with node and name data\n"
usageInfo += "from a sqlite database, associates nodes with enwiki pages, and adds\n"
usageInfo += "alt-name and description information for those nodes.\n"
if len(sys.argv) > 1:
print(usageInfo, file=sys.stderr)
sys.exit(1)
enwikiDb = "enwiki/enwikiData.db"
dbFile = "data.db"
# Open dbs
enwikiCon = sqlite3.connect(enwikiDb)
enwikiCur = enwikiCon.cursor()
dbCon = sqlite3.connect(dbFile)
dbCur = dbCon.cursor()
# Find page id for each node name
nodeToPageId = {}
print("Getting node page-ids")
iterationNum = 0
for row in dbCur.execute("SELECT name from nodes"):
iterationNum += 1
if iterationNum % 1e4 == 0:
print("At iteration {}".format(iterationNum))
#
name = row[0]
row = enwikiCur.execute("SELECT id FROM pages where pages.title = ? COLLATE nocase", (name,)).fetchone()
if row != None:
nodeToPageId[name] = row[0]
# Resolve redirects
print("Resolving redirects")
redirectingNames = set()
iterationNum = 0
for (name, pageId) in nodeToPageId.items():
iterationNum += 1
if iterationNum % 1e4 == 0:
print("At iteration {}".format(iterationNum))
#
row = enwikiCur.execute("SELECT target_id FROM redirects where redirects.id = ?", (pageId,)).fetchone()
if row != None:
nodeToPageId[name] = row[0]
redirectingNames.add(name)
# Add descriptions for each node
print("Adding node description data")
dbCur.execute("CREATE TABLE descs (name TEXT PRIMARY KEY, desc TEXT, redirected INT)")
iterationNum = 0
for (name, pageId) in nodeToPageId.items():
iterationNum += 1
if iterationNum % 1e4 == 0:
print("At iteration {}".format(iterationNum))
#
row = enwikiCur.execute("SELECT desc FROM descs where descs.id = ?", (pageId,)).fetchone()
if row != None:
dbCur.execute("INSERT INTO descs VALUES (?, ?, ?)", (name, row[0], 1 if name in redirectingNames else 0))
# Close dbs
dbCon.commit()
dbCon.close()
enwikiCon.commit()
enwikiCon.close()
|