Repository navigation
fix: do not count punctuation-only tokens as words - #1871
MohammadHijjawi97 wants to merge 3 commits into
Conversation
word_summary_vc strips punctuation from every whitespace-separated token, so tokens such as "-" or "..." became empty strings and were counted as a word. For text like "Star Wars - Episode IV" the empty word could become the most common entry in the Words table. Drop the empty tokens before counting.
fabclmnt
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
Yes indeed, punctuation should not be included and the fix works for “-” and “...”. The root cause is that string. Nevertheless, punctuation is ASCII-only, so Unicode punctuation (em/en dashes, “…”, « », “ ”) still ends up as standalone “words” — “Star Wars — Episode IV” reproduces the original bug.
Could you strip with a Unicode-aware pattern instead, e.g. .str.replace(r"^[\W_]+|[\W_]+$", "", regex=True) and then drop empty tokens? That keeps inner punctuation (“don’t”, “e-mail”) as today.
Please also add test cases for “—”, “–”, “…” and a column where every value is punctuation-only (expect no word_counts), and update the TODO: remove punctuation
string.punctuation is ASCII-only, so em dashes, en dashes, ellipses and curly or angle quotes were still counted as words. Strip leading and trailing non-word characters with a Unicode-aware pattern instead, and keep inner punctuation such as "don't" and "e-mail".
|
Thanks for the review. I switched the stripping to I added tests for —, –, …, « », “ ” and for punctuation-only columns (no One thing to flag: |
To be able to merge a pull request, there are a few checks:
[x] the
blackstyle should be enforced (make lint)[ ] the docs should be regenerated (make docs)
[ ] the pull request should pass the tests (make test)
[ ] the examples should be generated (make examples)
Problem
word_summary_vcsplits values on whitespace and strips punctuation from each token. Tokens made only of punctuation (-,--,...,&) become empty strings and are counted as a word. The empty entry then shows up in the Words table of Categorical and Text columns, often at the top:Solution
Drop empty tokens after stripping, before counting.
Tests
Added
test_word_summary_vc_ignores_punctuation_only_tokensintests/unit/test_pandas/test_describe_categorical_pandas.py; it fails before the change and passes after. Rantests/unit/test_pandasandtests/unit/test_describe.py.