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)
| 243 | return word.lower() |
| 244 | |
| 245 | def 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 | |
| 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. |
no test coverage detected
searching dependent graphs…