summaryrefslogtreecommitdiff
path: root/schema-manipulation/get_type.py
blob: af4554e16922c94e4c7006d22bfc8745f0d80758 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# TODO: copy /freebase/type_hints junk

from pprint import pprint
import random

from type_creation import create_type, create_property, delegate_property, reciprocate_property

from freebase.api  import HTTPMetawebSession, MetawebError

s = HTTPMetawebSession("http://sandbox-freebase.com")
s.login("username", "password")

import time

def create_type_dependencies(s, base_id=None, type_id=None):
    
    if base_id:
        q = {"id" : base_id, "/type/domain/types" : [{"id" : None}] }
        results = s.mqlread(q)
        types = map(lambda x: x["id"], results["/type/domain/types"])
        #pprint(types)
    
    if type_id:
        types = [type_id]
        base_id = "/".join(type_id.split("/")[:-1])
        print base_id
    
    if not base_id and not type_id:
        raise Exception("You need to supply either a base_id or a type_id")
    
    graph = {}
    to_update = set(types)
    done = set()
    while len(to_update) > 0:
        new = to_update.pop()
        graph[new] = get_needed(s, new)
        [to_update.add(b) for b in graph[new]["related"] if b not in done]
        done.update(graph[new]["related"])
    
    # find distinct subgraphs (looking at needs)
    subgraphs = []
    unknown = set(graph.keys())
    while len(unknown) > 0:
        visited = set()
        to_visit = set([list(unknown)[0]])
        while len(to_visit) > 0:
            new = to_visit.pop()
            visited.add(new)
            try:
                unknown.remove(new)
            except KeyError:
                pass
            [to_visit.add(b) for b in graph[new]["related"] if b not in visited]
        subgraphs.append(list(visited))
    #pprint(subgraphs)
    
    pprint(graph)
    return
    
    # create type dependencies
    typegraph = {}
    for tid, idres in graph.items():
        typegraph[tid] = idres["needs"]
    
    least_needy_type = map(lambda (name, x): (len(x), name), typegraph.items())
    least_needy_type.sort()
    
    types_to_create = create_what(least_needy_type, typegraph)
    
    # create property dependencies
    propgraph = {}
    proptotype = {}
    for tid, idres in graph.items():
        for prop in idres["properties"]:
            propgraph[prop["id"]] = prop["needs"]
            proptotype[prop["id"]] = tid
    least_needy = map(lambda (name, x): (len(x), name), propgraph.items())
    least_needy.sort()
    
    props_to_create = create_what(least_needy, propgraph)
    
    domainname = "awesome" + str(int(random.random() * 1e10))
    print "\ndomainid", domainname
    print "--------------------------"
    dn = s.create_private_domain(domainname, domainname + "!")
    domain_id = dn.domain_id
    
    better_id = s.mqlread({"id" : domain_id, "a:id" : None})
    domain_id = better_id["a:id"]
    
    for type in types_to_create:
        print type
        key = ""
        if len(graph[type]["key"]) == 1:
            key = graph[type]["key"][0]["value"]
        else:
            expectedname = graph[type]["id"].split("/")[-1]
            if base_id:
                for group in graph[type]["key"]:
                    if group["namespace"] == base_id:
                        key = group["value"]
                        continue
            if key is None:
                key = expectedname
        tip = None
        if graph[type]["/freebase/documented_object/tip"]:
            tip = graph[type]["/freebase/documented_object/tip"]["value"]
        create_type(s, graph[type]["name"]["value"], key, domain_id,
            included=map(lambda x: convert_name(x["id"], base_id, domain_id), graph[type]["/freebase/type_hints/included_types"]),
            cvt=graph[type]["/freebase/type_hints/mediator"],
            enum=graph[type]["/freebase/type_hints/enumeration"],
            tip=tip)
    
    print "--------------------------"
    
    for prop in props_to_create:
        info = graph[proptotype[prop]]["properties"]
        for i in info:
            if i["id"] == prop: 
                schema = convert_name(proptotype[prop], base_id, domain_id)
                print prop
                expected = None
                if i["expected_type"]:
                    expected = convert_name(i["expected_type"]["id"], base_id, domain_id)
                for k in i["key"]:
                    if k.namespace == proptotype[prop]:
                        key = k.value
                if i["/freebase/documented_object/tip"]:
                    tip = graph[type]["/freebase/documented_object/tip"]
                    
                if i['master_property']:
                    reciprocate_property(s, i["name"], key, convert_name(i["master_property"]["id"], base_id, domain_id),
                        i["unique"], disambig=i["/freebase/property_hints/disambiguator"], tip=tip)
                elif i['delegated']:
                    delegate_property(s, convert_name(i['delegated']['id'], base_id, domain_id), schema,
                        expected=expected, tip=tip)
                else:
                    create_property(s, i["name"], key, schema, expected, i["unique"], 
                        disambig=i["/freebase/property_hints/disambiguator"], tip=tip) 
                        
                
        
    
    print "\ndomain id was", domainname
    

def convert_name(old_name, operating_base, new_base):
    if old_name.startswith(operating_base):
        return new_base + old_name.replace(operating_base, "", 1)
    else:
        return old_name
    
def create_what(deps, graph):
    create_list = []
    while len(deps) > 0:
        neediness, id = deps.pop(0)
        if neediness == 0:
            create_list.append(id)
            continue
        else:
            work = True
            for req in graph[id]:
                if req not in create_list:
                    work = False
                    continue
            if work:
                create_list.append(id)
            else:
                deps.append((neediness, id))
    return create_list        
       


def get_needed(s, type_id):
    #print type_id
    q = {
        "type" : "/type/type",
        "id" : type_id,
        "domain" : [{}],
        "expected_by" : [{}],
        "key" : [{"namespace" : None, "value" : None}],
        "name" : {"value" : None, "lang" : "/lang/en", "optional":True},
        "/freebase/type_hints/included_types" : [{}],
        "/freebase/type_hints/mediator" : None,
        "/freebase/type_hints/enumeration" : None,
        "/freebase/documented_object/tip" : {"value" : None, "limit":1, "optional":True},
        "properties" : [{
            "optional" : True,
            "delegated" : {},
            "enumeration" : None, 
            "expected_type" : {}, 
            "id" : None,
            "key" : [{
                "namespace" : None, 
                "value" : None
            }],
            #"link" : [{}], 
            "master_property" : {},
            "name" : {"value" : None, "lang" : "/lang/en", "optional":True}, 
            "reverse_property" : {}, 
            "schema" : {"id" : None, "name" : None},
            "unique" : None, 
            "unit" : None,
            "/freebase/documented_object/tip" : {"value" : None, "limit":1, "optional" : True},
            "/freebase/property_hints/disambiguator" : None
        }]  
    }
    
    r = s.mqlread(q)
    properties = r.properties
    
    #print "************************************************" + type_id
    #pprint(r)
    #print "************************************************" + type_id
    # hopefully there's only one domain
    fathers = map(lambda x: x["id"], r["domain"])
   
    brothers = set()
    included_types = map(lambda x: x["id"], r["/freebase/type_hints/included_types"])
    brothers.update(included_types)
    for prop in properties:
        if prop["expected_type"]:
            brothers.add(prop["expected_type"]["id"])
        
    
    family = return_relevant(brothers, fathers)
    needed = return_relevant(included_types, fathers)
    
    # get property information
    properties = r["properties"]
    for prop in properties:
        needs = set()
        if prop["master_property"]:
            needs.add(prop["master_property"]["id"])
        if prop["delegated"]:
            needs.add(prop["delegated"]["id"])
        
        prop["needs"] = return_relevant(needs, fathers)
    
    answer = r
    answer.update(related=family, needs=needed, properties=properties)
    
    return answer
    

def return_relevant(start_list, fathers):
    final = []
    for item in start_list:
        indomain = False
        for father in fathers:
            if item.startswith(father):
                indomain = True
                continue
        if indomain:
            final.append(item)
    return final

if __name__ == '__main__':
    
    create_type_dependencies(s, base_id="/people")