<s> """<STR_LIT>""" <EOL> class Node : <EOL> def __init__ ( self ) : <EOL> self . value = None <EOL> self . children = { } <EOL> class Trie : <EOL> def __init__ ( self ) : <EOL> self . root = Node ( ) <EOL> def add ( self , key , value ) : <EOL> curr_node = self . root <EOL> for part in key : <EOL> curr_node = curr_node . children . setdefault ( part , Node ( ) ) <EOL> curr_node . value = value <EOL> def find_prefix ( self , key ) : <EOL> curr_node = self . root <EOL> remainder = key <EOL> for part in key : <EOL> if part not in curr_node . children : <EOL> break <EOL> curr_node = curr_node . children [ part ] <EOL> remainder = remainder [ <NUM_LIT:1> : ] <EOL> return ( curr_node . value , remainder ) <EOL> class Collator : <EOL> def __init__ ( self , filename ) : <EOL> self . table = Trie ( ) <EOL> self . load ( filename ) <EOL> def load ( self , filename ) : <EOL> for line in open ( filename ) : <EOL> if line . startswith ( "<STR_LIT:#>" ) or line . startswith ( "<STR_LIT:%>" ) : <EOL> continue <EOL> if line . strip ( ) == "<STR_LIT>" : <EOL> continue <EOL> line = line [ : line . find ( "<STR_LIT:#>" ) ] + "<STR_LIT:\n>" <EOL> line = line [ : line . find ( "<STR_LIT:%>" ) ] + "<STR_LIT:\n>" <EOL> line = line . strip ( ) <EOL> if line . startswith ( "<STR_LIT:@>" ) : <EOL> pass <EOL> else : <EOL> semicolon = line . find ( "<STR_LIT:;>" ) <EOL> charList = line [ : semicolon ] . strip ( ) . split ( ) <EOL> x = line [ semicolon : ] <EOL> collElements = [ ] <EOL> while True : <EOL> begin = x . find ( "<STR_LIT:[>" ) <EOL> if begin == - <NUM_LIT:1> : <EOL> break <EOL> end = x [ begin : ] . find ( "<STR_LIT:]>" ) <EOL> collElement = x [ begin : begin + end + <NUM_LIT:1> ] <EOL> x = x [ begin + <NUM_LIT:1> : ] <EOL> alt = collElement [ <NUM_LIT:1> ] <EOL> chars = collElement [ <NUM_LIT:2> : - <NUM_LIT:1> ] . split ( "<STR_LIT:.>" ) <EOL> collElements . append ( ( alt , chars ) ) <EOL> integer_points = [ int ( ch , <NUM_LIT:16> ) for ch in charList ] <EOL> self . table . add ( integer_points , collElements ) <EOL> def sort_key ( self , string ) : <EOL> collation_elements = [ ] <EOL> lookup_key = [ ord ( ch ) for ch in string ] <EOL> while lookup_key : <EOL> value , lookup_key = self . table . find_prefix ( lookup_key ) <EOL> if not value : <EOL> value = [ ] <EOL> value . append ( ( "<STR_LIT:.>" , [ "<STR_LIT>" % ( <NUM_LIT> + ( lookup_key [ <NUM_LIT:0> ] >> <NUM_LIT:15> ) ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> value . append ( ( "<STR_LIT:.>" , [ "<STR_LIT>" % ( ( lookup_key [ <NUM_LIT:0> ] & <NUM_LIT> ) | <NUM_LIT> ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> lookup_key = lookup_key [ <NUM_LIT:1> : ] <EOL> collation_elements . extend ( value ) <EOL> sort_key = [ ] <EOL> for level in range ( <NUM_LIT:4> ) : <EOL> if level : <EOL> sort_key . append ( <NUM_LIT:0> ) <EOL> for element in collation_elements : <EOL> ce_l = int ( element [ <NUM_LIT:1> ] [ level ] , <NUM_LIT:16> ) <EOL> if ce_l : <EOL> sort_key . append ( ce_l ) <EOL> return tuple ( sort_key ) </s>
