Skip to content

Update stemming and Snowball #13561

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

Merged
merged 19 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Features added
``linkcheck_allowed_redirects = {}``.
Patch by Adam Turner.
* #13497: Support C domain objects in the table of contents.
* #13535: html search: Update to the latest version of Snowball (v3.0.1).
Patch by Adam Turner.

Bugs fixed
----------
Expand Down
12 changes: 7 additions & 5 deletions doc/internals/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,15 @@ Updating generated files
------------------------

* JavaScript stemming algorithms in :file:`sphinx/search/non-minified-js/*.js`
are generated using `snowball <https://github.com/snowballstem/snowball>`_
by cloning the repository, executing ``make dist_libstemmer_js`` and then
unpacking the tarball which is generated in :file:`dist` directory.
and stopword files in :file:`sphinx/search/_stopwords/`
are generated from the `Snowball project`_
by running :file:`utils/generate_snowball.py`.

Minified files in :file:`sphinx/search/minified-js/*.js` are generated from
non-minified ones using :program:`uglifyjs` (installed via npm), with ``-m``
option to enable mangling.
non-minified ones using :program:`uglifyjs` (installed via npm).
See :file:`sphinx/search/minified-js/README.rst`.

.. _Snowball project: https://snowballstem.org/

* The :file:`searchindex.js` files found in
the :file:`tests/js/fixtures/*` directories
Expand Down
42 changes: 21 additions & 21 deletions sphinx/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@
"""Return true if the target word should be registered in the search index.
This method is called after stemming.
"""
return len(word) == 0 or not (
((len(word) < 3) and (12353 < ord(word[0]) < 12436))
or (ord(word[0]) < 256 and (word in self.stopwords))
)
return word == '' or not word.isdigit() or word not in self.stopwords

Check failure on line 120 in sphinx/search/__init__.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLC1901)

sphinx/search/__init__.py:120:24: PLC1901 `word == ''` can be simplified to `not word` as an empty string is falsey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want word == '' in here? From the docstring, that seems to indicate that empty-strings would be included in the index?

(the test indices don't indicate any actual change in behaviour -- I'm not sure how an empty-string would be provided to the _filter function here..)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this has been marked as resolved, but I can answer the last part. It looks like this function gets called with both unstemmed words and their stems, and it's possible for get an empty string output from some stemmers for a non-empty input.

I would tend to argue that a well designed stemming algorithm should only produce an empty output for an empty input. However there have been a few cases where an algorithm can produce an empty stem for a certain non-empty input (or inputs). We've usually fixed these when they've come to light, but the original porter algorithm produces an empty stem for input s and given it's aiming to essentially be a reference implementation we have left that alone.

I'm pretty confident you won't get an empty stem from "english". You may from other algorithms (but please report if you do).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we could/should add an info-level output message when we detect empty-string output from a stemmer, with a message indicating that it may be a Snowball bug.

(a warning message wouldn't be appropriate, because there isn't anything the user can do to fix it (and many Sphinx users run in strict mode, where warnings fail the build))

I'm leaning towards allowing the empty string values to reach the index file itself, because, provided that both Python and JS stemming is consistent, that seems marginally more correct behaviour-wise.



# SearchEnglish imported after SearchLanguage is defined due to circular import
Expand Down Expand Up @@ -503,32 +500,34 @@

_filter = self.lang.word_filter
_stem = self.lang.stem
_mapping = self._mapping

# memoise self.lang.stem
@functools.cache
def stem(word_to_stem: str) -> str:
return _stem(word_to_stem).lower()

def add_term(term: str, /) -> None:
if _filter(term):
_mapping.setdefault(term, set()).add(docname)

self._all_titles[docname] = word_store.titles

for word in word_store.title_words:
# add stemmed and unstemmed as the stemmer must not remove words
# from search index.
stemmed_word = stem(word)
if _filter(stemmed_word):
self._title_mapping.setdefault(stemmed_word, set()).add(docname)
elif _filter(word):
self._title_mapping.setdefault(word, set()).add(docname)
add_term(stemmed_word)
add_term(word)

for word in word_store.words:
# add stemmed and unstemmed as the stemmer must not remove words
# from search index.
stemmed_word = stem(word)
if not _filter(stemmed_word) and _filter(word):
stemmed_word = word
already_indexed = docname in self._title_mapping.get(stemmed_word, ())
if _filter(stemmed_word) and not already_indexed:
self._mapping.setdefault(stemmed_word, set()).add(docname)
if not already_indexed:
add_term(stemmed_word)
add_term(word)

# find explicit entries within index directives
_index_entries: set[tuple[str, str, str]] = set()
Expand Down Expand Up @@ -583,17 +582,18 @@

def get_js_stemmer_code(self) -> str:
"""Returns JS code that will be inserted into language_data.js."""
if self.lang.js_stemmer_rawcode:
base_js_path = _NON_MINIFIED_JS_PATH / 'base-stemmer.js'
language_js_path = _NON_MINIFIED_JS_PATH / self.lang.js_stemmer_rawcode
base_js = base_js_path.read_text(encoding='utf-8')
language_js = language_js_path.read_text(encoding='utf-8')
return (
f'{base_js}\n{language_js}\nStemmer = {self.lang.language_name}Stemmer;'
)
else:
if not self.lang.js_stemmer_rawcode:
return self.lang.js_stemmer_code

base_js_path = _MINIFIED_JS_PATH / 'base-stemmer.js'
language_js_path = _MINIFIED_JS_PATH / self.lang.js_stemmer_rawcode
return '\n'.join((
base_js_path.read_text(encoding='utf-8'),
language_js_path.read_text(encoding='utf-8'),
f'const Stemmer = {self.lang.language_name}Stemmer;',
f'globalThis.Stemmer = {self.lang.language_name}Stemmer;',
))


def _feed_visit_nodes(
node: nodes.Node,
Expand Down
3 changes: 3 additions & 0 deletions sphinx/search/_stopwords/da.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# automatically generated by utils/generate-snowball.py
# from https://snowballstem.org/algorithms/danish/stop.txt

from __future__ import annotations

DANISH_STOPWORDS = frozenset({
Expand Down
9 changes: 8 additions & 1 deletion sphinx/search/_stopwords/da.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
| source: https://snowballstem.org/algorithms/danish/stop.txt

| A Danish stop word list. Comments begin with vertical bar. Each stop
| word is at the start of a line.

| This is a ranked list (commonest to rarest) of stopwords derived from
| a large text sample.


og | and
i | in
jeg | I
Expand Down
3 changes: 3 additions & 0 deletions sphinx/search/_stopwords/de.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# automatically generated by utils/generate-snowball.py
# from https://snowballstem.org/algorithms/german/stop.txt

from __future__ import annotations

GERMAN_STOPWORDS = frozenset({
Expand Down
9 changes: 8 additions & 1 deletion sphinx/search/_stopwords/de.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
|source: https://snowballstem.org/algorithms/german/stop.txt

| A German stop word list. Comments begin with vertical bar. Each stop
| word is at the start of a line.

| The number of forms in this list is reduced significantly by passing it
| through the German stemmer.


aber | but

alle | all
Expand Down
148 changes: 146 additions & 2 deletions sphinx/search/_stopwords/en.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,181 @@
# automatically generated by utils/generate-snowball.py
# from https://snowballstem.org/algorithms/english/stop.txt

from __future__ import annotations

ENGLISH_STOPWORDS = frozenset({
'a',
'about',
'above',
'after',
'again',
'against',
'all',
'am',
'an',
'and',
'any',
'are',
"aren't",
'as',
'at',
'be',
'because',
'been',
'before',
'being',
'below',
'between',
'both',
'but',
'by',
"can't",
'cannot',
'could',
"couldn't",
'did',
"didn't",
'do',
'does',
"doesn't",
'doing',
"don't",
'down',
'during',
'each',
'few',
'for',
'from',
'further',
'had',
"hadn't",
'has',
"hasn't",
'have',
"haven't",
'having',
'he',
"he'd",
"he'll",
"he's",
'her',
'here',
"here's",
'hers',
'herself',
'him',
'himself',
'his',
'how',
"how's",
'i',
"i'd",
"i'll",
"i'm",
"i've",
'if',
'in',
'into',
'is',
"isn't",
'it',
'near',
"it's",
'its',
'itself',
"let's",
'me',
'more',
'most',
"mustn't",
'my',
'myself',
'no',
'nor',
'not',
'of',
'off',
'on',
'once',
'only',
'or',
'other',
'ought',
'our',
'ours',
'ourselves',
'out',
'over',
'own',
'same',
"shan't",
'she',
"she'd",
"she'll",
"she's",
'should',
"shouldn't",
'so',
'some',
'such',
'than',
'that',
"that's",
'the',
'their',
'theirs',
'them',
'themselves',
'then',
'there',
"there's",
'these',
'they',
"they'd",
"they'll",
"they're",
"they've",
'this',
'those',
'through',
'to',
'too',
'under',
'until',
'up',
'very',
'was',
'will',
"wasn't",
'we',
"we'd",
"we'll",
"we're",
"we've",
'were',
"weren't",
'what',
"what's",
'when',
"when's",
'where',
"where's",
'which',
'while',
'who',
"who's",
'whom',
'why',
"why's",
'with',
"won't",
'would',
"wouldn't",
'you',
"you'd",
"you'll",
"you're",
"you've",
'your',
'yours',
'yourself',
'yourselves',
})
Loading
Loading