Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert stop words filter from SortedSet/Array to Object #170

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 123 additions & 126 deletions lib/stop_word_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,132 +16,129 @@
* @see lunr.Pipeline
*/
lunr.stopWordFilter = function (token) {
if (lunr.stopWordFilter.stopWords.indexOf(token) === -1) return token
if (token && lunr.stopWordFilter.stopWords[token] !== token) return token;
}

lunr.stopWordFilter.stopWords = new lunr.SortedSet
lunr.stopWordFilter.stopWords.length = 119
lunr.stopWordFilter.stopWords.elements = [
"",
"a",
"able",
"about",
"across",
"after",
"all",
"almost",
"also",
"am",
"among",
"an",
"and",
"any",
"are",
"as",
"at",
"be",
"because",
"been",
"but",
"by",
"can",
"cannot",
"could",
"dear",
"did",
"do",
"does",
"either",
"else",
"ever",
"every",
"for",
"from",
"get",
"got",
"had",
"has",
"have",
"he",
"her",
"hers",
"him",
"his",
"how",
"however",
"i",
"if",
"in",
"into",
"is",
"it",
"its",
"just",
"least",
"let",
"like",
"likely",
"may",
"me",
"might",
"most",
"must",
"my",
"neither",
"no",
"nor",
"not",
"of",
"off",
"often",
"on",
"only",
"or",
"other",
"our",
"own",
"rather",
"said",
"say",
"says",
"she",
"should",
"since",
"so",
"some",
"than",
"that",
"the",
"their",
"them",
"then",
"there",
"these",
"they",
"this",
"tis",
"to",
"too",
"twas",
"us",
"wants",
"was",
"we",
"were",
"what",
"when",
"where",
"which",
"while",
"who",
"whom",
"why",
"will",
"with",
"would",
"yet",
"you",
"your"
]
lunr.stopWordFilter.stopWords = {
a: 'a',
able: 'able',
about: 'about',
across: 'across',
after: 'after',
all: 'all',
almost: 'almost',
also: 'also',
am: 'am',
among: 'among',
an: 'an',
and: 'and',
any: 'any',
are: 'are',
as: 'as',
at: 'at',
be: 'be',
because: 'because',
been: 'been',
but: 'but',
by: 'by',
can: 'can',
cannot: 'cannot',
could: 'could',
dear: 'dear',
did: 'did',
do: 'do',
does: 'does',
either: 'either',
else: 'else',
ever: 'ever',
every: 'every',
for: 'for',
from: 'from',
get: 'get',
got: 'got',
had: 'had',
has: 'has',
have: 'have',
he: 'he',
her: 'her',
hers: 'hers',
him: 'him',
his: 'his',
how: 'how',
however: 'however',
i: 'i',
if: 'if',
in: 'in',
into: 'into',
is: 'is',
it: 'it',
its: 'its',
just: 'just',
least: 'least',
let: 'let',
like: 'like',
likely: 'likely',
may: 'may',
me: 'me',
might: 'might',
most: 'most',
must: 'must',
my: 'my',
neither: 'neither',
no: 'no',
nor: 'nor',
not: 'not',
of: 'of',
off: 'off',
often: 'often',
on: 'on',
only: 'only',
or: 'or',
other: 'other',
our: 'our',
own: 'own',
rather: 'rather',
said: 'said',
say: 'say',
says: 'says',
she: 'she',
should: 'should',
since: 'since',
so: 'so',
some: 'some',
than: 'than',
that: 'that',
the: 'the',
their: 'their',
them: 'them',
then: 'then',
there: 'there',
these: 'these',
they: 'they',
this: 'this',
tis: 'tis',
to: 'to',
too: 'too',
twas: 'twas',
us: 'us',
wants: 'wants',
was: 'was',
we: 'we',
were: 'were',
what: 'what',
when: 'when',
where: 'where',
which: 'which',
while: 'while',
who: 'who',
whom: 'whom',
why: 'why',
will: 'will',
with: 'with',
would: 'would',
yet: 'yet',
you: 'you',
your: 'your'
}

lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'stopWordFilter')
lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'stopWordFilter')
8 changes: 8 additions & 0 deletions test/stop_word_filter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ test('non stop words pass through', function () {
})
})

test('should not filter Object.prototype terms', function () {
var nonStopWords = ['constructor', 'hasOwnProperty', 'toString', 'valueOf']

nonStopWords.forEach(function (word) {
equal(lunr.stopWordFilter(word), word)
})
})

test('should be registered with lunr.Pipeline', function () {
equal(lunr.stopWordFilter.label, 'stopWordFilter')
deepEqual(lunr.Pipeline.registeredFunctions['stopWordFilter'], lunr.stopWordFilter)
Expand Down