transform

old TransE-like models
git clone https://esimon.eu/repos/transform.git
Log | Files | Refs | README

relations analyzer.py (2806B)


      1 #!/usr/bin/env python2
      2 
      3 from __future__ import print_function
      4 import sys
      5 
      6 from dataset import *
      7 
      8 if __name__ == '__main__':
      9     if len(sys.argv)<2:
     10         print('Usage: {0} dataset'.format(sys.argv[0]), file=sys.stderr)
     11         sys.exit(1)
     12     data = Dataset(sys.argv[1])
     13 
     14     print('# Splitting dataset and building adjacency lists')
     15     lefts, rights = [[] for _ in xrange(data.number_relations)], [[] for _ in xrange(data.number_relations)]
     16     adj_list = [[] for _ in xrange(data.number_entities)]
     17     for relation, left, right in data.iterate("test"):
     18         left, relation, right = left.indices[0], relation.indices[0], right.indices[0]
     19         lefts[relation].append(left)
     20         rights[relation].append(right)
     21         adj_list[left].append((relation, right))
     22 
     23     total_injective, total_functional, total_onetoone, total_irreflexive, total_circuit = 0, 0, 0, 0, 0
     24     print(' '*103+'injective functional 1-to-1 irreflexive circuit')
     25     for index, relation in enumerate(data.relations):
     26         left = lefts[index]
     27         right = rights[index]
     28         preimage = set(left)
     29         image = set(right)
     30         injective = len(image) == len(right)
     31         functional = len(preimage) == len(left)
     32         onetoone = injective & functional
     33 
     34         irreflexive = True
     35         for l,r in zip(left, right):
     36             if l==r:
     37                 irreflexive = False
     38 
     39         visitme = image & preimage
     40         visiting = set()
     41 
     42         def dfs(node):
     43             visiting.add(node)
     44             for (rel, right) in adj_list[node]:
     45                 if rel == index:
     46                     if right in visiting:
     47                         return True
     48                     elif right in visitme:
     49                         visitme.remove(right)
     50                         if dfs(right):
     51                             return True
     52             visiting.remove(node)
     53             return False
     54 
     55         circuit = False
     56         while len(visitme)>0:
     57             if dfs(visitme.pop()):
     58                 circuit = True
     59                 break
     60 
     61 
     62         print('{0:<100} : {1:<9} {2:<10} {3:<6} {4:<11} {5:<6}'.format(relation, injective, functional, onetoone, irreflexive, circuit))
     63 
     64         total_injective += injective
     65         total_functional += functional
     66         total_irreflexive += irreflexive
     67         total_onetoone += onetoone
     68         total_circuit += circuit
     69     
     70     N = float(len(data.relations))
     71     print('')
     72     print('Total injective: {0} ({1})'.format(total_injective/N, total_injective))
     73     print('Total functional: {0} ({1})'.format(total_functional/N, total_functional))
     74     print('Total 1-to-1: {0} ({1})'.format(total_onetoone/N, total_onetoone))
     75     print('Total irreflexive: {0} ({1})'.format(total_irreflexive/N, total_irreflexive))
     76     print('Total circuit: {0} ({1})'.format(total_circuit/N, total_circuit))