<s> import difflib <EOL> import os <EOL> import multiprocessing <EOL> import utils <EOL> import jdecode <EOL> import cardlib <EOL> libdir = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> datadir = os . path . realpath ( os . path . join ( libdir , '<STR_LIT>' ) ) <EOL> cores = multiprocessing . cpu_count ( ) <EOL> def list_split ( l , n ) : <EOL> if n <= <NUM_LIT:0> : <EOL> return l <EOL> split_size = len ( l ) / n <EOL> if len ( l ) % n > <NUM_LIT:0> : <EOL> split_size += <NUM_LIT:1> <EOL> return [ l [ i : i + split_size ] for i in range ( <NUM_LIT:0> , len ( l ) , split_size ) ] <EOL> def list_flatten ( l ) : <EOL> return [ item for sublist in l for item in sublist ] <EOL> def f_nearest ( name , matchers , n ) : <EOL> for m in matchers : <EOL> m . set_seq1 ( name ) <EOL> ratios = [ ( m . ratio ( ) , m . b ) for m in matchers ] <EOL> ratios . sort ( reverse = True ) <EOL> if ratios [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] >= <NUM_LIT:1> : <EOL> return ratios [ : <NUM_LIT:1> ] <EOL> else : <EOL> return ratios [ : n ] <EOL> def f_nearest_per_thread ( workitem ) : <EOL> ( worknames , names , n ) = workitem <EOL> matchers = [ difflib . SequenceMatcher ( b = name , autojunk = False ) for name in names ] <EOL> return map ( lambda name : f_nearest ( name , matchers , n ) , worknames ) <EOL> class Namediff : <EOL> def __init__ ( self , verbose = True , <EOL> json_fname = os . path . join ( datadir , '<STR_LIT>' ) ) : <EOL> self . verbose = verbose <EOL> self . names = { } <EOL> self . codes = { } <EOL> self . cardstrings = { } <EOL> if self . verbose : <EOL> print '<STR_LIT>' <EOL> if self . verbose : <EOL> print '<STR_LIT>' + json_fname <EOL> json_srcs = jdecode . mtg_open_json ( json_fname , verbose ) <EOL> namecount = <NUM_LIT:0> <EOL> for json_cardname in sorted ( json_srcs ) : <EOL> if len ( json_srcs [ json_cardname ] ) > <NUM_LIT:0> : <EOL> jcards = json_srcs [ json_cardname ] <EOL> idx = <NUM_LIT:0> <EOL> card = cardlib . Card ( jcards [ idx ] ) <EOL> name = card . name <EOL> jname = jcards [ idx ] [ '<STR_LIT:name>' ] <EOL> jcode = jcards [ idx ] [ utils . json_field_info_code ] <EOL> if '<STR_LIT>' in jcards [ idx ] : <EOL> jnum = jcards [ idx ] [ '<STR_LIT>' ] <EOL> else : <EOL> jnum = '<STR_LIT>' <EOL> if name in self . names : <EOL> print '<STR_LIT>' + name + '<STR_LIT>' <EOL> else : <EOL> self . names [ name ] = jname <EOL> self . cardstrings [ name ] = card . encode ( ) <EOL> if jcode and jnum : <EOL> self . codes [ name ] = jcode + '<STR_LIT:/>' + jnum + '<STR_LIT>' <EOL> else : <EOL> self . codes [ name ] = '<STR_LIT>' <EOL> namecount += <NUM_LIT:1> <EOL> print '<STR_LIT>' + str ( namecount ) + '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> self . matchers = [ difflib . SequenceMatcher ( b = n , autojunk = False ) for n in self . names ] <EOL> self . card_matchers = [ difflib . SequenceMatcher ( b = self . cardstrings [ n ] , autojunk = False ) for n in self . cardstrings ] <EOL> print '<STR_LIT>' <EOL> def nearest ( self , name , n = <NUM_LIT:3> ) : <EOL> return f_nearest ( name , self . matchers , n ) <EOL> def nearest_par ( self , names , n = <NUM_LIT:3> , threads = cores ) : <EOL> workpool = multiprocessing . Pool ( threads ) <EOL> proto_worklist = list_split ( names , threads ) <EOL> worklist = map ( lambda x : ( x , self . names , n ) , proto_worklist ) <EOL> donelist = workpool . map ( f_nearest_per_thread , worklist ) <EOL> return list_flatten ( donelist ) <EOL> def nearest_card ( self , card , n = <NUM_LIT:5> ) : <EOL> return f_nearest ( card . encode ( ) , self . card_matchers , n ) <EOL> def nearest_card_par ( self , cards , n = <NUM_LIT:5> , threads = cores ) : <EOL> workpool = multiprocessing . Pool ( threads ) <EOL> proto_worklist = list_split ( cards , threads ) <EOL> worklist = map ( lambda x : ( map ( lambda c : c . encode ( ) , x ) , self . cardstrings . values ( ) , n ) , proto_worklist ) <EOL> donelist = workpool . map ( f_nearest_per_thread , worklist ) <EOL> return list_flatten ( donelist ) </s>
