Structural Stylometry¤
StructuralAnalyzer
pydantic-model
¤
Bases: BaseModel
Builds a stylometric matrix from a corpus of documents, with options for punctuation and whitespace tracking.
Config:
arbitrary_types_allowed:True
Fields:
-
corpus(dict[str, str | Doc] | Corpus | Loader) -
model(Optional[str]) -
max_features(Optional[int]) -
include_whitespace(bool) -
feature_mode(str) -
min_punctuation_threshold(int) -
action_on_low_count(Optional[str]) -
tokenized_corpus(dict[str, list[str]]) -
doc_ids(list[str]) -
vocabulary(list[str]) -
vocab_idx(dict[str, int]) -
raw_matrix(ndarray)
Source code in lexos/structural_stylometry/structural_stylometry.py
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 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 | |
action_on_low_count: Optional[str] = 'warn'
pydantic-field
¤
"warn" to log console warnings, "drop" to filter out doc completely
corpus: dict[str, str | Doc] | Corpus | Loader
pydantic-field
¤
Dictionary of {doc_id: str | Doc} representing the corpus or a Lexos Corpus or Loader object.
doc_ids: list[str]
pydantic-field
¤
List of document IDs that passed filtering
feature_mode: str = 'all'
pydantic-field
¤
"all", "punctuation_only", or "structural_only"
include_whitespace: bool = True
pydantic-field
¤
Whether to include whitespace markers as features
max_features: Optional[int] = 100
pydantic-field
¤
Maximum number of features to track in the vocabulary
min_punctuation_threshold: int = 20
pydantic-field
¤
Minimum total punctuation marks required per doc
model: Optional[str] = 'xx_sent_ud_sm'
pydantic-field
¤
spaCy language model to use for processing text.
tokenized_corpus: dict[str, list[str]]
pydantic-field
¤
Dictionary of {doc_id: list_of_tokens}
__init__(**data)
¤
Constructor for stylometric matrix builder.
Source code in lexos/structural_stylometry/structural_stylometry.py
get_distance_matrix(method: str = 'classic', as_df: bool = False) -> np.ndarray | pd.DataFrame
¤
Computes the Burrows' Delta distance matrix.
Use classic pairwise Burrows' Delta distance if you are benchmarking your results against older stylometry frameworks, or if your dataset relies on a mixture of words and punctuation where you want every feature to contribute linearly.
Use the Argamon quadratic variant (applies a Euclidean metric to the Z-scored feature values) if you are utilizing the structural_only or punctuation_only feature modes. Because punctuation marks often show dramatic outliers (e.g., one text using ten times as many semicolons as the corpus average), squaring the differences prevents these massive single-feature spikes from distorting your overall unsupervised clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"classic" or "quadratic" for the distance calculation method. |
'classic'
|
as_df
|
bool
|
Whether to return the result as a pandas DataFrame with labeled rows and columns. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | DataFrame
|
np.ndarray | pd.DataFrame: A square NumPy array or pandas DataFrame where matrix[i, j] is the stylistic distance. |
Source code in lexos/structural_stylometry/structural_stylometry.py
get_feature_matrix(method: str = 'tfidf')
¤
Returns the stylometric matrix in the specified representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" |
'tfidf'
|
Source code in lexos/structural_stylometry/structural_stylometry.py
get_loadings(method: str = 'tfidf', pca: PCA | None = None) -> dict[str, pd.DataFrame]
¤
Returns a dictionary of PCA loadings for each principal component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" for the stylometric representation. |
'tfidf'
|
pca
|
PCA | None
|
An optional pre-fitted PCA object. If None, a new PCA will be fitted. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, DataFrame]
|
A dictionary where keys are component names (e.g., 'PC1', 'PC2') and values are DataFrames of features and their corresponding loadings. |
Source code in lexos/structural_stylometry/structural_stylometry.py
to_csv(filepath: str, method: str)
¤
Exports a stylometric representation matrix to CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Local disk save path destination string. |
required |
method
|
str
|
"raw", "tfidf", or "burrows_z" |
required |
Source code in lexos/structural_stylometry/structural_stylometry.py
to_df(method: str) -> pd.DataFrame
¤
Converts a stylometric representation matrix into a labeled DataFrame for export.
Args: - method (str): "raw", "tfidf", or "burrows_z"
Returns: - pd.DataFrame: A DataFrame with document IDs as the first column and vocabulary features as subsequent columns.
Source code in lexos/structural_stylometry/structural_stylometry.py
visualize(method: str = 'tfidf', top_n: int = 5, show_plots: bool = True, show_loadings: bool = True)
¤
Generates Dendrogram and PCA plots.
Also prints the specific structural features driving the variance on PC1 and PC2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" for the stylometric representation. |
'tfidf'
|
top_n
|
int
|
Number of top features to display for each principal component. |
5
|
show_plots
|
bool
|
Whether to display the plots. If False, only prints the loadings. |
True
|
show_loadings
|
bool
|
Whether to print the top feature loadings for PC1 and PC2. |
True
|
Source code in lexos/structural_stylometry/structural_stylometry.py
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 | |
__init__(**data)
¤
Constructor for stylometric matrix builder.
Source code in lexos/structural_stylometry/structural_stylometry.py
_count_total_punctuation(text: str) -> int
¤
Quick internal count of total punctuation characters using spaCy token rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw text to analyze |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of punctuation characters in the text |
Source code in lexos/structural_stylometry/structural_stylometry.py
_tokenize_structural(text: str | Doc, lowercase: bool = True) -> list
¤
Extracts and filters tokens based on configuration rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw text to tokenize |
required |
lowercase
|
bool
|
Whether to convert tokens to lowercase |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of tokens, including punctuation and optional whitespace markers |
Source code in lexos/structural_stylometry/structural_stylometry.py
_build_raw_matrix()
¤
Constructs the raw frequency matrix for the corpus.
Source code in lexos/structural_stylometry/structural_stylometry.py
get_distance_matrix(method: str = 'classic', as_df: bool = False) -> np.ndarray | pd.DataFrame
¤
Computes the Burrows' Delta distance matrix.
Use classic pairwise Burrows' Delta distance if you are benchmarking your results against older stylometry frameworks, or if your dataset relies on a mixture of words and punctuation where you want every feature to contribute linearly.
Use the Argamon quadratic variant (applies a Euclidean metric to the Z-scored feature values) if you are utilizing the structural_only or punctuation_only feature modes. Because punctuation marks often show dramatic outliers (e.g., one text using ten times as many semicolons as the corpus average), squaring the differences prevents these massive single-feature spikes from distorting your overall unsupervised clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"classic" or "quadratic" for the distance calculation method. |
'classic'
|
as_df
|
bool
|
Whether to return the result as a pandas DataFrame with labeled rows and columns. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | DataFrame
|
np.ndarray | pd.DataFrame: A square NumPy array or pandas DataFrame where matrix[i, j] is the stylistic distance. |
Source code in lexos/structural_stylometry/structural_stylometry.py
get_feature_matrix(method: str = 'tfidf')
¤
Returns the stylometric matrix in the specified representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" |
'tfidf'
|
Source code in lexos/structural_stylometry/structural_stylometry.py
to_csv(filepath: str, method: str)
¤
Exports a stylometric representation matrix to CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Local disk save path destination string. |
required |
method
|
str
|
"raw", "tfidf", or "burrows_z" |
required |
Source code in lexos/structural_stylometry/structural_stylometry.py
to_df(method: str) -> pd.DataFrame
¤
Converts a stylometric representation matrix into a labeled DataFrame for export.
Args: - method (str): "raw", "tfidf", or "burrows_z"
Returns: - pd.DataFrame: A DataFrame with document IDs as the first column and vocabulary features as subsequent columns.
Source code in lexos/structural_stylometry/structural_stylometry.py
visualize(method: str = 'tfidf', top_n: int = 5, show_plots: bool = True, show_loadings: bool = True)
¤
Generates Dendrogram and PCA plots.
Also prints the specific structural features driving the variance on PC1 and PC2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" for the stylometric representation. |
'tfidf'
|
top_n
|
int
|
Number of top features to display for each principal component. |
5
|
show_plots
|
bool
|
Whether to display the plots. If False, only prints the loadings. |
True
|
show_loadings
|
bool
|
Whether to print the top feature loadings for PC1 and PC2. |
True
|
Source code in lexos/structural_stylometry/structural_stylometry.py
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 | |
get_loadings(method: str = 'tfidf', pca: PCA | None = None) -> dict[str, pd.DataFrame]
¤
Returns a dictionary of PCA loadings for each principal component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"raw", "tfidf", or "burrows_z" for the stylometric representation. |
'tfidf'
|
pca
|
PCA | None
|
An optional pre-fitted PCA object. If None, a new PCA will be fitted. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, DataFrame]
|
A dictionary where keys are component names (e.g., 'PC1', 'PC2') and values are DataFrames of features and their corresponding loadings. |