From bc345e870abde717cc637c96e50d9bf7ceda87c7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 1 Oct 2025 11:53:53 +0000 Subject: [PATCH] Update gemini_cookbook.py with checks from google.py --- .../tools/nblint/style/gemini_cookbook.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tools/tensorflow_docs/tools/nblint/style/gemini_cookbook.py b/tools/tensorflow_docs/tools/nblint/style/gemini_cookbook.py index b34c8193c1..e6ce9ced8d 100644 --- a/tools/tensorflow_docs/tools/nblint/style/gemini_cookbook.py +++ b/tools/tensorflow_docs/tools/nblint/style/gemini_cookbook.py @@ -43,6 +43,27 @@ from tensorflow_docs.tools.nblint.decorator import Options +def search_wordlist(wordlist, src_str): + """Search for wordlist entries in text and return set of found items. + + Args: + wordlist: Dict of word entries and recommendations to search in string. + src_str: String to search for word entries. + + Returns: + A dict that is a subset of entries from `wordlist` found in `src_str`. + """ + found_words = {} + for word in wordlist: + # Word-boundary and ignore between path separator '/'. + if re.search(rf"[^/]\b{word}\b[^/]", src_str, re.IGNORECASE): + alt_word = wordlist[word] + if not alt_word: + alt_word = "n/a" + found_words[word] = alt_word + return found_words + + # Acceptable copyright heading for notebooks following this style. copyrights_re = [ r"Copyright 20[1-9][0-9] The TensorFlow\s.*?\s?Authors", @@ -339,3 +360,48 @@ def button_r1_extra(args): ) else: return True + + +# Non-exhaustive list: {word: alt-word} (Use False if alt not provided.) +_SECOND_PERSON_WORDLIST = {"we": "you", "we're": "you are"} + + +@lint( + message="Prefer second person instead of first person: https://developers.google.com/style/person", + cond=Options.Cond.ALL) +def second_person(args): + """Test for first person usage in doc and recommend second person.""" + found_words = search_wordlist(_SECOND_PERSON_WORDLIST, args["cell_source"]) + if found_words: + words = ", ".join([f"{word} => {alt}" for word, alt in found_words.items()]) + fail( + f"Prefer second person instead of first person. Found: {words} in" + f" {args['cell_source']}" + ) + else: + return True + + +# Non-exhaustive list: {word: alt-word} (Use False if alt not provided.) +_INCLUSIVE_WORDLIST = { + "blacklist": "blocked", + "whitelist": "allowed", + "master": "primary", + "slave": "replica", +} + + +@lint( + message="Use inclusive language: https://developers.google.com/style/inclusive-documentation", + cond=Options.Cond.ALL) +def inclusive_language(args): + """Test for words found in inclusive wordlist and recommend alternatives.""" + found_words = search_wordlist(_INCLUSIVE_WORDLIST, args["cell_source"]) + if found_words: + words = ", ".join([f"{word} => {alt}" for word, alt in found_words.items()]) + fail( + f"Use inclusive language where possible and accurate. Found: {words} in" + f" {args['cell_source']}" + ) + else: + return True