Skip to content

fix(text-analyzer): count sentence ends only before whitespace or the end of text - #807

Merged
rng1995 merged 14 commits into
NVIDIA:mainfrom
mvanhorn:feat/text-analyzer-sentence-ends
Nov 23, 2026
Merged

rng1995 merged 14 commits into
NVIDIA:mainfrom
mvanhorn:feat/text-analyzer-sentence-ends

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

The text-analyzer reference skill split sentences with re.split(r"[.!?]+", text), so every period inside a token started a new sentence. On main (9757f986):

  • Open input.txt and report the totals. counted 2 sentences.
  • Pi is 3.14. Done! counted 3.
  • Ship v1.2.3 today. counted 3.
  • URLs and e-mail addresses were split the same way.

The skill's description names files such as input.txt as normal input, and SKILL.md documented only abbreviations as a known gap.

A run of ., ! or ? now ends a sentence only when whitespace or the end of the text follows it. Closing quotes or brackets right after the terminator still count, so He said "go." Then left. is 2 sentences. Text with no terminator still counts as 1, and empty or whitespace-only text counts as 0. The starter eval text still reports 3 sentences, Wait... what? still reports 2, and word, line, character, average-length and top-word results do not change. Abbreviations followed by a space (Dr. Smith) still split, and the Limitations entry in SKILL.md now says so.

--file used to crash with a traceback when given a directory (IsADirectoryError: [Errno 21] Is a directory) or an unreadable file. It now prints a message on stderr and exits 1 with no traceback:

  • Error: not a file: <path> for a directory.
  • Error: cannot read file: <path> (<reason>) for any other read error.

The existing Error: file not found: <path> is unchanged. The Troubleshooting table lists all three messages.

Verification

  • I am familiar with the Contributing Guidelines
  • Added or updated focused tests
  • Updated documentation for user-visible changes
  • Ran make lint
  • Ran make test
  • Ran make build
  • Did not add credentials, private datasets, or proprietary benchmark content

Release Impact

  • No user-visible release note needed
  • Updated CHANGELOG.md

Commands run

Platform: Linux 6.12.113+ x86_64, Python 3.13.5 (uv), on top of upstream 9757f986.

  • Branch version of tests/test_reference_text_analyzer.py run against untouched upstream/main: 7 failed, 12 passed. The failures are the file name, decimal, version, URL and e-mail sentence counts, plus the directory and unreadable-file cases.
  • This branch:
    • uv run pytest -n 0 tests/test_reference_text_analyzer.py tests/test_reference_skill_scripts.py: 37 passed.
    • uv run make lint: All checks passed.
    • uv run ruff format --check on the two touched Python files: 2 files already formatted.
    • uv run make build: Successfully built skillevaluator-1.0.1.tar.gz and skillevaluator-1.0.1-py3-none-any.whl.
    • PYTEST_ADDOPTS='--deselect tests/test_harbor_secure_docker_environment.py::test_windows_transfer_repeated_cancellation_reaps_process_and_removes_partial_output' uv run make test: 56 failed, 42477 passed, 168 skipped, 20 deselected, 297 errors. Untouched upstream/main with the same command: 56 failed, 42758 passed, 168 skipped, 20 deselected. The 56 failures are the same node IDs on both sides: 27 Docker-client tests in tests/test_harbor_secure_docker_environment.py, 15 Rich terminal-width assertions in tests/reporting/, 3 in tests/test_harbor_local_mode.py, 2 NeMo Gym backend tests, and 9 other tier3/harness/hygiene tests. The deselected Windows-transfer test hangs the serial run on untouched upstream/main too. The 297 errors were setup failures in tests/tier3/test_network_uploads_from_code_and_configs.py (OSError: could not create numbered dir under /tmp/pytest-of-box) because the machine ran out of inodes during the run. Re-running that module alone gave 2150 passed on this branch and 2150 passed on upstream/main.

Not verified: the unreadable-file test on Windows or as root. It is skipped there because permission bits do not apply.

Demo

demo

Full recording (MP4): https://raw.githubusercontent.com/mvanhorn/pr-demo-assets/main/osc-bulk-1119se/text-analyzer-sentence-ends/out.mp4

… end of text

The analyzer split on every run of .!? so periods inside file names, decimals,
and version numbers started extra sentences, and --file on a directory or an
unreadable path printed a traceback.

Sentence ends now count only when the terminator is followed by whitespace or
the end of the text, with optional closing quotes or brackets. For those path
problems --file now prints an error on stderr and exits 1.

Signed-off-by: Matt Van Horn <mvanhorn@users.noreply.github.com>

@chrisknvidia chrisknvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this is a clear improvement and the numbers people rely on do not move. I reproduced the overcount on main (input.txt, 3.14 and v1.2.3 each split), copied the branch tests onto main (7 fail, the five token cases plus the two --file error cases) and ran them here (37 passed with test_reference_skill_scripts.py). I also re-ran the analyzer on everything that quotes a sentence count: the starter fixture still gives 4 words, 2 lines, 27 characters, 2 sentences, the eval text still gives 3, and the evals2 texts give the same counts before and after. U.S., e.g., URLs, ellipses, ?! and a sentence ending at end of text all behave as documented, and the regex has no backtracking problem on long inputs.

One non-blocking note inline about curly closing quotes, which the new trailing class does not include. The --file error handling for directories and unreadable files is a nice touch and the Troubleshooting table matches the messages.

Ruff and format are clean, DCO is signed, CHANGELOG entry is under Unreleased.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 19:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The sentence-boundary pattern excludes typographic closing quotes, causing ordinary quoted prose to be undercounted compared with the previous behavior.

1 open issue
What changed in this PR

This PR improves sentence counting in the text-analyzer reference skill and adds clearer handling of file-input errors.

Changes:

  • Recognizes sentence endings before whitespace or end of text, preserving periods within filenames, decimals, versions, URLs, and email addresses.
  • Reports directory and file-read errors without tracebacks.
  • Adds focused regression tests and updates user documentation and release notes.
File Description
tests/​test_reference_text_analyzer.py Adds sentence-boundary and file-error regression tests.
src/​skillevaluator/​tier3/​reference_skills/​text-analyzer/​SKILL.md Clarifies sentence-detection limitations and file-error troubleshooting.
src/​skillevaluator/​tier3/​reference_skills/​text-analyzer/​scripts/​analyze_text.py Updates sentence splitting and handles directory and read errors.
CHANGELOG.md Records the sentence-counting and file-error improvements.

Review effort: Balanced

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @mvanhorn for this contribution. File names, decimals, versions, URLs and e-mail addresses now stay in one sentence, and the new --file messages behave as described. The tests pass here (37 passed with test_reference_skill_scripts.py), and ruff is clean.

I left two inline comments. The new pattern takes quadratic time on long runs of terminators, and a citation marker or Markdown emphasis after a terminator now merges two sentences. The curly-quote note already on line 18 still applies.

Treat curly double, curly single and angle closing quotes like straight
quotes after a terminator, and add regression cases next to the dotted
token tests.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 20:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The sentence-counting regex exhibits quadratic backtracking on long punctuation runs followed by text, allowing larger inputs to stall processing.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Restrict punctuation-run matching to prevent quadratic backtracking

src/​skillevaluator/​tier3/​reference_skills/​text-analyzer/​scripts/​analyze_text.py:19

A long punctuation run followed by a non-boundary character, such as "!" * n + "word", causes quadratic backtracking because the regex retries the failed run from every punctuation character. A standalone check took about 0.06, 0.25, and 0.99 seconds for 2,000, 4,000, and 8,000 characters, so larger inputs can stall sentence counting. Add a negative lookbehind to restrict matches to the start of each punctuation run while preserving the intended boundaries.

1 resolved since last review

Review effort: Balanced

Allow Markdown * and _ and numeric citations such as [1] between a
terminator and the following whitespace, so "1993.[1] The" and
"**done.** Next" count as two sentences again.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The sentence-boundary regex has quadratic runtime on punctuation runs followed by text, so large punctuation-heavy inputs can stall analysis.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Prevent quadratic regex backtracking on punctuation runs

src/​skillevaluator/​tier3/​reference_skills/​text-analyzer/​scripts/​analyze_text.py:19

This pattern takes quadratic time on a punctuation run followed by ordinary text, such as 'Start' + '.' * n + 'suffix', because matching restarts at every period. An isolated check took about 0.10 seconds for 2,000 periods and 1.56 seconds for 8,000, so larger punctuation-heavy inputs can stall analysis. Add (?<![.!?]) before [.!?]+ to prevent these retries while preserving the sentence-boundary behavior.

Review effort: Balanced

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
@mvanhorn
mvanhorn force-pushed the feat/text-analyzer-sentence-ends branch from ae783f2 to 1fda33e Compare November 19, 2026 21:19
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:19
A run of `.`, `!` or `?` followed by a non-boundary character made
re.split retry the pattern at every position inside the run, and each
attempt backtracked through the rest of it, so the time grew with the
square of the run length (40,000 dots took about a minute).

Start a match only at the first terminator of a run and take the run
possessively, so a failed run costs one pass. Sentence counts are
unchanged.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The analyzer can take quadratic time on long punctuation runs and emit an unhandled traceback when filesystem permission checks fail.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Possessive regex quantifiers require Python 3.11, causing immediate module failure on the advertised Python 3.10 target and breaking both inline and file analysis.

2 open issues
1 resolved since last review

Review effort: Balanced

@mvanhorn

Copy link
Copy Markdown
Contributor Author

@rng1995 the quadratic case is fixed in 855fd66: the (?<![.!?]) lookbehind now starts a match only at the first terminator of a run, and a timing test covers a 50,000-dot run followed by x. That commit also made the run possessive, which as you noted needs Python 3.11, so I'll push a follow-up that drops it back to a plain +, since the lookbehind alone keeps the split linear.

@mvanhorn
mvanhorn force-pushed the feat/text-analyzer-sentence-ends branch from bdf3d3d to 4f9b999 Compare November 20, 2026 14:11

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found a remaining sentence-count regression for Markdown footnotes.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 18:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the sentence-counting and CLI file-error handling changes.

No open issues 🎉

Review effort: Balanced

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 19:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The review reports no active issues with the sentence-counting and file-error-handling changes.

No open issues 🎉

Review effort: Balanced

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @mvanhorn for this contribution! I reviewed a9216419fb: I read the change and its callers, ran probes on the head, and checked the tests and docs. I left 2 inline comments (2 P3); please take a look. One more blocker outside the code: the DCO check is failing because the latest commit, a9216419fb ("Address review feedback"), has no Signed-off-by: trailer. Amend it with git commit --amend --signoff --no-edit and push with --force-with-lease; the other non-merge commits are already signed.

Comment thread tests/test_reference_text_analyzer.py Resolved
@mvanhorn
mvanhorn force-pushed the feat/text-analyzer-sentence-ends branch from a921641 to 5670d77 Compare November 20, 2026 20:35
Copilot AI balanced review requested due to automatic review settings November 20, 2026 22:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the sentence-counting and file-read error-handling improvements.

No open issues 🎉

Review effort: Balanced

…osers

Exercise the PermissionError branch on every platform by monkeypatching
Path.read_text, so a broken Windows directory fallback fails the suite.
Also cover sentence ends before ', ], } and the single guillemet.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 00:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the sentence-detection and file-error handling changes.

No open issues 🎉

Review effort: Balanced

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed at 9d9f5d8. The Markdown footnote case now keeps both sentences, the closing punctuation cases are covered, and unreadable paths still produce the correct diagnostic. All 58 focused tests passed. GitHub is awaiting authorization to run this fork's workflows.

@rng1995
rng1995 enabled auto-merge (squash) November 21, 2026 00:50
Copilot AI balanced review requested due to automatic review settings November 21, 2026 00:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the sentence-counting and file-error handling changes.

No open issues 🎉

Review effort: Balanced

@rng1995
rng1995 merged commit d10eeab into NVIDIA:main Nov 23, 2026
23 checks passed
@chrisknvidia chrisknvidia mentioned this pull request Nov 23, 2026
10 tasks done
@mvanhorn mvanhorn mentioned this pull request Nov 23, 2026
5 of 9 tasks
@chrisknvidia chrisknvidia mentioned this pull request Nov 23, 2026
6 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants