Analyzing Text¶
Language has been the heartbeat of Taters from day one. My background is heavily influenced by this area, and so are the project's roots: the psychology of verbal behavior. What follows is some general information to help you grapple with the idea of using text for psychometric purposes regardless of the analytic method used, ranging from dictionary-based analyses to transformer-based methods, and everything in-between and beyond.
If you're new to this space and want a single on-ramp, start here:
- Kennedy, B., Ashokkumar, A., Boyd, R. L., & Dehghani, M. (2022). Text analysis for Psychology: Methods, principles, and practices. In M. Dehghani & R. L. Boyd (Eds.), The handbook of language analysis in psychology (pp. 3–62). The Guilford Press.
For broader field context and the "verbal behavior" perspective:
-
Boyd, R. L., & Schwartz, H. A. (2021). Natural language analysis and the psychology of verbal behavior: The past, present, and future states of the field. Journal of Language and Social Psychology, 40(1), 21–41. https://doi.org/10.1177/0261927X20967028
-
Boyd, R. L., & Markowitz, D. M. (2025). Verbal behavior and the future of social science. American Psychologist, 80(3), 411–433. https://doi.org/10.1037/amp0001319
Using text analysis methods in Taters¶
Most methods accept analysis-ready CSVs (text_id,text
), raw CSVs (with text_cols
/ optional id_cols
/ optional group_by
), or a folder of .txt
. Below, you'll find short descriptions and information drawn from the full API. Note that this page is really intended as a starting point to wrap your head around some core concepts — it is not exhaustive in the least, and necessarily reflects my own perspectives, knowledge, and limitations of both.
Dictionary-based analyses¶
What & why¶
Dictionary methods treat language as evidence of attention and style: the words we use (including the "little" ones) systematically reflect cognitive, affective, and social processes. Decades of work show that transparent category counts can be highly diagnostic — especially with function words and psychologically motivated lexicons. A few starting points:
-
Pennebaker, J. W. (2011). The secret life of pronouns: What our words say about us. Bloomsbury.
-
Tausczik, Y. R., & Pennebaker, J. W. (2010). The psychological meaning of words: LIWC and computerized text analysis methods. Journal of Language and Social Psychology, 29(1), 24–54. https://doi.org/10.1177/0261927X09351676
-
Boyd, R. L. (2017). Psychological text analysis in the digital humanities. In S. Hai-Jew (Ed.), Data Analytics in Digital Humanities (pp. 161–189). Springer. https://doi.org/10.1007/978-3-319-54499-1_7
API: analyze texts with an arbitrary number of dictionaries¶
Compute LIWC-style dictionary features for text rows and write a wide features CSV.
The function supports exactly one of three input modes:
analysis_csv
— Use a prebuilt file with columnstext_id
andtext
.csv_path
— Gather text from an arbitrary CSV usingtext_cols
(and optionalid_cols
/group_by
) to produce an analysis-ready file.txt_dir
— Gather text from a folder of.txt
files.
If out_features_csv
is omitted, the default output path is
./features/dictionary/<analysis_ready_filename>
. Multiple dictionaries are supported;
passing a directory discovers all .dic
, .dicx
, and .csv
dictionary files
recursively in a stable order. Global columns (e.g., word counts, punctuation) are emitted
once (from the first dictionary) and each dictionary contributes a namespaced block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_path
|
str or Path
|
Source CSV to gather from. Mutually exclusive with |
None
|
txt_dir
|
str or Path
|
Folder containing |
None
|
analysis_csv
|
str or Path
|
Prebuilt analysis-ready CSV with exactly two columns: |
None
|
out_features_csv
|
str or Path
|
Output file path. If |
None
|
overwrite_existing
|
bool
|
If |
False
|
dict_paths
|
Sequence[str or Path]
|
One or more dictionary inputs (files or directories). Supported extensions:
|
required |
encoding
|
str
|
Text encoding used for reading/writing CSV files. |
"utf-8-sig"
|
text_cols
|
Sequence[str]
|
When gathering from a CSV, name(s) of the column(s) containing text. |
("text",)
|
id_cols
|
Sequence[str] or None
|
Optional ID columns to carry into grouping when gathering from CSV. |
None
|
mode
|
(concat, separate)
|
Gathering behavior when multiple text columns are provided. |
"concat"
|
group_by
|
Sequence[str] or None
|
Optional grouping keys used during CSV gathering (e.g., |
None
|
delimiter
|
str
|
Delimiter for reading/writing CSV files. |
","
|
joiner
|
str
|
Separator used when concatenating multiple text chunks in |
" "
|
num_buckets
|
int
|
Number of temporary hash buckets used during scalable CSV gathering. |
512
|
max_open_bucket_files
|
int
|
Maximum number of bucket files kept open concurrently during gathering. |
64
|
tmp_root
|
str or Path or None
|
Root directory for temporary gathering artifacts. |
None
|
recursive
|
bool
|
When gathering from a text folder, recurse into subdirectories. |
True
|
pattern
|
str
|
Glob pattern for selecting text files when gathering from a folder. |
"*.txt"
|
id_from
|
(stem, name, path)
|
How to derive |
"stem"
|
include_source_path
|
bool
|
If |
True
|
relative_freq
|
bool
|
Emit relative frequencies instead of raw counts, when supported by the dictionary engine. |
True
|
drop_punct
|
bool
|
Drop punctuation prior to analysis (dictionary-dependent). |
True
|
rounding
|
int
|
Decimal places to round numeric outputs. Use |
4
|
retain_captures
|
bool
|
Pass-through flag to the underlying analyzer to retain capture groups, if applicable. |
False
|
wildcard_mem
|
bool
|
Pass-through optimization flag for wildcard handling in the analyzer. |
True
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written features CSV. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If input files/folders or any dictionary file cannot be found. |
ValueError
|
If input modes are misconfigured (e.g., multiple sources provided or none), required columns are missing from the analysis-ready CSV, or unsupported dictionary extensions are encountered. |
Examples:
Run on a transcript CSV, grouped by speaker:
>>> analyze_with_dictionaries(
... csv_path="transcripts/session.csv",
... text_cols=["text"], id_cols=["speaker"], group_by=["speaker"],
... dict_paths=["dictionaries/liwc/LIWC-22 Dictionary (2022-01-27).dicx"]
... )
PosixPath('.../features/dictionary/session.csv')
Notes
If overwrite_existing
is False
and the output exists, the existing file path
is returned without recomputation.
Source code in src\taters\text\analyze_with_dictionaries.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
Lexical richness analyses¶
What & why¶
Lexical richness/diversity asks: how varied is a speaker's vocabulary use? Classic measures (e.g., TTR, RTTR/CTTR, Herdan's C, Yule's K/I) capture type–token structure, while modern, length-robust metrics (MTLD, MATTR, HD-D, VOCD/D) reduce text-length bias and are widely used in psycholinguistics and language assessment. In Taters, these metrics are computed per text (or per group such as source,speaker
) with tokenization and reproducibility controls (window sizes, draws, seeds). A few helpful references and codebases:
-
The original
lexicalrichness
package (reference implementation and API ideas). https://github.com/LSYS/lexicalrichness -
McCarthy, P. M., & Jarvis, S. (2007). vocd: A theoretical and empirical evaluation. Language Testing, 24(4), 459–488. https://doi.org/10.1177/0265532207080767
-
McCarthy, P. M., & Jarvis, S. (2010). MTLD, vocd-D, and HD-D: A validation study of sophisticated approaches to lexical diversity assessment. Behavior Research Methods, 42(2), 381–392. https://doi.org/10.3758/BRM.42.2.381
Tip: Results can vary with tokenization choices (e.g., handling of hyphens) and window/draw parameters. For strict comparability with prior work or other toolkits, keep those settings consistent and document them in your analysis.
API: analyze texts with lexical diversity metrics¶
Compute lexical richness/diversity metrics for each text row and write a features CSV. Draws heavily from https://github.com/LSYS/lexicalrichness but makes several key changes with the goals of minimizing dependencies, attempting to make some speed optimizations with grid search instead of precise curve specifications, and making some principled decisions around punctuation/hyphenization that differ from the original Note that these decisions are not objectively "better" than the original but, instead, reflect my own experiences/intuitions about what makes sense.
This function accepts (a) an analysis-ready CSV (with columns text_id,text
), (b) a
raw CSV plus instructions for gathering/aggregation, or (c) a folder of .txt
files.
For each resulting row of text, it tokenizes words and computes a suite of classical
lexical richness measures (e.g., TTR, Herdan's C, Yule's K, MTLD, MATTR, HDD, VOCD).
Results are written as a wide CSV whose rows align with the rows in the analysis-ready
table (or the gathered group_by
rows), preserving any non-text metadata columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_path
|
str or Path
|
Source CSV to gather from. Use with |
None
|
txt_dir
|
str or Path
|
Folder of |
None
|
analysis_csv
|
str or Path
|
Existing analysis-ready CSV with columns |
None
|
out_features_csv
|
str or Path
|
Output CSV path. If omitted, defaults to
|
None
|
overwrite_existing
|
bool
|
If |
False
|
encoding
|
str
|
Encoding for reading/writing CSVs. |
"utf-8-sig"
|
text_cols
|
sequence of str
|
Text column(s) to use when |
("text",)
|
id_cols
|
sequence of str
|
Columns to carry through unchanged into the analysis-ready CSV prior to analysis
(e.g., |
None
|
mode
|
('concat', 'separate')
|
Gathering behavior when multiple |
"concat"
|
group_by
|
sequence of str
|
If provided, texts are grouped by these columns before analysis (e.g.,
|
None
|
delimiter
|
str
|
CSV delimiter used for input and output. |
","
|
joiner
|
str
|
String used to join text fields when |
" "
|
num_buckets
|
int
|
Internal streaming/gather parameter to control temporary file bucketing (passed through to the gatherer). |
512
|
max_open_bucket_files
|
int
|
Maximum number of temporary files simultaneously open during gathering. |
64
|
tmp_root
|
str or Path
|
Temporary directory root for the gatherer. Defaults to a system temp location. |
None
|
recursive
|
bool
|
When |
True
|
pattern
|
str
|
Glob pattern for discovering text files under |
"*.txt"
|
id_from
|
('stem', 'name', 'path')
|
How to construct |
"stem"
|
include_source_path
|
bool
|
When |
True
|
msttr_window
|
int
|
Window size for MSTTR (Mean Segmental TTR). Must be smaller than the number of tokens in the text to produce a value. |
100
|
mattr_window
|
int
|
Window size for MATTR (Moving-Average TTR). Must be smaller than the number of tokens. |
100
|
mtld_threshold
|
float
|
MTLD threshold for factor completion. A higher threshold yields shorter factors and typically lower MTLD values; the default follows common practice. |
0.72
|
hdd_draws
|
int
|
Sample size |
42
|
vocd_ntokens
|
int
|
Maximum sample size used to estimate VOCD (D). For each |
50
|
vocd_within_sample
|
int
|
Number of random samples drawn per |
100
|
vocd_iterations
|
int
|
Repeat-estimate count for VOCD. The best-fit D from each repetition is averaged. |
3
|
vocd_seed
|
int
|
Seed for the VOCD random sampler (controls reproducibility across runs). |
42
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written features CSV. |
Notes
Tokenization and preprocessing.
Texts are lowercased, digits are removed, and punctuation characters are
replaced with spaces prior to tokenization. As a result, hyphenated forms such
as "state-of-the-art"
will be split into separate tokens ("state"
, "of"
,
"the"
, "art"
). This choice yields robust behavior across corpora but can
produce different numeric results than implementations that remove hyphens
(treating "state-of-the-art"
as a single token). If you require strict parity
with a hyphen-removal scheme, adapt the internal preprocessing accordingly.
Metrics.
The following measures are emitted per row (values are None
when a text is
too short to support the computation):
- ttr
: Type-Token Ratio (|V| / N)
- rttr
: Root TTR (|V| / sqrt(N))
- cttr
: Corrected TTR (|V| / sqrt(2N))
- herdan_c
: Herdan's C (log |V| / log N)
- summer_s
: Summer's S (log log |V| / log log N)
- dugast
: Dugast's U ((log N)^2 / (log N − log |V|))
- maas
: Maas a^2 ((log N − log |V|) / (log N)^2)
- yule_k
: Yule's K (dispersion of frequencies; higher = less diverse)
- yule_i
: Yule's I (inverse of K, scaled)
- herdan_vm
: Herdan's Vm
- simpson_d
: Simpson's D (repeat-probability across tokens)
- msttr_{msttr_window}
: Mean Segmental TTR over fixed segments
- mattr_{mattr_window}
: Moving-Average TTR over a sliding window
- mtld_{mtld_threshold}
: Measure of Textual Lexical Diversity (bidirectional)
- hdd_{hdd_draws}
: HD-D (expected proportion of types in a sample of size hdd_draws
)
- vocd_{vocd_ntokens}
: VOCD (D) estimated by fitting TTR(N) to a theoretical curve
VOCD estimation. VOCD is fit without external optimization libraries: the function performs a coarse grid search over candidate D values (minimizing squared error between observed mean TTRs and a theoretical TTR(N; D) curve) for multiple repetitions, then averages the best D across repetitions. This generally tracks SciPy-based curve fits closely; you can widen the search grid or add a fine local search if tighter agreement is desired.
Output shape.
The output CSV includes all non-text columns from the analysis-ready CSV
(e.g., text_id
, plus any id_cols
) and appends one column per metric. When
a group-by is specified during gathering, each output row corresponds to one
group (e.g., one (source, speaker)
).
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If |
ValueError
|
If none or more than one of |
Examples:
Analyze an existing analysis-ready CSV (utterance-level):
>>> analyze_lexical_richness(
... analysis_csv="transcripts_all.csv",
... out_features_csv="features/lexical-richness.csv",
... overwrite_existing=True,
... )
Gather from a transcript CSV and aggregate per (source, speaker):
>>> analyze_lexical_richness(
... csv_path="transcripts/session.csv",
... text_cols=["text"],
... id_cols=["source", "speaker"],
... group_by=["source", "speaker"],
... mode="concat",
... out_features_csv="features/lexical-richness.csv",
... )
See Also
analyze_readability : Parallel analyzer producing readability indices. csv_to_analysis_ready_csv : Helper for building the analysis-ready table from a CSV. txt_folder_to_analysis_ready_csv : Helper for building the analysis-ready table from a folder of .txt files.
Source code in src\taters\text\analyze_lexical_richness.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
|
options: members_order: alphabetical show_source: true
Transformer-based analyses¶
Archetypes (theory-driven, embedding-based)¶
Archetype analysis encodes each text with a Sentence-Transformers model and measures similarity to curated seed phrases (one CSV per construct). The model handles nuance; your archetype definitions provide direction in embedding space. Recent examples:
-
Varadarajan, V., Lahnala, A., Ganesan, A. V., Dey, G., Mangalik, S., Bucur, A.-M., Soni, N., Rao, R., Lanning, K., Vallejo, I., Flek, L., Schwartz, H. A., Welch, C., & Boyd, R. L. (2024). Archetypes and entropy: Theory-driven extraction of evidence for suicide risk. In Proceedings of CLPsych 2024 (pp. 278–291). https://aclanthology.org/2024.clpsych-1.28
-
Lahnala, A., Varadarajan, V., Flek, L., Schwartz, H. A., & Boyd, R. L. (2025). Unifying the extremes: Developing a unified model for detecting and predicting extremist traits and radicalization. Proceedings of the International AAAI Conference on Web and Social Media, 19, 1051–1067. https://doi.org/10.1609/icwsm.v19i1.35860
-
Soni, N., Nilsson, A. H., Mahwish, S., Varadarajan, V., Schwartz, H. A., & Boyd, R. L. (2025). Who we are, where we are: Mental health at the intersection of person, situation, and large language models. In Proceedings of CLPsych 2025 (pp. 300–313). https://aclanthology.org/2025.clpsych-1.27/
-
Atari, M., Omrani, A., & Dehghani, M. (2023). Contextualized construct representation: Leveraging psychometric scales to advance theory-driven text analysis. OSF. https://doi.org/10.31234/osf.io/m93pd
-
Chen, Y., Li, S., Li, Y., & Atari, M. (2024). Surveying the dead minds: Historical-psychological text analysis with contextualized construct representation (CCR) for classical Chinese. In Y. Al-Onaizan, M. Bansal, & Y.-N. Chen (Eds.), Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing (pp. 2597–2615). Association for Computational Linguistics. https://doi.org/10.18653/v1/2024.emnlp-main.151
-
Simchon, A., Hadar, B., & Gilead, M. (2023). A computational text analysis investigation of the relation between personal and linguistic agency. Communications Psychology, 1(1), 23. https://doi.org/10.1038/s44271-023-00020-1
API: Analyze texts with/for archetypes¶
Compute archetype scores for text rows and write a wide, analysis-ready features CSV.
This function supports three input modes:
analysis_csv
— Use a prebuilt CSV with exactly two columns:text_id
andtext
.csv_path
— Gather text from an arbitrary CSV by specifyingtext_cols
(and optionallyid_cols
andgroup_by
) to construct an analysis-ready CSV on the fly.txt_dir
— Gather text from a folder of.txt
files.
Archetype scoring is delegated to a middle layer that embeds text with a Sentence-Transformers
model and evaluates cosine similarity to one or more archetype CSVs. If out_features_csv
is
omitted, the default path is ./features/archetypes/<analysis_ready_filename>
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_path
|
str or Path
|
Source CSV for gathering. Mutually exclusive with |
None
|
txt_dir
|
str or Path
|
Folder of |
None
|
analysis_csv
|
str or Path
|
Precomputed analysis-ready CSV containing exactly the columns |
None
|
out_features_csv
|
str or Path
|
Output path for the features CSV. If |
None
|
overwrite_existing
|
bool
|
If |
False
|
archetype_csvs
|
Sequence[str or Path]
|
One or more archetype CSVs (name → seed phrases). Directories are allowed and expanded
recursively to all |
required |
encoding
|
str
|
Text encoding for CSV I/O. |
"utf-8-sig"
|
delimiter
|
str
|
Field delimiter for CSV I/O. |
","
|
text_cols
|
Sequence[str]
|
When gathering from a CSV: column(s) that contain text. Used only if |
("text",)
|
id_cols
|
Sequence[str]
|
When gathering from a CSV: optional ID columns to carry into grouping (e.g., |
None
|
mode
|
(concat, separate)
|
Gathering behavior when multiple |
"concat"
|
group_by
|
Sequence[str]
|
Optional grouping keys used during gathering (e.g., |
None
|
joiner
|
str
|
Separator used when concatenating multiple text chunks. |
" "
|
num_buckets
|
int
|
Number of temporary hash buckets used for scalable CSV gathering. |
512
|
max_open_bucket_files
|
int
|
Maximum number of bucket files to keep open concurrently during gathering. |
64
|
tmp_root
|
str or Path
|
Root directory for temporary files used by gathering. |
None
|
recursive
|
bool
|
When gathering from a text folder, whether to recurse into subdirectories. |
True
|
pattern
|
str
|
Filename glob used when gathering from a text folder. |
"*.txt"
|
id_from
|
(stem, name, path)
|
How to derive the |
"stem"
|
include_source_path
|
bool
|
Whether to include the absolute source path as an additional column when gathering from a text folder. |
True
|
model_name
|
str
|
Sentence-Transformers model used to embed text for archetype scoring. |
"sentence-transformers/all-roberta-large-v1"
|
mean_center_vectors
|
bool
|
If |
True
|
fisher_z_transform
|
bool
|
If |
False
|
rounding
|
int
|
Number of decimal places to round numeric outputs. Use |
4
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written features CSV. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If an input file or folder does not exist, or an archetype CSV path is invalid. |
ValueError
|
If required arguments are incompatible or missing (e.g., no input mode chosen),
or if the analysis-ready CSV lacks |
Examples:
Run on a transcript CSV, grouped by speaker:
>>> analyze_with_archetypes(
... csv_path="transcripts/session.csv",
... text_cols=["text"],
... id_cols=["speaker"],
... group_by=["speaker"],
... archetype_csvs=["dictionaries/archetypes"],
... model_name="sentence-transformers/all-roberta-large-v1",
... )
PosixPath('.../features/archetypes/session.csv')
Notes
If out_features_csv
exists and overwrite_existing=False
, the existing path is returned
without recomputation. Directories passed in archetype_csvs
are expanded recursively to
all .csv
files and deduplicated before scoring.
Source code in src\taters\text\analyze_with_archetypes.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
Sentence embeddings (general semantic features)¶
When you want task-agnostic semantic features (for clustering, similarity, regression/classification), use taters.text.extract_sentence_embeddings
. Taters tokenizes into sentences, embeds each sentence, and averages to a single vector per row. Optional L2 normalization makes cosine comparisons straightforward. Example applications:
-
Kjell, O. N. E., Sikström, S., Kjell, K., & Schwartz, H. A. (2022). Natural language analyzed with AI-based transformers predict traditional subjective well-being measures approaching the theoretical upper limits in accuracy. Scientific Reports, 12, 3918. https://doi.org/10.1038/s41598-022-07520-w
-
Nilsson, A. H., Schwartz, H. A., Rosenthal, R. N., McKay, J. R., Vu, H., Cho, Y.-M., Mahwish, S., Ganesan, A. V., & Ungar, L. (2024). Language-based EMA assessments help understand problematic alcohol consumption. PLOS ONE, 19(3), e0298300. https://doi.org/10.1371/journal.pone.0298300
API: Extract sentence embeddings¶
Average sentence embeddings per row of text and write a wide features CSV.
Supports three mutually exclusive input modes:
analysis_csv
— Use a prebuilt file with columnstext_id
andtext
.csv_path
— Gather from a CSV usingtext_cols
(and optionalid_cols
/group_by
) to build an analysis-ready CSV.txt_dir
— Gather from a folder of.txt
files.
For each row, the text is split into sentences (NLTK if available; otherwise a regex fallback). Each sentence is embedded with a Sentence-Transformers model and the vectors are averaged into one row-level embedding. Optionally, vectors are L2-normalized. The output CSV schema is:
text_id, e0, e1, ..., e{D-1}
If out_features_csv
is omitted, the default is
./features/sentence-embeddings/<analysis_ready_filename>
. When
overwrite_existing
is False
and the output exists, the function
returns the existing path without recomputation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_path
|
str or Path
|
Source CSV to gather from. Mutually exclusive with |
None
|
txt_dir
|
str or Path
|
Folder of |
None
|
analysis_csv
|
str or Path
|
Prebuilt analysis-ready CSV containing exactly |
None
|
out_features_csv
|
str or Path
|
Output features CSV path. If |
None
|
overwrite_existing
|
bool
|
If |
False
|
encoding
|
str
|
CSV I/O encoding. |
"utf-8-sig"
|
delimiter
|
str
|
CSV field delimiter. |
","
|
text_cols
|
Sequence[str]
|
When gathering from a CSV: column(s) containing text. |
("text",)
|
id_cols
|
Sequence[str]
|
When gathering from a CSV: optional ID columns to carry through. |
None
|
mode
|
('concat', 'separate')
|
Gathering behavior if multiple |
"concat"
|
group_by
|
Sequence[str]
|
Optional grouping keys used during CSV gathering (e.g., |
None
|
joiner
|
str
|
Separator used when concatenating text in |
" "
|
num_buckets
|
int
|
Number of temporary hash buckets for scalable gathering. |
512
|
max_open_bucket_files
|
int
|
Maximum number of bucket files kept open concurrently during gathering. |
64
|
tmp_root
|
str or Path
|
Root directory for temporary gathering artifacts. |
None
|
recursive
|
bool
|
When gathering from a text folder, recurse into subdirectories. |
True
|
pattern
|
str
|
Glob pattern for selecting text files. |
"*.txt"
|
id_from
|
('stem', 'name', 'path')
|
How to derive |
"stem"
|
include_source_path
|
bool
|
Whether to include the absolute source path as an additional column when gathering from a text folder. |
True
|
model_name
|
str
|
Sentence-Transformers model name or path. |
"sentence-transformers/all-roberta-large-v1"
|
batch_size
|
int
|
Batch size for model encoding. |
32
|
normalize_l2
|
bool
|
If |
True
|
rounding
|
int or None
|
If provided, round floats to this many decimals (useful for smaller files). |
None
|
show_progress
|
bool
|
Show a progress bar during embedding. |
False
|
Returns:
Type | Description |
---|---|
Path
|
Path to the written features CSV. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If an input file or directory does not exist. |
ImportError
|
If |
ValueError
|
If input modes are misconfigured (e.g., multiple or none provided),
or if the analysis-ready CSV lacks |
Examples:
Compute row-level embeddings from a transcript CSV, grouped by speaker:
>>> analyze_with_sentence_embeddings(
... csv_path="transcripts/session.csv",
... text_cols=["text"], id_cols=["speaker"], group_by=["speaker"],
... model_name="sentence-transformers/all-roberta-large-v1",
... normalize_l2=True
... )
PosixPath('.../features/sentence-embeddings/session.csv')
Notes
- Rows with no recoverable sentences produce empty feature cells (not zeros).
- The embedding dimensionality
D
is taken from the model and used to construct header columnse0..e{D-1}
.
Source code in src\taters\text\extract_sentence_embeddings.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
Practical notes¶
- Interpretability vs. nuance: Dictionaries are directly interpretable; embeddings are flexible and expressive. Many projects benefit from both.
- Construct validity: Whether counting or embedding, the theory matters. Tie features to constructs you can define, defend, and, ideally, test across datasets.
- Reproducibility: Taters standardizes I/O, uses "don't overwrite unless asked," and writes predictable outputs under
./features/*/
— so your analyses are easy to rerun and audit later.