Returns the base form of the word when counting words in count(). With stemmer=PORTER, the Porter2 stemming algorithm is used. With stemmer=LEMMA, either uses Word.lemma or inflect.singularize(). (with optional parameter language="en", pattern.en.inflect is used).
(word, stemmer=PORTER, **kwargs)
| 213 | |
| 214 | PORTER, LEMMA = "porter", "lemma" |
| 215 | def stem(word, stemmer=PORTER, **kwargs): |
| 216 | """ Returns the base form of the word when counting words in count(). |
| 217 | With stemmer=PORTER, the Porter2 stemming algorithm is used. |
| 218 | With stemmer=LEMMA, either uses Word.lemma or inflect.singularize(). |
| 219 | (with optional parameter language="en", pattern.en.inflect is used). |
| 220 | """ |
| 221 | if isinstance(word, basestring): |
| 222 | word = decode_utf8(word.lower()) |
| 223 | if stemmer is None: |
| 224 | return word.lower() |
| 225 | if stemmer == PORTER: |
| 226 | return _stemmer.stem(word, **kwargs) |
| 227 | if stemmer == LEMMA: |
| 228 | if word.__class__.__name__ == "Word": |
| 229 | w = word.string.lower() |
| 230 | if word.lemma is not None: |
| 231 | return word.lemma |
| 232 | if word.pos == "NNS": |
| 233 | return singularize(w) |
| 234 | if word.pos.startswith(("VB", "MD")): |
| 235 | return conjugate(w, "infinitive") or w |
| 236 | if word.pos.startswith(("JJ",)): |
| 237 | return predicative(w) |
| 238 | if word.pos.startswith(("DT", "PR", "WP")): |
| 239 | return singularize(w, pos=pos) |
| 240 | return singularize(word, pos=kwargs.get("pos", "noun")) |
| 241 | if hasattr(stemmer, "__call__"): |
| 242 | return decode_utf8(stemmer(word)) |
| 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. |
no test coverage detected
searching dependent graphs…