MCPcopy Index your code
hub / github.com/clips/pattern / character_ngrams

Function character_ngrams

pattern/vector/__init__.py:270–287  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

268 return count
269
270def 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
289chngrams = character_ngrams
290

Callers 1

trigramsFunction · 0.85

Calls 5

iteritemsMethod · 0.80
getMethod · 0.45
__setitem__Method · 0.45
keysMethod · 0.45
__delitem__Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…