<s> """<STR_LIT>""" <EOL> import sys <EOL> import ellyChar <EOL> import ellyException <EOL> NOname = '<STR_LIT>' <EOL> FRAC = <NUM_LIT:20> <EOL> TOP = '<STR_LIT>' <EOL> class Concept : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . level = - <NUM_LIT:1> <EOL> self . parent = None <EOL> self . children = [ ] <EOL> self . split = <NUM_LIT:0> <EOL> self . total = <NUM_LIT:0> <EOL> self . alias = [ ] <EOL> self . unseen = True <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> pname = NOname if self . parent == None else self . parent . name <EOL> if self . split == <NUM_LIT:0> : <EOL> chs = '<STR_LIT>' <EOL> else : <EOL> label = '<STR_LIT>' if self . split == <NUM_LIT:1> else '<STR_LIT>' <EOL> chs = '<STR_LIT>' + str ( self . split ) + label <EOL> return self . name + '<STR_LIT:(>' + str ( self . total ) + '<STR_LIT>' + pname + chs <EOL> class ConceptualHierarchy : <EOL> """<STR_LIT>""" <EOL> def getConcept ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name = name . strip ( ) . upper ( ) <EOL> if name == NOname : return None <EOL> elif name == TOP : return self . index [ TOP ] <EOL> elif len ( name ) == <NUM_LIT:0> or not ellyChar . isLetterOrDigit ( name [ <NUM_LIT:0> ] ) : return None <EOL> elif name in self . index : return self . index [ name ] <EOL> for x in name : <EOL> if not ellyChar . isLetterOrDigit ( x ) : return None <EOL> c = Concept ( name ) <EOL> self . index [ name ] = c <EOL> return c <EOL> def __init__ ( self , defn = None ) : <EOL> """<STR_LIT>""" <EOL> self . _errcount = <NUM_LIT:0> <EOL> top = Concept ( TOP ) <EOL> top . level = - <NUM_LIT:1> <EOL> self . index = { TOP : top } <EOL> self . inters = None <EOL> eqv = [ ] <EOL> if defn == None : return <EOL> while True : <EOL> l = defn . readline ( ) <EOL> if len ( l ) == <NUM_LIT:0> : break <EOL> if l [ <NUM_LIT:0> ] == '<STR_LIT:=>' : <EOL> sl = l <EOL> l = l [ <NUM_LIT:1> : ] . lstrip ( ) . upper ( ) <EOL> k = l . find ( '<STR_LIT:U+0020>' ) <EOL> if k < <NUM_LIT:0> : <EOL> self . _err ( '<STR_LIT>' , sl ) <EOL> continue <EOL> s = l [ : k ] <EOL> l = l [ k : ] . lstrip ( ) <EOL> eqv . append ( [ s , l ] ) <EOL> continue <EOL> ls = l . split ( '<STR_LIT:>>' ) <EOL> if len ( ls ) > <NUM_LIT:2> : <EOL> self . _err ( '<STR_LIT>' , l ) <EOL> continue <EOL> elif len ( ls ) < <NUM_LIT:2> or len ( ls [ <NUM_LIT:0> ] ) == <NUM_LIT:0> or len ( ls [ <NUM_LIT:1> ] ) == <NUM_LIT:0> : <EOL> self . _err ( '<STR_LIT>' , l ) <EOL> continue <EOL> cPA = self . getConcept ( ls [ <NUM_LIT:0> ] ) <EOL> cCH = self . getConcept ( ls [ <NUM_LIT:1> ] ) <EOL> if cPA == None or cCH == None : <EOL> self . _err ( '<STR_LIT>' , l ) <EOL> continue <EOL> if cCH . parent != None : <EOL> self . _err ( '<STR_LIT>' , l ) <EOL> continue <EOL> else : <EOL> cPA . split += <NUM_LIT:1> <EOL> cCH . parent = cPA <EOL> cPA . children . append ( cCH ) <EOL> for p in eqv : <EOL> if not p [ <NUM_LIT:0> ] in self . index : <EOL> self . _err ( '<STR_LIT>' , p [ <NUM_LIT:1> ] ) <EOL> continue <EOL> c = self . index [ p [ <NUM_LIT:0> ] ] <EOL> self . index [ p [ <NUM_LIT:1> ] ] = c <EOL> p1l = p [ <NUM_LIT:1> ] . lower ( ) <EOL> c . alias . append ( p1l ) <EOL> cnls = self . index . keys ( ) <EOL> for cn in cnls : <EOL> if cn != TOP : <EOL> c = self . index [ cn ] <EOL> if c . parent == None : <EOL> print >> sys . stderr , '<STR_LIT>' , cn <EOL> self . _errcount += <NUM_LIT:1> <EOL> if len ( top . children ) == <NUM_LIT:0> and len ( cnls ) > <NUM_LIT:1> : <EOL> self . _err ( '<STR_LIT>' ) <EOL> if self . _errcount > <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' , self . _errcount , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> raise ellyException . TableFailure <EOL> for c in cnls : <EOL> cn = self . index [ c ] <EOL> if cn . split == <NUM_LIT:0> and cn . unseen : <EOL> cn . unseen = False <EOL> n = <NUM_LIT:0> <EOL> while cn != top : <EOL> cn . total += n <EOL> cn = cn . parent <EOL> n += <NUM_LIT:1> <EOL> cn . total += n <EOL> stk = [ ] <EOL> stk . append ( [ top ] ) <EOL> while len ( stk ) > <NUM_LIT:0> : <EOL> cs = stk [ - <NUM_LIT:1> ] <EOL> if len ( cs ) == <NUM_LIT:0> : <EOL> stk . pop ( ) <EOL> else : <EOL> c = cs . pop ( ) <EOL> if c . level >= <NUM_LIT:0> : <EOL> self . _err ( '<STR_LIT>' , str ( c ) , c . level , len ( stk ) ) <EOL> break <EOL> c . level = len ( stk ) - <NUM_LIT:1> <EOL> stk . append ( c . children ) <EOL> if self . _errcount > <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> raise ellyException . TableFailure <EOL> print len ( cnls ) , "<STR_LIT>" <EOL> def intersection ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . inters <EOL> def _err ( self , s , l = '<STR_LIT>' , lvl = None , dep = None ) : <EOL> """<STR_LIT>""" <EOL> self . _errcount += <NUM_LIT:1> <EOL> print >> sys . stderr , '<STR_LIT>' , s <EOL> if l != '<STR_LIT>' : <EOL> print >> sys . stderr , '<STR_LIT>' , l , '<STR_LIT:]>' , <EOL> if lvl == None : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> else : <EOL> print >> sys . stderr , str ( lvl ) + '<STR_LIT:/>' + str ( dep ) <EOL> def isEmpty ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( self . index ) <= <NUM_LIT:1> ) <EOL> def generalize ( self , an ) : <EOL> """<STR_LIT>""" <EOL> n = int ( len ( self . index ) / FRAC ) <EOL> if an == TOP : <EOL> return TOP <EOL> elif not an in self . index : <EOL> return NOname <EOL> else : <EOL> a = ab = self . index [ an ] <EOL> while a != None and a . total <= n and a . level > <NUM_LIT:4> : <EOL> ab = a <EOL> a = a . parent <EOL> return ab . name <EOL> def isA ( self , an , bn ) : <EOL> """<STR_LIT>""" <EOL> self . inters = None <EOL> if not an in self . index or not bn in self . index : return - <NUM_LIT:1> <EOL> a = self . index [ an ] <EOL> b = self . index [ bn ] <EOL> if a . level < b . level : return - <NUM_LIT:1> <EOL> c = a <EOL> while c . level > b . level : <EOL> c = c . parent <EOL> if b == c : <EOL> self . inters = c <EOL> return a . level - b . level <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def relatedness ( self , an , bn ) : <EOL> """<STR_LIT>""" <EOL> if not an in self . index or not bn in self . index : return - <NUM_LIT:1> <EOL> a = self . index [ an ] <EOL> b = self . index [ bn ] <EOL> while a . level < b . level : <EOL> b = b . parent <EOL> while a . level > b . level : <EOL> a = a . parent <EOL> while a != b : <EOL> a = a . parent <EOL> b = b . parent <EOL> self . inters = a <EOL> return a . level <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import ellyDefinitionReader <EOL> data = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> def tisA ( tre , a , b ) : <EOL> """<STR_LIT>""" <EOL> k = tre . isA ( a . upper ( ) , b . upper ( ) ) <EOL> x = tre . intersection ( ) . name if k >= <NUM_LIT:0> else NOname <EOL> print "<STR_LIT>" + a + "<STR_LIT:U+002C>" + b + "<STR_LIT>" , <EOL> print k , "<STR_LIT>" , x <EOL> def trelatedness ( tre , a , b ) : <EOL> """<STR_LIT>""" <EOL> k = tre . relatedness ( a . upper ( ) , b . upper ( ) ) <EOL> x = tre . intersection ( ) . name if k >= <NUM_LIT:0> else NOname <EOL> print "<STR_LIT>" + a + "<STR_LIT:U+002C>" + b + "<STR_LIT>" , <EOL> print k , "<STR_LIT>" , x <EOL> def tdump ( tre ) : <EOL> """<STR_LIT>""" <EOL> kyl = tre . index . keys ( ) <EOL> for ky in kyl : <EOL> if ky == '<STR_LIT>' : continue <EOL> r = tre . index [ ky ] <EOL> print ky , '<STR_LIT:(>' , r . name , '<STR_LIT:)>' , '<STR_LIT>' , r . level , '<STR_LIT:<>' , r . parent . name <EOL> filn = sys . argv [ <NUM_LIT:1> ] + '<STR_LIT>' if len ( sys . argv ) > <NUM_LIT:1> else data <EOL> inp = ellyDefinitionReader . EllyDefinitionReader ( filn ) <EOL> if inp . error != None : <EOL> print >> sys . stderr , inp . error <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif filn != data : <EOL> print >> sys . stderr , '<STR_LIT>' , filn <EOL> print inp . linecount ( ) , "<STR_LIT>" <EOL> try : <EOL> ctre = ConceptualHierarchy ( inp ) <EOL> except ellyException . TableFailure : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if ctre . isEmpty ( ) : <EOL> print "<STR_LIT>" <EOL> elif filn == data : <EOL> print "<STR_LIT>" <EOL> tdump ( ctre ) <EOL> print "<STR_LIT>" <EOL> tisA ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tisA ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tisA ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tisA ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> trelatedness ( ctre , '<STR_LIT>' , '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> print ctre . index [ '<STR_LIT>' ] <EOL> print ctre . index [ '<STR_LIT>' ] <EOL> print "<STR_LIT>" <EOL> print ctre . generalize ( '<STR_LIT>' ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> tdump ( ctre ) </s>
