DTM¤
DTM
pydantic-model
¤
Bases: BaseModel
Class for a document-term matrix.
Config:
default:validation_config
Fields:
-
docs(Optional[list[list[str] | Doc]]) -
labels(Optional[list[str]]) -
vectorizer(Optional[Callable]) -
alg(Optional[ns]) -
doc_term_matrix(Optional[spmatrix])
Source code in lexos/dtm/__init__.py
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 | |
alg: Optional[ns] = ns.LOCALE
pydantic-field
¤
The sorting algorithm to use.
doc_term_matrix: Optional[sp.spmatrix] = None
pydantic-field
¤
The document-term matrix.
docs: Optional[list[list[str] | Doc]] = None
pydantic-field
¤
A list of spaCy docs or a list of token lists.
labels: Optional[list[str]] = None
pydantic-field
¤
A list of labels for the documents.
shape: tuple[int, int]
property
¤
Return the shape of the DTM.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
tuple[int, int]: The shape of the DTM. |
sorted_term_counts: dict[str, int]
property
¤
Return a natsorted dict of terms and their TOTAL counts across all documents in the DTM.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
dict[str, int]: A natsorted dict of terms and their total counts. |
sorted_terms_list: list[str]
property
¤
Return a natsorted list of terms in the DTM.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A natsorted list of terms in the DTM. |
__call__(docs: Optional[list[list[str] | Doc]], labels: Optional[Iterable[str]], **kwargs: dict[str, str | int | float | bool]) -> None
¤
Call method for DTM class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
list[list[str] | Doc]
|
A list of spaCy docs or a list of token lists. |
required |
labels
|
list[str]
|
A list of labels for the documents. |
required |
**kwargs
|
dict
|
Additional keyword arguments to pass to the vectorizer. |
{}
|
Note
- If you want to filter the docs by token attributes, you can do so beforehand and pass the filtered docs to this method.
- If you want to sort the dataframe, use pandas sort_values(), but make sure to
pass
SORTING_ALGORITHMorself.algto thekeyparameter for natsorting.
Source code in lexos/dtm/__init__.py
__init__(**data: dict[str, list | str | Callable | ns | sp.spmatrix]) -> None
¤
Initialize the DTM class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
A dictionary of data to initialize the DTM. - docs: A list of spaCy docs or a list of token lists. - labels: A list of labels for the documents. - vectorizer: A callable Vectorizer. Must have a fit_transform() method. - alg: The sorting algorithm to use (default is ns.LOCALE). - doc_term_matrix: The document-term matrix (optional). - **kwargs: Additional keyword arguments to pass to the vectorizer. |
{}
|
Source code in lexos/dtm/__init__.py
to_df(by: Optional[list | list[str]] = None, ascending: Optional[bool | list[bool]] = True, as_percent: Optional[bool] = False, rounding: Optional[int] = 3, transpose: Optional[bool] = False, sum: Optional[bool] = False, mean: Optional[bool] = False, median: Optional[bool] = False) -> pd.DataFrame
¤
Return the whole DTM as a pandas dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
Optional[list | list[str]]
|
The column(s) to sort by. |
None
|
ascending
|
Optional[bool | list[bool]]
|
Whether to sort in ascending order. |
True
|
as_percent
|
Optional[bool]
|
Whether to return the terms as percentages. |
False
|
rounding
|
Optional[int]
|
The number of decimal places to round to. |
3
|
transpose
|
Optional[bool]
|
Whether to transpose the dataframe. |
False
|
sum
|
Optional[bool]
|
Whether to include a column for the sum of each row. |
False
|
mean
|
Optional[bool]
|
Whether to include a column for the mean of each row. |
False
|
median
|
Optional[bool]
|
Whether to include a column for the median of each row. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The DTM as a pandas dataframe. |
Source code in lexos/dtm/__init__.py
__init__(**data: dict[str, list | str | Callable | ns | sp.spmatrix]) -> None
¤
Initialize the DTM class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
A dictionary of data to initialize the DTM. - docs: A list of spaCy docs or a list of token lists. - labels: A list of labels for the documents. - vectorizer: A callable Vectorizer. Must have a fit_transform() method. - alg: The sorting algorithm to use (default is ns.LOCALE). - doc_term_matrix: The document-term matrix (optional). - **kwargs: Additional keyword arguments to pass to the vectorizer. |
{}
|
Source code in lexos/dtm/__init__.py
shape: tuple[int, int]
property
¤
Return the shape of the DTM.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
tuple[int, int]: The shape of the DTM. |
sorted_terms_list: list[str]
property
¤
Return a natsorted list of terms in the DTM.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A natsorted list of terms in the DTM. |
sorted_term_counts: dict[str, int]
property
¤
Return a natsorted dict of terms and their TOTAL counts across all documents in the DTM.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
dict[str, int]: A natsorted dict of terms and their total counts. |
_get_term_percentages(df: pd.DataFrame, rounding: int = 3, as_str: bool | str = 'string', sum: bool = False, mean: bool = False, median: bool = False) -> pd.DataFrame
¤
Return a dataframe with term frequencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The dataframe to convert to percentages. |
required |
rounding
|
int
|
The number of decimal places to round to. |
3
|
as_str
|
bool | str
|
Whether to return the terms as strings. |
'string'
|
sum
|
bool
|
Whether to include a column for the sum of each row. |
False
|
mean
|
bool
|
Whether to include a column for the mean of each row. |
False
|
median
|
bool
|
Whether to include a column for the median of each row. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: A dataframe with term frequencies. |
Source code in lexos/dtm/__init__.py
_update_vectorizer(**kwargs: dict[str, str | int | float | bool]) -> None
¤
Update the vectorizer with additional keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict
|
Additional keyword arguments to update the vectorizer. |
{}
|
Source code in lexos/dtm/__init__.py
_validate_sorting_algorithm() -> bool
¤
Ensure that the specified sorting algorithm is a valid natsort locale.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Whether the sorting algorithm is valid. |
Source code in lexos/dtm/__init__.py
to_df(by: Optional[list | list[str]] = None, ascending: Optional[bool | list[bool]] = True, as_percent: Optional[bool] = False, rounding: Optional[int] = 3, transpose: Optional[bool] = False, sum: Optional[bool] = False, mean: Optional[bool] = False, median: Optional[bool] = False) -> pd.DataFrame
¤
Return the whole DTM as a pandas dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
Optional[list | list[str]]
|
The column(s) to sort by. |
None
|
ascending
|
Optional[bool | list[bool]]
|
Whether to sort in ascending order. |
True
|
as_percent
|
Optional[bool]
|
Whether to return the terms as percentages. |
False
|
rounding
|
Optional[int]
|
The number of decimal places to round to. |
3
|
transpose
|
Optional[bool]
|
Whether to transpose the dataframe. |
False
|
sum
|
Optional[bool]
|
Whether to include a column for the sum of each row. |
False
|
mean
|
Optional[bool]
|
Whether to include a column for the mean of each row. |
False
|
median
|
Optional[bool]
|
Whether to include a column for the median of each row. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The DTM as a pandas dataframe. |
Source code in lexos/dtm/__init__.py
Vectorizer
pydantic-model
¤
Bases: BaseModel
Wrapper class for Textacy's Vectorizer.
Source code in lexos/dtm/__init__.py
__call__(*, tf_type: Literal['linear', 'sqrt', 'log', 'binary'] = 'linear', idf_type: Optional[Literal['linear', 'sqrt', 'log']] = None, dl_type: Optional[Literal['linear', 'sqrt', 'log']] = None, norm: Optional[Literal['l1', 'l2']] = None, min_df: int | float = 1, max_df: int | float = 1.0, max_n_terms: Optional[int] = None, vocabulary_terms: Optional[dict[str, int] | Iterable[str]] = None) -> TextacyVectorizer
¤
Return a Textacy Vectorizer object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tf_type
|
str
|
Term frequency type. |
'linear'
|
idf_type
|
str
|
Inverse document frequency type. |
None
|
dl_type
|
str
|
Document length type. |
None
|
norm
|
str
|
Normalization type. |
None
|
min_df
|
int | float
|
Minimum document frequency. |
1
|
max_df
|
int | float
|
Maximum document frequency. |
1.0
|
max_n_terms
|
int
|
Maximum number of terms. |
None
|
vocabulary_terms
|
dict | Iterable[str]
|
Vocabulary terms. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TextacyVectorizer |
Vectorizer
|
A Textacy Vectorizer object. |
Source code in lexos/dtm/__init__.py
__call__(*, tf_type: Literal['linear', 'sqrt', 'log', 'binary'] = 'linear', idf_type: Optional[Literal['linear', 'sqrt', 'log']] = None, dl_type: Optional[Literal['linear', 'sqrt', 'log']] = None, norm: Optional[Literal['l1', 'l2']] = None, min_df: int | float = 1, max_df: int | float = 1.0, max_n_terms: Optional[int] = None, vocabulary_terms: Optional[dict[str, int] | Iterable[str]] = None) -> TextacyVectorizer
¤
Return a Textacy Vectorizer object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tf_type
|
str
|
Term frequency type. |
'linear'
|
idf_type
|
str
|
Inverse document frequency type. |
None
|
dl_type
|
str
|
Document length type. |
None
|
norm
|
str
|
Normalization type. |
None
|
min_df
|
int | float
|
Minimum document frequency. |
1
|
max_df
|
int | float
|
Maximum document frequency. |
1.0
|
max_n_terms
|
int
|
Maximum number of terms. |
None
|
vocabulary_terms
|
dict | Iterable[str]
|
Vocabulary terms. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TextacyVectorizer |
Vectorizer
|
A Textacy Vectorizer object. |