#!/usr/bin/env python

from nltk.corpus import wordnet as wn

def identify_missing_inverses():
    """
    Exhaustive search of WordNet to identify any exceptions to
    relation pairs that we expect to be inverses of each other.
    """    
    # Synset relation-pairs that we expect to be inverses of each other:
    expected_pairs = (('hypernyms','hyponyms'),
                      ('instance_hypernyms','instance_hyponyms'),
                      ('member_holonyms','member_meronyms'),
                      ('substance_holonyms','substance_meronyms'),    
                      ('part_holonyms','part_meronyms'))
    # Iterate through all synsets:
    for syn1 in wn.all_synsets():
        for relA, relB in expected_pairs:
            # Check both orderings:
            for rel1, rel2 in ((relA, relB), (relB, relA)):
                # Treat rel1 as a method name and get the associated values:
                method1 = getattr(syn1, rel1)
                vals1 = method1()
                for syn2 in vals1:
                    # Treat rel2 as a method name and get the associated values:
                    method2 = getattr(syn2, rel2)
                    vals2 = method2()
                    # This is the non-inverse condition:
                    if syn1 not in vals2:
                        print "======================================================================"
                        print "%(b)s in %(a)s.%(r1)s but\n%(a)s not in %(b)s.%(r2)s" % {'a':syn1, 'b':syn2, 'r1':rel1, 'r2':rel2}
                        
# identify_missing_inverses()                
