Registry Mirror
Skip to content

Conversation

@ac1982

@ac1982 ac1982 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #26.

Cosine(4).similarity("0 s", "foo bar") raises ZeroDivisionError: the raw string passes the length check, but whitespace normalization leaves it too short to produce a shingle. All-whitespace inputs have the same problem. The documented precomputed-profile entry point also divides by zero for empty profiles.

Calculate the norms once and return similarity 0 when either norm is zero. Route string comparisons through that same calculation. Identical strings retain the existing similarity-1 shortcut; ordinary nonempty profiles retain the cosine formula. The README now states the zero-norm convention.

Validation on Linux:

  • The initial regression set produced 11 ZeroDivisionError subtest errors before the fix.
  • Python 3.9.25: python -m pytest -q — all 21 tests pass, including empty/zero-count profiles, operand reversal, distance, and unchanged equality/nonempty controls.
  • CI's fatal Flake8 command passes; advisory lint reports 12 warnings in unchanged files. Both changed Python files pass the configured advisory lint without warnings.
  • git diff --check passes.

Other Python/OS matrix combinations were not run locally. Developed and tested by OpenAI Codex on behalf of the submitting account owner.

@luozhouyang
luozhouyang merged commit cec9f38 into luozhouyang:master Oct 31, 2026
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.

Cosine has divide by zero bug

2 participants