-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelationFinder.py
50 lines (44 loc) · 1.63 KB
/
RelationFinder.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
import argparse
import ItemRelationsCsvReader
from collections import defaultdict
from CompressedFileType import CompressedFileType
def computeTable(generator):
table = {}
for entity, claims in generator:
for pid, value in claims:
if not entity in table:
table[entity] = set()
table[entity].add((pid, value))
return table
def computeRelations(table, itemList):
setList = []
for item in itemList:
setList.append(set(table[item]))
#todo: Compute in database!?!
intersection = setList[0].intersection(*setList[1:])
return intersection
def rankRelations(table, tupleSet):
result = []
for t in tupleSet:
i = 0
for item in table:
if t in table[item]:
i+=1
result.append((t[0],t[1],i))
result = sorted(result, key=lambda t: t[2])
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="this program finds relations between a list of wikidata-items")
parser.add_argument("input", help="The CSV input file (wikidata triple)", type=CompressedFileType("r"))
parser.add_argument("itemList", help="Comma seperated list of items to find relations for - example: 'Q937,Q35149,Q192112' ")
args = parser.parse_args()
print "computing table..."
table = computeTable(ItemRelationsCsvReader.read_csv(args.input))
itemList = args.itemList.split(",")
print "finding relations..."
result = computeRelations(table, itemList)
print "ranking relations..."
result = rankRelations(table, result)
print result
#if len(itemList)==2:
# pass...