@@ -50,7 +50,7 @@ def ngrams_counts(byte_obj: bytes | object, n: int = 1, step: int = 1) -> list[t
5050 a = np .frombuffer (data := byte_obj if isinstance (byte_obj , bytes ) else byte_obj .bytes , dtype = np .uint8 )
5151 l = a .size
5252 if l < n :
53- return {}
53+ return []
5454 if n == 1 :
5555 counts = {b .to_bytes (1 , "big" ): int (c ) for b , c in \
5656 enumerate (np .bincount (np .frombuffer (data , dtype = np .uint8 )))}
@@ -68,7 +68,7 @@ def ngrams_counts(byte_obj: bytes | object, n: int = 1, step: int = 1) -> list[t
6868 if n not in byte_obj ._ngram_counts_cache .keys ():
6969 byte_obj ._ngram_counts_cache [n ] = counts
7070 return byte_obj ._ngram_counts_cache [n ]
71- raise TypeError ("Bad input type ; should be a byte sequence or object" )
71+ raise TypeError (f "Bad input type ; should be a byte sequence or object (got ' { type ( byte_obj ) } ') " )
7272
7373
7474def ngrams_distribution (byte_obj : bytes | object , n : int = 1 , step : int = 1 , n_most_common : Optional [int ] = None ,
@@ -83,7 +83,8 @@ def ngrams_distribution(byte_obj: bytes | object, n: int = 1, step: int = 1, n_m
8383 :param exclude: list of specific n-grams to be excluded, no exclusion by default
8484 :return: list of n_most_common (n-gram, count) pairs
8585 """
86- c = ngrams_counts (byte_obj , n , step )
86+ if len (c := ngrams_counts (byte_obj , n , step )) == 0 :
87+ return []
8788 r = c [:len (c ) if n_most_common is None else n_most_common + n_exclude_top + len (exclude or [])]
8889 if exclude is not None :
8990 r = [(ngram , count ) for ngram , count in r if ngram not in exclude ]
0 commit comments