Returns a dictionary of (character n-gram, count)-items, in lowercase. N-grams in the exclude list are not counted. N-grams whose count falls below (or equals) the given threshold are excluded. N-grams that are not in the given top most counted are excluded.
(string="", n=3, top=None, threshold=0, exclude=[], **kwargs)
| 268 | return count |
| 269 | |
| 270 | def character_ngrams(string="", n=3, top=None, threshold=0, exclude=[], **kwargs): |
| 271 | """ Returns a dictionary of (character n-gram, count)-items, in lowercase. |
| 272 | N-grams in the exclude list are not counted. |
| 273 | N-grams whose count falls below (or equals) the given threshold are excluded. |
| 274 | N-grams that are not in the given top most counted are excluded. |
| 275 | """ |
| 276 | # An optional dict-parameter can be used to specify a subclass of dict, |
| 277 | # e.g., count(words, dict=readonlydict) as used in Document. |
| 278 | count = kwargs.get("dict", dict)() |
| 279 | for w in re.findall(r"(?=(" + "."*n + "))", string.lower()): |
| 280 | if w not in exclude: |
| 281 | dict.__setitem__(count, w, (w in count) and count[w]+1 or 1) |
| 282 | for k in count.keys(): |
| 283 | if count[k] <= threshold: |
| 284 | dict.__delitem__(count, k) |
| 285 | if top is not None: |
| 286 | count = count.__class__(heapq.nsmallest(top, count.iteritems(), key=lambda (k,v): (-v,k))) |
| 287 | return count |
| 288 | |
| 289 | chngrams = character_ngrams |
| 290 |
no test coverage detected
searching dependent graphs…