ADescribe · describe.text
Text exploration (words, n-grams, sentiment)
What is in a text column — how long are the texts, which words and phrases dominate, and how positive or negative are they?
Profiles a free-text column: text and word lengths, character classes, texts containing e-mail addresses, URLs or numbers, the most frequent words or phrases (n-grams, common English words left out), the terms that set each group apart (tf-idf by group), and VADER sentiment (Hutto & Gilbert 2014) per text and per group — identical in Python and R.
A real result
Example “Reviews words groups”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
59 texts, 8.2 words on average; top words: “good”, “great”, “coffee”, “comfortable”, “nothing”; mean sentiment 0.19.
- Texts
- 59
- Mean words
- 8.153
- Distinct tokens
- 282
- Mean sentiment
- 0.1880
Plots
View data
| Occurrences | Term |
|---|---|
| 5 | good |
| 5 | great |
| 4 | coffee |
| 4 | comfortable |
| 4 | nothing |
| 3 | best |
| 3 | headphones |
| 3 | love |
| 3 | re |
| 2 | absolutely |
| 2 | brew |
| 2 | carafe |
| 2 | com |
| 2 | cup |
| 2 | cushioning |
| 2 | example |
| 2 | fantastic |
| 2 | fine |
| 2 | happy |
| 2 | left |
View data
| tf-idf | product | Term |
|---|---|---|
| 0.0458 | coffee maker | coffee |
| 0.0229 | coffee maker | brew |
| 0.0229 | coffee maker | carafe |
| 0.0229 | coffee maker | morning |
| 0.0114 | coffee maker | annoying |
| 0.0114 | coffee maker | build |
| 0.0114 | coffee maker | burnt |
| 0.0114 | coffee maker | cheap |
| 0.0114 | coffee maker | clean |
| 0.0114 | coffee maker | constantly |
| 0.0297 | headphones | headphones |
| 0.0198 | headphones | com |
| 0.0198 | headphones | example |
| 0.0198 | headphones | much |
| 0.0198 | headphones | noise |
| 0.0198 | headphones | review |
| 0.0198 | headphones | sound |
| 0.0198 | headphones | worst |
| 0.0099 | headphones | amazing |
| 0.0099 | headphones | anc |
| 0.0234 | running shoes | cushioning |
| 0.0234 | running shoes | look |
| 0.0234 | running shoes | perfect |
| 0.0234 | running shoes | runs |
| 0.0234 | running shoes | shoes |
| 0.0234 | running shoes | size |
| 0.0117 | running shoes | average |
| 0.0117 | running shoes | beginners |
| 0.0117 | running shoes | blisters |
| 0.0117 | running shoes | break |
View data
| Words |
|---|
| 8 |
| 10 |
| 8 |
| 9 |
| 8 |
| 6 |
| 12 |
| 11 |
| 9 |
| 9 |
| 7 |
| 12 |
| 8 |
| 7 |
| 7 |
| 9 |
| 8 |
| 8 |
| 8 |
| 9 |
| 9 |
| 8 |
| 9 |
| 9 |
| 8 |
| 7 |
| 8 |
| 7 |
| 10 |
| 9 |
| 10 |
| 10 |
| 9 |
| 6 |
| 9 |
| 10 |
| 6 |
| 5 |
| 0 |
| 9 |
| 6 |
| 10 |
| 7 |
| 12 |
| 9 |
| 6 |
| 7 |
| 7 |
| 9 |
| 9 |
| 9 |
| 5 |
| 6 |
| 10 |
| 9 |
| 10 |
| 7 |
| 7 |
| 5 |
View data
| product | Sentiment (compound) |
|---|---|
| headphones | 0.8912 |
| headphones | −0.4939 |
| headphones | 0.7050 |
| headphones | 0.5984 |
| headphones | −0.8545 |
| headphones | −0.0920 |
| headphones | 0.8869 |
| headphones | 0.2023 |
| headphones | 0 |
| headphones | −0.6588 |
| headphones | 0.7783 |
| headphones | −0.6124 |
| headphones | 0.4226 |
| headphones | 0 |
| headphones | 0.8655 |
| headphones | −0.2732 |
| headphones | 0.2960 |
| headphones | 0.6249 |
| headphones | −0.3400 |
| headphones | 0.7474 |
| coffee maker | 0.6249 |
| coffee maker | 0 |
| coffee maker | 0.6808 |
| coffee maker | −0.0191 |
| coffee maker | 0.5719 |
| coffee maker | −0.7074 |
| coffee maker | 0.7184 |
| coffee maker | 0.2023 |
| coffee maker | −0.3392 |
| coffee maker | 0.8360 |
| coffee maker | 0 |
| coffee maker | −0.5748 |
| coffee maker | −0.4019 |
| coffee maker | −0.1695 |
| coffee maker | 0.4927 |
| coffee maker | −0.2500 |
| coffee maker | 0.6908 |
| coffee maker | −0.4939 |
| coffee maker | 0 |
| coffee maker | 0.6696 |
| running shoes | 0.8020 |
| running shoes | −0.5256 |
| running shoes | 0.3716 |
| running shoes | 0.7430 |
| running shoes | 0.2263 |
| running shoes | −0.4023 |
| running shoes | 0.5719 |
| running shoes | 0.6249 |
| running shoes | 0.7788 |
| running shoes | −0.2057 |
| running shoes | −0.5106 |
| running shoes | 0.7430 |
| running shoes | −0.4404 |
| running shoes | 0.2960 |
| running shoes | 0 |
| running shoes | 0.7713 |
| running shoes | 0 |
| running shoes | 0.7955 |
| running shoes | 0.2263 |
Assumptions
- English text — The stop list and VADER's lexicon are English; other languages need another tokenizer and sentiment model.
- Personal data — 1 text(s) contain an e-mail address and 1 a URL; consider marking review text sensitive in the data dictionary.
Texts
| Measure | Value |
|---|---|
| Texts | 59 |
| Empty texts | 1 |
| Characters per text (mean) | 47.542 |
| Characters per text (median) | 48 |
| Characters per text (max) | 77 |
| Words per text (mean) | 8.153 |
| Words per text (median) | 8 |
| Words per text (max) | 12 |
| Tokens (letter runs ≥ 2) | 462 |
| Distinct tokens | 282 |
| Type–token ratio | 0.6104 |
| Share of texts with an e-mail address | 0.0169 |
| Share of texts with a URL | 0.0169 |
| Share of texts with a digit | 0.0847 |
Most frequent words
| # | Term | Occurrences | Texts | Share of texts |
|---|---|---|---|---|
| 1 | good | 5 | 5 | 0.0847 |
| 2 | great | 5 | 5 | 0.0847 |
| 3 | coffee | 4 | 4 | 0.0678 |
| 4 | comfortable | 4 | 4 | 0.0678 |
| 5 | nothing | 4 | 3 | 0.0508 |
| 6 | best | 3 | 3 | 0.0508 |
| 7 | headphones | 3 | 3 | 0.0508 |
| 8 | love | 3 | 3 | 0.0508 |
| 9 | re | 3 | 3 | 0.0508 |
| 10 | absolutely | 2 | 2 | 0.0339 |
| 11 | brew | 2 | 2 | 0.0339 |
| 12 | carafe | 2 | 2 | 0.0339 |
| 13 | com | 2 | 2 | 0.0339 |
| 14 | cup | 2 | 2 | 0.0339 |
| 15 | cushioning | 2 | 2 | 0.0339 |
| 16 | example | 2 | 2 | 0.0339 |
| 17 | fantastic | 2 | 2 | 0.0339 |
| 18 | fine | 2 | 2 | 0.0339 |
| 19 | happy | 2 | 2 | 0.0339 |
| 20 | left | 2 | 2 | 0.0339 |
Common English words left out. Terms used in at least 2 text(s).
Characters
| Kind | Count | Share |
|---|---|---|
| Letters A–Z | 2,241 | 0.7989 |
| Digits | 14 | 0.0050 |
| Spaces and line breaks | 423 | 0.1508 |
| ASCII punctuation | 120 | 0.0428 |
| Other ASCII (control) | 0 | 0 |
| Non-ASCII (accents, symbols, emoji, other scripts) | 7 | 0.0025 |
By product
| product | Texts | Mean words | Mean sentiment | Positive | Negative |
|---|---|---|---|---|---|
| coffee maker | 20 | 7.900 | 0.1266 | 0.4500 | 0.3500 |
| headphones | 20 | 8.650 | 0.1847 | 0.5500 | 0.3500 |
| running shoes | 19 | 7.895 | 0.2561 | 0.6316 | 0.2632 |
Terms that set each product apart (tf-idf)
| product | # | Term | Occurrences | tf-idf |
|---|---|---|---|---|
| coffee maker | 1 | coffee | 4 | 0.0458 |
| coffee maker | 2 | brew | 2 | 0.0229 |
| coffee maker | 3 | carafe | 2 | 0.0229 |
| coffee maker | 4 | morning | 2 | 0.0229 |
| coffee maker | 5 | annoying | 1 | 0.0114 |
| coffee maker | 6 | build | 1 | 0.0114 |
| coffee maker | 7 | burnt | 1 | 0.0114 |
| coffee maker | 8 | cheap | 1 | 0.0114 |
| coffee maker | 9 | clean | 1 | 0.0114 |
| coffee maker | 10 | constantly | 1 | 0.0114 |
| headphones | 1 | headphones | 3 | 0.0297 |
| headphones | 2 | com | 2 | 0.0198 |
| headphones | 3 | example | 2 | 0.0198 |
| headphones | 4 | much | 2 | 0.0198 |
| headphones | 5 | noise | 2 | 0.0198 |
| headphones | 6 | review | 2 | 0.0198 |
| headphones | 7 | sound | 2 | 0.0198 |
| headphones | 8 | worst | 2 | 0.0198 |
| headphones | 9 | amazing | 1 | 0.0099 |
| headphones | 10 | anc | 1 | 0.0099 |
| running shoes | 1 | cushioning | 2 | 0.0234 |
| running shoes | 2 | look | 2 | 0.0234 |
| running shoes | 3 | perfect | 2 | 0.0234 |
| running shoes | 4 | runs | 2 | 0.0234 |
| running shoes | 5 | shoes | 2 | 0.0234 |
| running shoes | 6 | size | 2 | 0.0234 |
| running shoes | 7 | average | 1 | 0.0117 |
| running shoes | 8 | beginners | 1 | 0.0117 |
| running shoes | 9 | blisters | 1 | 0.0117 |
| running shoes | 10 | break | 1 | 0.0117 |
Each group's texts are one document: tf = share of the group's terms, idf = ln(groups / groups using the term).
- 1 of 60 rows were left out because a value was missing.
- 1 text(s) are empty.
- Sentiment: VADER compound score (−1 to 1); positive ≥ 0.05, negative ≤ −0.05, neutral in between (Hutto & Gilbert 2014).
59 texts, 8.2 words on average; top words: “good”, “great”, “coffee”, “comfortable”, “nothing”; mean sentiment 0.19.
- Texts
- 59
- Mean words
- 8.153
- Distinct tokens
- 282
- Mean sentiment
- 0.1880
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- English text — The stop list and VADER's lexicon are English; other languages need another tokenizer and sentiment model.
- Personal data — 1 text(s) contain an e-mail address and 1 a URL; consider marking review text sensitive in the data dictionary.
Texts
| Measure | Value |
|---|---|
| Texts | 59 |
| Empty texts | 1 |
| Characters per text (mean) | 47.542 |
| Characters per text (median) | 48 |
| Characters per text (max) | 77 |
| Words per text (mean) | 8.153 |
| Words per text (median) | 8 |
| Words per text (max) | 12 |
| Tokens (letter runs ≥ 2) | 462 |
| Distinct tokens | 282 |
| Type–token ratio | 0.6104 |
| Share of texts with an e-mail address | 0.0169 |
| Share of texts with a URL | 0.0169 |
| Share of texts with a digit | 0.0847 |
Most frequent words
| # | Term | Occurrences | Texts | Share of texts |
|---|---|---|---|---|
| 1 | good | 5 | 5 | 0.0847 |
| 2 | great | 5 | 5 | 0.0847 |
| 3 | coffee | 4 | 4 | 0.0678 |
| 4 | comfortable | 4 | 4 | 0.0678 |
| 5 | nothing | 4 | 3 | 0.0508 |
| 6 | best | 3 | 3 | 0.0508 |
| 7 | headphones | 3 | 3 | 0.0508 |
| 8 | love | 3 | 3 | 0.0508 |
| 9 | re | 3 | 3 | 0.0508 |
| 10 | absolutely | 2 | 2 | 0.0339 |
| 11 | brew | 2 | 2 | 0.0339 |
| 12 | carafe | 2 | 2 | 0.0339 |
| 13 | com | 2 | 2 | 0.0339 |
| 14 | cup | 2 | 2 | 0.0339 |
| 15 | cushioning | 2 | 2 | 0.0339 |
| 16 | example | 2 | 2 | 0.0339 |
| 17 | fantastic | 2 | 2 | 0.0339 |
| 18 | fine | 2 | 2 | 0.0339 |
| 19 | happy | 2 | 2 | 0.0339 |
| 20 | left | 2 | 2 | 0.0339 |
Common English words left out. Terms used in at least 2 text(s).
Characters
| Kind | Count | Share |
|---|---|---|
| Letters A–Z | 2,241 | 0.7989 |
| Digits | 14 | 0.0050 |
| Spaces and line breaks | 423 | 0.1508 |
| ASCII punctuation | 120 | 0.0428 |
| Other ASCII (control) | 0 | 0 |
| Non-ASCII (accents, symbols, emoji, other scripts) | 7 | 0.0025 |
By product
| product | Texts | Mean words | Mean sentiment | Positive | Negative |
|---|---|---|---|---|---|
| coffee maker | 20 | 7.900 | 0.1266 | 0.4500 | 0.3500 |
| headphones | 20 | 8.650 | 0.1847 | 0.5500 | 0.3500 |
| running shoes | 19 | 7.895 | 0.2561 | 0.6316 | 0.2632 |
Terms that set each product apart (tf-idf)
| product | # | Term | Occurrences | tf-idf |
|---|---|---|---|---|
| coffee maker | 1 | coffee | 4 | 0.0458 |
| coffee maker | 2 | brew | 2 | 0.0229 |
| coffee maker | 3 | carafe | 2 | 0.0229 |
| coffee maker | 4 | morning | 2 | 0.0229 |
| coffee maker | 5 | annoying | 1 | 0.0114 |
| coffee maker | 6 | build | 1 | 0.0114 |
| coffee maker | 7 | burnt | 1 | 0.0114 |
| coffee maker | 8 | cheap | 1 | 0.0114 |
| coffee maker | 9 | clean | 1 | 0.0114 |
| coffee maker | 10 | constantly | 1 | 0.0114 |
| headphones | 1 | headphones | 3 | 0.0297 |
| headphones | 2 | com | 2 | 0.0198 |
| headphones | 3 | example | 2 | 0.0198 |
| headphones | 4 | much | 2 | 0.0198 |
| headphones | 5 | noise | 2 | 0.0198 |
| headphones | 6 | review | 2 | 0.0198 |
| headphones | 7 | sound | 2 | 0.0198 |
| headphones | 8 | worst | 2 | 0.0198 |
| headphones | 9 | amazing | 1 | 0.0099 |
| headphones | 10 | anc | 1 | 0.0099 |
| running shoes | 1 | cushioning | 2 | 0.0234 |
| running shoes | 2 | look | 2 | 0.0234 |
| running shoes | 3 | perfect | 2 | 0.0234 |
| running shoes | 4 | runs | 2 | 0.0234 |
| running shoes | 5 | shoes | 2 | 0.0234 |
| running shoes | 6 | size | 2 | 0.0234 |
| running shoes | 7 | average | 1 | 0.0117 |
| running shoes | 8 | beginners | 1 | 0.0117 |
| running shoes | 9 | blisters | 1 | 0.0117 |
| running shoes | 10 | break | 1 | 0.0117 |
Each group's texts are one document: tf = share of the group's terms, idf = ln(groups / groups using the term).
- 1 of 60 rows were left out because a value was missing.
- 1 text(s) are empty.
- Sentiment: VADER compound score (−1 to 1); positive ≥ 0.05, negative ≤ −0.05, neutral in between (Hutto & Gilbert 2014).
Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.
Worked examples in the app
- Reviews words groupss5_reviews · group: product · text: review text
- Bigrams cases5_reviews · text: review text
- Corpus no stop no sentimentml_corpus · group: theme · text: text
- Trigramss5_reviews · text: review text
In the guides
Where it sits in the decision guides
- Explore: summary statistics, plots, outliers, skewness · Statistics Guide · Step 0 — Prepare your data
- EDA (correlation, distributions) · DS & ML Decision Guide · Describe — summarize and visualize
- Multimodal data exploration (text, image, video) · DS & ML Decision Guide · Describe — summarize and visualize
What you need
- Textcategorical · 1 column
- A column of free text (reviews, comments, notes).
- Compare groupscategorical / boolean · 0–1 column · 2–20 levels
- Optional — terms and sentiment per group.
Choices you make
statistics
- Termsdefault 1
Count single words, or phrases of two or three words.
- Single words(default) — Counts single words; pick it for the broad vocabulary.
- Two-word phrases — Counts pairs of adjacent words; pick it to see set phrases.
- Three-word phrases — Counts runs of three words; pick it for longer phrases, which need more text.
- Terms to listdefault 20
Number of most frequent terms to list.
- Minimum texts per termdefault 2
Terms used in fewer texts are not listed (one-off words are mostly noise).
- Sentiment (VADER)default on
A rule-based English sentiment score from −1 (negative) to 1 (positive) that understands negation, intensifiers, capitals, punctuation and emoji; best on short informal text.
options
- Leave out common English wordsdefault on
The, and, of … (a fixed list of 127 words; phrases containing one are left out too).
- Ignore casedefault on
Treat upper- and lower-case words as the same.
Checks and remedies
CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.
English text
The stop list and VADER's lexicon are English; for other languages turn sentiment off and read the n-grams with care.
Personal data
Free text often holds names, e-mail addresses and phone numbers; the flags count e-mail addresses and URLs so you can decide on a data-dictionary policy.
What it reports
- n_docs
- How many independent units the analysis used; it sets precision and power.
- n_empty
- How many independent units the analysis used; it sets precision and power.
- chars_mean
- Length of each text in characters, summarized as the mean, the median or the longest across all texts.
- chars_median
- Length of each text in characters, summarized as the mean, the median or the longest across all texts.
- chars_max
- Length of each text in characters, summarized as the mean, the median or the longest across all texts.
- words_mean
- Words in each text (runs of non-space characters), summarized as the mean, the median or the longest across all texts.
- words_median
- Words in each text (runs of non-space characters), summarized as the mean, the median or the longest across all texts.
- words_max
- Words in each text (runs of non-space characters), summarized as the mean, the median or the longest across all texts.
- words_total
- Words across all texts combined, counting runs of non-space characters.
- n_tokens
- How many independent units the analysis used; it sets precision and power.
- n_terms
- How many distinct words a text method kept after lower-casing, removing stop words and dropping words in too few or too many documents.
- ttr
- Distinct words divided by total words. Low values mean repetition, such as templated text.
- share_letters
- Share of all characters, pooled over texts, that are ASCII letters A–Z or a–z. Accented letters count as non-ASCII.
- share_digits
- Share of all characters, pooled over texts, that are the digits 0–9.
- share_whitespace
- Share of all characters, pooled over texts, that are spaces, tabs or line breaks.
- share_punctuation
- Share of all characters, pooled over texts, that are ASCII punctuation or symbols, such as . , ! ? @ or #.
- share_non_ascii
- Share of all characters, pooled over texts, outside ASCII: accented letters, curly quotes, emoji and other scripts.
- docs_email
- Share of texts that contain at least one e-mail address. Worth checking before the data are shared.
- docs_url
- Share of texts that contain a web address starting with http://, https:// or www.
- docs_number
- Share of texts that contain at least one digit 0–9, such as a number, a date or a code.
- top_terms_text
- Up to five of the most frequent terms, quoted and comma-separated. Says none when no term appears in enough texts.
- ngram_label
- Plain name of the unit counted in the top terms: words, two-word phrases or three-word phrases.
- sentiment
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- sentiment_mean
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- sentiment_sd
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- share_positive
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- share_neutral
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- share_negative
- A lexicon-based score from −1 (negative) to 1 (positive). A rough guide that misses sarcasm and context.
- n_groups
- How many independent units the analysis used; it sets precision and power.
Per group, level or term: n[<group>], sentiment_mean[<group>], term_count[<term>]
Python and R
Python and R agree to 10 decimals (relative tolerance 1e-10). Python uses pandas, numpy, codesplash; R uses nanoparquet, codesplashr.
Default traps between the two languages
- Tokens are letter runs: Python's [^\W\d_] also counts a few non-letter characters (superscript digits, fractions) as letters where R's \p{L} does not.
- VADER is the codesplash twin of vaderSentiment 3.3.2 in both engines (equal to it after vaderSentiment's rounding); scores are kept unrounded.
The exact script CodeSplash ran for example “Reviews words groups” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Text exploration (words, n-grams, sentiment)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method describe.text 1.0.1 · spec 20003684d61078fb
# Dataset: s5_reviews (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.text import explore as tx # words, tokens, n-grams, tf-idf by group (twins of codesplashr cs_text_*)
from codesplash.text import vader # VADER sentiment: port of vaderSentiment 3.3.2 (twin: cs_text_vader)
# %% Data
df = pd.read_parquet("tests/parity/fixtures/s5_reviews.parquet", columns=["review text", "product"])
n_input = len(df)
df["review text"] = pd.Categorical(df["review text"], categories=sorted(df["review text"].dropna().unique()))
df["product"] = pd.Categorical(df["product"], categories=["coffee maker", "headphones", "running shoes"]) # stored level order
df = df.dropna(subset=["review text", "product"]) # listwise deletion over the analysis columns
df["review text"] = df["review text"].cat.remove_unused_categories()
df["product"] = df["product"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["review text", "product"]) # infinite values stop the run (registry/errors.yaml)
# %% Texts
tc = "review text"
gc = "product"
g_levels = [lv for lv in ["coffee maker", "headphones", "running shoes"] if lv in set(df[gc].astype(str))] # stored level order
groups = df[gc].astype(str).tolist()
texts = df[tc].astype(str).tolist()
n = len(texts)
if n == 0:
raise ValueError(f"{tc} has no texts.")
n_gram = int("1")
top_n, min_docs = int(20), int(2)
stop, lowercase, with_sentiment = True, True, True
ngram_label = {1: "words", 2: "two-word phrases", 3: "three-word phrases"}[n_gram]
# %% Profile
chars = np.array([len(t) for t in texts], dtype=float)
nwords = np.array([len(tx.words(t)) for t in texts], dtype=float)
toks = [tx.tokens(t, lowercase) for t in texts]
grams = [tx.ngrams(tk, n_gram, stop) for tk in toks]
counts = tx.count_terms(grams)
top = tx.top_terms(counts, top_n, min_docs)
all_toks = [w for tk in toks for w in tk]
n_tokens, n_terms = len(all_toks), len(set(all_toks))
classes = {k: 0 for k in ("letters", "digits", "whitespace", "punctuation", "other_ascii", "non_ascii")}
for t in texts:
for k, v in tx.char_classes(t).items():
classes[k] += v
total_chars = int(chars.sum())
fl = [tx.flags(t) for t in texts]
comp = np.array([vader.scores(t)["compound"] for t in texts]) if with_sentiment else None
# %% Groups
group_rows, tfidf_rows = [], []
if gc is not None:
garr = np.array(groups)
for g in g_levels:
m = garr == g
row = {"group": g, "n": int(m.sum()), "words": float(nwords[m].mean())}
if comp is not None:
row.update({"sentiment": float(comp[m].mean()), "positive": float(np.mean(comp[m] >= 0.05)), "negative": float(np.mean(comp[m] <= -0.05))})
group_rows.append(row)
tfidf_rows = tx.tfidf_by_group({g: [grams[i] for i in range(n) if groups[i] == g] for g in g_levels}, top=min(top_n, 10), min_docs=1)
# %% Assumptions
assumptions = [
{"key": "language", "label": "English text", "status": "info",
"detail": "The stop list and VADER's lexicon are English; other languages need another tokenizer and sentiment model."},
{"key": "privacy", "label": "Personal data", "status": "warn" if any(f["email"] for f in fl) else "info",
"detail": f"{sum(f['email'] for f in fl)} text(s) contain an e-mail address and {sum(f['url'] for f in fl)} a URL; consider marking {tc} sensitive in the data dictionary."},
]
n_empty = int(np.sum(nwords == 0))
if n_empty:
warnings.append(f"{n_empty} text(s) are empty.")
# %% Result
share = (lambda k: classes[k] / total_chars) if total_chars else (lambda k: None)
values = {
"n_docs": n, "n_empty": n_empty, "chars_mean": float(chars.mean()), "chars_median": float(np.median(chars)), "chars_max": float(chars.max()),
"words_mean": float(nwords.mean()), "words_median": float(np.median(nwords)), "words_max": float(nwords.max()), "words_total": float(nwords.sum()),
"n_tokens": n_tokens, "n_terms": n_terms, "ttr": n_terms / n_tokens if n_tokens else None,
"share_letters": share("letters"), "share_digits": share("digits"), "share_whitespace": share("whitespace"),
"share_punctuation": share("punctuation"), "share_non_ascii": share("non_ascii"),
"docs_email": sum(f["email"] for f in fl) / n, "docs_url": sum(f["url"] for f in fl) / n, "docs_number": sum(f["number"] for f in fl) / n,
"top_terms_text": ", ".join(f"“{t}”" for t, _, _ in top[:5]) if top else "none (no term repeats often enough)",
"ngram_label": ngram_label, "sentiment": with_sentiment, "n_groups": len(g_levels),
"sentiment_mean": float(comp.mean()) if comp is not None else None,
"sentiment_sd": float(comp.std(ddof=1)) if comp is not None and n > 1 else None,
"share_positive": float(np.mean(comp >= 0.05)) if comp is not None else None,
"share_neutral": float(np.mean((comp > -0.05) & (comp < 0.05))) if comp is not None else None,
"share_negative": float(np.mean(comp <= -0.05)) if comp is not None else None,
}
for i, (t, c, d) in enumerate(top, start=1):
values[f"term_count[{t}]"] = c
for r in group_rows:
values[f"n[{r['group']}]"] = r["n"]
if comp is not None:
values[f"sentiment_mean[{r['group']}]"] = r["sentiment"]
tables = [
{"key": "summary", "title": "Texts", "columns": [{"key": "measure", "label": "Measure", "type": "string"}, {"key": "value", "label": "Value", "type": "number"}],
"rows": [
{"measure": "Texts", "value": n}, {"measure": "Empty texts", "value": n_empty},
{"measure": "Characters per text (mean)", "value": values["chars_mean"]}, {"measure": "Characters per text (median)", "value": values["chars_median"]},
{"measure": "Characters per text (max)", "value": values["chars_max"]}, {"measure": "Words per text (mean)", "value": values["words_mean"]},
{"measure": "Words per text (median)", "value": values["words_median"]}, {"measure": "Words per text (max)", "value": values["words_max"]},
{"measure": "Tokens (letter runs ≥ 2)", "value": n_tokens}, {"measure": "Distinct tokens", "value": n_terms},
{"measure": "Type–token ratio", "value": values["ttr"]}, {"measure": "Share of texts with an e-mail address", "value": values["docs_email"]},
{"measure": "Share of texts with a URL", "value": values["docs_url"]}, {"measure": "Share of texts with a digit", "value": values["docs_number"]}]},
{"key": "terms", "title": f"Most frequent {ngram_label}", "columns": [
{"key": "rank", "label": "#", "type": "integer"}, {"key": "term", "label": "Term", "type": "string"},
{"key": "count", "label": "Occurrences", "type": "integer"}, {"key": "docs", "label": "Texts", "type": "integer"},
{"key": "share", "label": "Share of texts", "type": "number"}],
"rows": [{"rank": i, "term": t, "count": c, "docs": d, "share": d / n} for i, (t, c, d) in enumerate(top, start=1)],
"note": ("Common English words left out. " if stop else "") + f"Terms used in at least {min_docs} text(s)."},
{"key": "characters", "title": "Characters", "columns": [
{"key": "kind", "label": "Kind", "type": "string"}, {"key": "count", "label": "Count", "type": "integer"}, {"key": "share", "label": "Share", "type": "number"}],
"rows": [{"kind": lab, "count": classes[k], "share": share(k)} for k, lab in (
("letters", "Letters A–Z"), ("digits", "Digits"), ("whitespace", "Spaces and line breaks"), ("punctuation", "ASCII punctuation"),
("other_ascii", "Other ASCII (control)"), ("non_ascii", "Non-ASCII (accents, symbols, emoji, other scripts)"))]},
]
if group_rows:
gcols = [{"key": "group", "label": gc, "type": "string"}, {"key": "n", "label": "Texts", "type": "integer"}, {"key": "words", "label": "Mean words", "type": "number"}]
if comp is not None:
gcols += [{"key": "sentiment", "label": "Mean sentiment", "type": "number"}, {"key": "positive", "label": "Positive", "type": "number"},
{"key": "negative", "label": "Negative", "type": "number"}]
tables.append({"key": "groups", "title": f"By {gc}", "columns": gcols, "rows": group_rows})
tables.append({"key": "tfidf", "title": f"Terms that set each {gc} apart (tf-idf)", "columns": [
{"key": "group", "label": gc, "type": "string"}, {"key": "rank", "label": "#", "type": "integer"}, {"key": "term", "label": "Term", "type": "string"},
{"key": "count", "label": "Occurrences", "type": "integer"}, {"key": "tfidf", "label": "tf-idf", "type": "number"}],
"rows": tfidf_rows, "note": "Each group's texts are one document: tf = share of the group's terms, idf = ln(groups / groups using the term)."})
charts = [cs.chart("terms", "ngram-bars", f"Most frequent {ngram_label}", {
"term": cs.col("Term", [t for t, _, _ in top], kind="categorical"), "count": cs.col("Occurrences", [c for _, c, _ in top])}, controls={"top": top_n})]
if tfidf_rows and len(g_levels) <= 9: # the chart draws at most 9 panels; the table has every group
charts.append(cs.chart("tfidf", "ngram-bars", f"Distinctive {ngram_label} by {gc}", {
"term": cs.col("Term", [r["term"] for r in tfidf_rows], kind="categorical"), "count": cs.col("tf-idf", [r["tfidf"] for r in tfidf_rows]),
"group": cs.col(gc, [r["group"] for r in tfidf_rows], kind="categorical", levels=g_levels)}, controls={"top": 10}))
charts.append(cs.chart("lengths", "histogram", "Words per text", {"x": cs.col("Words", nwords)}))
if comp is not None:
if gc is not None:
charts.append(cs.chart("sentiment", "raincloud", f"Sentiment by {gc}", {"y": cs.col("Sentiment (compound)", comp), "group": cs.col(gc, groups, levels=g_levels)}))
else:
charts.append(cs.chart("sentiment", "histogram", "Sentiment (VADER compound)", {"x": cs.col("Sentiment (compound)", comp)}))
summary = (f"{n} texts, {values['words_mean']:.1f} words on average; top {ngram_label}: {values['top_terms_text']}"
+ (f"; mean sentiment {values['sentiment_mean']:.2f}." if comp is not None else "."))
cs.publish({
"method": "describe.text",
"methodVersion": "1.0.1",
"title": "Text exploration",
"engine": cs.engine_info("pandas", "numpy"),
"n": n,
"summary": summary,
"statistics": [{"key": "n_docs", "label": "Texts", "value": n}, {"key": "words_mean", "label": "Mean words", "value": values["words_mean"]},
{"key": "n_terms", "label": "Distinct tokens", "value": n_terms}]
+ ([{"key": "sentiment_mean", "label": "Mean sentiment", "value": values["sentiment_mean"]}] if comp is not None else []),
"tables": tables,
"assumptions": assumptions,
"warnings": warnings,
"notes": ["Sentiment: VADER compound score (−1 to 1); positive ≥ 0.05, negative ≤ −0.05, neutral in between (Hutto & Gilbert 2014)."] if comp is not None else [],
"charts": charts,
"values": values,
})
The exact script CodeSplash ran for example “Reviews words groups” · R version 4.5.1 (2025-06-13)
# Text exploration (words, n-grams, sentiment)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method describe.text 1.0.1 · spec 20003684d61078fb
# Dataset: s5_reviews (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_text_* (words, tokens, n-grams, tf-idf by group) and cs_text_vader (port of vaderSentiment 3.3.2)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/s5_reviews.parquet", col_select = c("review text", "product")))
n_input <- nrow(df)
df[["review text"]] <- factor(df[["review text"]], levels = sort(unique(df[["review text"]]), method = "radix"))
df[["product"]] <- factor(df[["product"]], levels = c("coffee maker", "headphones", "running shoes")) # stored level order
df <- df[stats::complete.cases(df[c("review text", "product")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("review text", "product")) # infinite values stop the run (registry/errors.yaml)
# %% Texts
tc <- "review text"
gc <- "product"
groups <- as.character(df[[gc]])
g_levels <- intersect(c("coffee maker", "headphones", "running shoes"), groups) # stored level order
texts <- as.character(df[[tc]])
n <- length(texts)
if (n == 0) stop(sprintf("%s has no texts.", tc))
n_gram <- as.integer("1")
top_n <- as.integer(20)
min_docs <- as.integer(2)
stop_words <- TRUE
lowercase <- TRUE
with_sentiment <- TRUE
ngram_label <- c("words", "two-word phrases", "three-word phrases")[n_gram]
# %% Profile
chars <- nchar(texts, type = "chars")
nwords <- vapply(texts, function(t) length(cs_text_words(t)), numeric(1), USE.NAMES = FALSE)
toks <- lapply(texts, cs_text_tokens, lowercase = lowercase)
grams <- lapply(toks, cs_text_ngrams, n = n_gram, stop = stop_words)
counts <- cs_text_count_terms(grams)
top <- cs_text_top_terms(counts, top_n, min_docs)
all_toks <- unlist(toks, use.names = FALSE)
n_tokens <- length(all_toks)
n_terms <- length(unique(all_toks))
classes <- c(letters = 0, digits = 0, whitespace = 0, punctuation = 0, other_ascii = 0, non_ascii = 0)
for (t in texts) {
cc <- cs_text_char_classes(t)
for (k in names(classes)) classes[[k]] <- classes[[k]] + cc[[k]]
}
total_chars <- sum(chars)
fl <- lapply(texts, cs_text_flags)
n_email <- sum(vapply(fl, function(f) f$email, logical(1)))
n_url <- sum(vapply(fl, function(f) f$url, logical(1)))
n_number <- sum(vapply(fl, function(f) f$number, logical(1)))
comp <- if (with_sentiment) vapply(texts, function(t) cs_text_vader(t)$compound, numeric(1), USE.NAMES = FALSE) else NULL
# %% Groups
group_rows <- list()
tfidf_rows <- list()
if (!is.null(gc)) {
for (g in g_levels) {
m <- groups == g
row <- list(group = g, n = sum(m), words = mean(nwords[m]))
if (!is.null(comp)) row <- c(row, list(sentiment = mean(comp[m]), positive = mean(comp[m] >= 0.05), negative = mean(comp[m] <= -0.05)))
group_rows[[length(group_rows) + 1]] <- row
}
tfidf_rows <- cs_text_tfidf_by_group(stats::setNames(lapply(g_levels, function(g) grams[groups == g]), g_levels), top = min(top_n, 10), min_docs = 1)
}
# %% Assumptions
assumptions <- list(
list(key = "language", label = "English text", status = "info",
detail = "The stop list and VADER's lexicon are English; other languages need another tokenizer and sentiment model."),
list(key = "privacy", label = "Personal data", status = if (n_email > 0) "warn" else "info",
detail = sprintf("%d text(s) contain an e-mail address and %d a URL; consider marking %s sensitive in the data dictionary.", n_email, n_url, tc))
)
n_empty <- sum(nwords == 0)
if (n_empty > 0) warnings <- c(warnings, sprintf("%d text(s) are empty.", n_empty))
# %% Result
share <- function(k) if (total_chars > 0) classes[[k]] / total_chars else NA
values <- list(
n_docs = n, n_empty = n_empty, chars_mean = mean(chars), chars_median = stats::median(chars), chars_max = max(chars),
words_mean = mean(nwords), words_median = stats::median(nwords), words_max = max(nwords), words_total = sum(nwords),
n_tokens = n_tokens, n_terms = n_terms, ttr = if (n_tokens > 0) n_terms / n_tokens else NA,
share_letters = share("letters"), share_digits = share("digits"), share_whitespace = share("whitespace"),
share_punctuation = share("punctuation"), share_non_ascii = share("non_ascii"),
docs_email = n_email / n, docs_url = n_url / n, docs_number = n_number / n,
top_terms_text = if (nrow(top) > 0) paste(sprintf("“%s”", utils::head(top$term, 5)), collapse = ", ") else "none (no term repeats often enough)",
ngram_label = ngram_label, sentiment = with_sentiment, n_groups = length(g_levels),
sentiment_mean = if (!is.null(comp)) mean(comp) else NA,
sentiment_sd = if (!is.null(comp) && n > 1) stats::sd(comp) else NA,
share_positive = if (!is.null(comp)) mean(comp >= 0.05) else NA,
share_neutral = if (!is.null(comp)) mean(comp > -0.05 & comp < 0.05) else NA,
share_negative = if (!is.null(comp)) mean(comp <= -0.05) else NA
)
for (i in seq_len(nrow(top))) values[[sprintf("term_count[%s]", top$term[i])]] <- top$count[i]
for (r in group_rows) {
values[[sprintf("n[%s]", r$group)]] <- r$n
if (!is.null(comp)) values[[sprintf("sentiment_mean[%s]", r$group)]] <- r$sentiment
}
class_labels <- c(letters = "Letters A–Z", digits = "Digits", whitespace = "Spaces and line breaks", punctuation = "ASCII punctuation",
other_ascii = "Other ASCII (control)", non_ascii = "Non-ASCII (accents, symbols, emoji, other scripts)")
tables <- list(
list(key = "summary", title = "Texts", columns = list(list(key = "measure", label = "Measure", type = "string"), list(key = "value", label = "Value", type = "number")),
rows = list(
list(measure = "Texts", value = n), list(measure = "Empty texts", value = n_empty),
list(measure = "Characters per text (mean)", value = values$chars_mean), list(measure = "Characters per text (median)", value = values$chars_median),
list(measure = "Characters per text (max)", value = values$chars_max), list(measure = "Words per text (mean)", value = values$words_mean),
list(measure = "Words per text (median)", value = values$words_median), list(measure = "Words per text (max)", value = values$words_max),
list(measure = "Tokens (letter runs ≥ 2)", value = n_tokens), list(measure = "Distinct tokens", value = n_terms),
list(measure = "Type–token ratio", value = values$ttr), list(measure = "Share of texts with an e-mail address", value = values$docs_email),
list(measure = "Share of texts with a URL", value = values$docs_url), list(measure = "Share of texts with a digit", value = values$docs_number))),
list(key = "terms", title = sprintf("Most frequent %s", ngram_label),
columns = list(list(key = "rank", label = "#", type = "integer"), list(key = "term", label = "Term", type = "string"),
list(key = "count", label = "Occurrences", type = "integer"), list(key = "docs", label = "Texts", type = "integer"),
list(key = "share", label = "Share of texts", type = "number")),
rows = lapply(seq_len(nrow(top)), function(i) list(rank = i, term = top$term[i], count = top$count[i], docs = top$docs[i], share = top$docs[i] / n)),
note = paste0(if (stop_words) "Common English words left out. " else "", sprintf("Terms used in at least %d text(s).", min_docs))),
list(key = "characters", title = "Characters",
columns = list(list(key = "kind", label = "Kind", type = "string"), list(key = "count", label = "Count", type = "integer"), list(key = "share", label = "Share", type = "number")),
rows = lapply(names(class_labels), function(k) list(kind = class_labels[[k]], count = classes[[k]], share = share(k))))
)
if (length(group_rows) > 0) {
gcols <- list(list(key = "group", label = gc, type = "string"), list(key = "n", label = "Texts", type = "integer"), list(key = "words", label = "Mean words", type = "number"))
if (!is.null(comp)) gcols <- c(gcols, list(list(key = "sentiment", label = "Mean sentiment", type = "number"), list(key = "positive", label = "Positive", type = "number"),
list(key = "negative", label = "Negative", type = "number")))
tables[[length(tables) + 1]] <- list(key = "groups", title = sprintf("By %s", gc), columns = gcols, rows = group_rows)
tables[[length(tables) + 1]] <- list(key = "tfidf", title = sprintf("Terms that set each %s apart (tf-idf)", gc),
columns = list(list(key = "group", label = gc, type = "string"), list(key = "rank", label = "#", type = "integer"),
list(key = "term", label = "Term", type = "string"), list(key = "count", label = "Occurrences", type = "integer"),
list(key = "tfidf", label = "tf-idf", type = "number")),
rows = tfidf_rows, note = "Each group's texts are one document: tf = share of the group's terms, idf = ln(groups / groups using the term).")
}
charts <- list(cs_chart("terms", "ngram-bars", sprintf("Most frequent %s", ngram_label),
list(term = cs_col("Term", top$term, kind = "categorical"), count = cs_col("Occurrences", top$count)), controls = list(top = top_n)))
if (length(tfidf_rows) > 0 && length(g_levels) <= 9) { # the chart draws at most 9 panels; the table has every group
tv <- function(k) vapply(tfidf_rows, function(r) r[[k]], if (k == "tfidf") numeric(1) else character(1))
charts[[length(charts) + 1]] <- cs_chart("tfidf", "ngram-bars", sprintf("Distinctive %s by %s", ngram_label, gc),
list(term = cs_col("Term", tv("term"), kind = "categorical"), count = cs_col("tf-idf", tv("tfidf")),
group = cs_col(gc, tv("group"), kind = "categorical", levels = g_levels)), controls = list(top = 10))
}
charts[[length(charts) + 1]] <- cs_chart("lengths", "histogram", "Words per text", list(x = cs_col("Words", nwords)))
if (!is.null(comp)) {
charts[[length(charts) + 1]] <- if (!is.null(gc)) {
cs_chart("sentiment", "raincloud", sprintf("Sentiment by %s", gc), list(y = cs_col("Sentiment (compound)", comp), group = cs_col(gc, groups, levels = g_levels)))
} else {
cs_chart("sentiment", "histogram", "Sentiment (VADER compound)", list(x = cs_col("Sentiment (compound)", comp)))
}
}
summary <- sprintf("%d texts, %.1f words on average; top %s: %s%s", n, values$words_mean, ngram_label, values$top_terms_text,
if (!is.null(comp)) sprintf("; mean sentiment %.2f.", values$sentiment_mean) else ".")
cs_publish(list(
method = "describe.text",
methodVersion = "1.0.1",
title = "Text exploration",
engine = cs_engine_info("nanoparquet"),
n = n,
summary = summary,
statistics = c(list(list(key = "n_docs", label = "Texts", value = n), list(key = "words_mean", label = "Mean words", value = values$words_mean),
list(key = "n_terms", label = "Distinct tokens", value = n_terms)),
if (!is.null(comp)) list(list(key = "sentiment_mean", label = "Mean sentiment", value = values$sentiment_mean)) else list()),
tables = tables,
assumptions = assumptions,
warnings = warnings,
notes = if (!is.null(comp)) list("Sentiment: VADER compound score (−1 to 1); positive ≥ 0.05, negative ≤ −0.05, neutral in between (Hutto & Gilbert 2014).") else list(),
charts = charts,
values = values
))
Sources
- Hutto, C. J., & Gilbert, E. (2014). VADER: A parsimonious rule-based model for sentiment analysis of social media text. Proceedings of the International AAAI Conference on Web and Social Media, 8(1), 216–225.
- Silge, J., & Robinson, D. (2017). Text Mining with R: A Tidy Approach. O'Reilly (tf-idf by document group).
From the CodeSplash method registry · describe.text v1.0.1.