@@ -1303,9 +1303,13 @@ class _Text(object):
13031303 __slots__ = ["entropy" , "lcharset" , "len" , "padding" , "printables" ]
13041304
13051305 def __init__ (self , text , pad_char = None ):
1306+ c = text [- 1 ]
1307+ last_char = c if isinstance (c , int ) else ord (c )
1308+ self .padding = pad_char is not None and last_char == ord (pad_char )
1309+ if self .padding :
1310+ text = text .rstrip (pad_char )
13061311 self .len = len (text )
13071312 self .lcharset = len (set (text ))
1308- self .padding = pad_char is not None and text [- 1 ] in [pad_char , b (pad_char )]
13091313 self .printables = float (len ([c for c in text if (chr (c ) if isinstance (c , int ) else c ) in printable ])) / self .len
13101314 self .entropy = entropy (text )
13111315
@@ -1363,16 +1367,16 @@ def __score(prev_input, input, prev_encoding, codec, heuristic=False, extended=F
13631367 s += .1
13641368 expf = sc .get ('expansion_factor' , 1. )
13651369 if expf :
1366- f = float (len (new_input )) / obj . len
1370+ f = obj . len / float (len (new_input )) # expansion while encoding => at decoding: 1/f
13671371 if isinstance (expf , type (lambda : None )):
13681372 try : # this case allows to consider the current encoding name from the current codec
13691373 expf = expf (f , encoding )
13701374 except TypeError :
13711375 expf = expf (f )
13721376 if isinstance (expf , (int , float )):
1373- expf = (f - .1 <= expf <= f + .1 )
1377+ expf = (1 / f - .1 <= 1 / expf <= 1 / f + .1 )
13741378 elif isinstance (expf , (tuple , list )) and len (expf ) == 2 :
1375- expf = f - expf [1 ] <= expf [0 ] <= expf [ 1 ] + .1
1379+ expf = 1 / f - expf [1 ] <= 1 / expf [0 ] <= 1 / f + expf [ 1 ]
13761380 s += [- 1. , .1 ][expf ]
13771381 # afterwards, if the input text has an entropy close to the expected one, give a bonus weighted on the
13781382 # number of input characters to take bad entropies of shorter strings into account
0 commit comments