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

Function count

pattern/vector/__init__.py:245–268  ·  view source on GitHub ↗

Returns a dictionary of (word, count)-items, in lowercase. Words in the exclude list and stop words (by default, English) are not counted. Words whose count falls below (or equals) the given threshold are excluded. Words that are not in the given top most counted are exclude

(words=[], top=None, threshold=0, stemmer=None, exclude=[], stopwords=False, language=None, **kwargs)

Source from the content-addressed store, hash-verified

243 return word.lower()
244
245def count(words=[], top=None, threshold=0, stemmer=None, exclude=[], stopwords=False, language=None, **kwargs):
246 """ Returns a dictionary of (word, count)-items, in lowercase.
247 Words in the exclude list and stop words (by default, English) are not counted.
248 Words whose count falls below (or equals) the given threshold are excluded.
249 Words that are not in the given top most counted are excluded.
250 """
251 # An optional dict-parameter can be used to specify a subclass of dict,
252 # e.g., count(words, dict=readonlydict) as used in Document.
253 count = kwargs.get("dict", dict)()
254 for w in words:
255 if w.__class__.__name__ == "Word":
256 w = w.string.lower()
257 if isinstance(w, basestring):
258 w = w.lower()
259 if (stopwords or not w in _stopwords.get(language or "en", ())) and not w in exclude:
260 if stemmer is not None:
261 w = stem(w, stemmer, **kwargs)
262 dict.__setitem__(count, w, (w in count) and count[w]+1 or 1)
263 for k in count.keys():
264 if count[k] <= threshold:
265 dict.__delitem__(count, k)
266 if top is not None:
267 count = count.__class__(heapq.nsmallest(top, count.iteritems(), key=lambda (k,v): (-v,k)))
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.

Callers 2

instanceFunction · 0.90
__init__Method · 0.70

Calls 6

iteritemsMethod · 0.80
stemFunction · 0.70
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…