-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumper.py
152 lines (151 loc) · 5.05 KB
/
dumper.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from pymongo import MongoClient
import config,urllib2
client = MongoClient(config.getConfig("mongo_url"))
db = client[config.getConfig("mongo_db")]
dic={}
redirects_dic={}
data={}
last_first=None
def createKey(key):
return key.replace(".","#dot#")
def searchKey(key):
key=key.replace("_"," ").replace("http://dbpedia.org/resource/","").replace("(","").replace(")","").lower()
return key
def insert(col,first,second,third):
global data,dic,last_first
first=urllib2.unquote(first)
second=urllib2.unquote(second)
third=urllib2.unquote(third)
if col=="homepages_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
collection.insert({"_id":createKey(first),"website":third,"search":searchKey(first)},w=0)
elif col=="disambiguations_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"to":data[last_first]["to"],"search":searchKey(last_first)},w=0)
del data[last_first]
last_first=first
if first not in data:
data[first]={}
data[first]["to"]=[]
data[first]["to"].append(third)
elif col=="article-categories_en":
collection = db[col]
if col not in dic:
collection.drop()
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"categories":data[last_first]["categories"]},w=0)
del data[last_first]
last_first=first
if first not in data:
data[first]={}
data[first]["categories"]=[]
data[first]["categories"].append(third)
elif col=="external-links_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"to":data[last_first]["to"],"search":searchKey(last_first)},w=0)
del data[last_first]
last_first=first
if first not in data:
data[first]={}
data[first]["to"]=[]
data[first]["to"].append(third)
elif col=="images_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"images":data[last_first]["to"],"search":searchKey(last_first),"hasType":second},w=0)
del data[last_first]
last_first=first
if first not in data:
data[first]={}
data[first]["to"]=[]
data[first]["to"].append(third)
elif col=="infobox-properties_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"box":data[last_first]["box"],"search":searchKey(last_first)},w=0)
del data[last_first]
last_first=first
if first not in data:
data[first]={}
data[first]["box"]={}
data[first]["box"][second.replace("http://dbpedia.org/property/","").replace(".","#dot#")]=third.replace("http://dbpedia.org/property/","").replace(".","#dot#")
elif col=="short-abstracts_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
collection.insert({"_id":createKey(first),"summary":third,"search":searchKey(first)},w=0)
elif col=="redirects_en":
collection = db[col]
if col not in dic:
collection.drop()
dic[col]=True
if third in redirects_dic:
redirects_dic[third].append(first)
else:
redirects_dic[third]=[]
redirects_dic[third].append(first)
#collection.insert({"from":createKey(first),"to":third},w=0)
elif col=="instance-types-transitive_en":
collection = db[col]
if col not in dic:
collection.drop()
collection.create_index([('search', "text")], default_language='english')
dic[col]=True
last_first=first
if first!=last_first:
collection.insert({"_id":createKey(last_first),"w3c":data[last_first]["w3c"],"sc":data[last_first]["sc"],"dbc":data[last_first]["dbc"],"wikiId":data[last_first]["wikiId"],"search":searchKey(last_first)},w=0)
del data[last_first]
last_first=first
#print first
if first not in data:
data[first]={}
data[first]["dbc"]=[]
data[first]["sc"]=[]
data[first]["w3c"]=[]
data[first]["wikiId"]=""
if "www.wikidata.org/entity/" in third:
#wikidata id
data[first]["wikiId"]=third
elif "dbpedia.org/ontology/" in third:
data[first]["dbc"].append(third)
elif "http://www.w3.org/" in third:
data[first]["w3c"].append(third)
elif "http://schema.org" in third:
data[first]["sc"].append(third)
def dumpRedirects():
"""
The redirects file does not have same articles in order.
"""
collection = db["redirects_en"]
for key in redirects_dic:
collection.insert({"from":redirects_dic[key],"_id":createKey(key)},w=0)