Corpus¤
The Corpus
module consists of a Corpus
class that helps you manage assets in your workflow and serialise them to disk for later use. It is strictly optional; you may find is sufficient to load your documents into memory with a Loader
or to manage your corpus assets independently through a different application.
lexos.corpus.Corpus
¤
Bases: BaseModel
The main Corpus model.
Source code in lexos\corpus\__init__.py
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 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 |
|
Config
¤
Config for the Corpus class.
Source code in lexos\corpus\__init__.py
139 140 141 142 143 |
|
__init__(**data)
¤
Initialise the Corpus.
Source code in lexos\corpus\__init__.py
145 146 147 148 149 150 151 152 |
|
__repr__()
¤
Return a string representation of the Corpus.
Source code in lexos\corpus\__init__.py
154 155 156 157 158 159 |
|
add(content, name=None, is_parsed=False, is_active=True, model=None, metadata=None, cache=False)
¤
Add a document the Corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
Union[object, str]
|
A text string or a spaCy document. |
required |
name |
str
|
A name for the document. |
None
|
is_parsed |
bool
|
Whether or not the document is parsed. |
False
|
is_active |
bool
|
Whether or not the document is active. |
True
|
model |
str
|
The name of the language model used to parse the document (optional). |
None
|
metadata |
dict
|
A dict containing any metadata. |
None
|
cache |
bool
|
Whether or not to cache the record. |
False
|
Source code in lexos\corpus\__init__.py
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 |
|
add_docs(docs, name=None, is_parsed=False, is_active=True, model=None, cache=False)
¤
Add multiple docs to the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
List[dict]
|
A list of dicts containing texts or docs to add, plus metadata. |
required |
name |
str
|
The name of the record. |
None
|
is_parsed |
bool
|
Whether the record has been parsed. |
False
|
is_active |
bool
|
Whether the record is active. |
True
|
model |
str
|
The name of the model used to parse the record. |
None
|
cache |
bool
|
Whether to cache the record. |
False
|
Note
Each doc in docs
should have a content
field containing doc text or spaCy doc and
may have any number of additional metadata fields. If "name", "is_parsed", "is_active",
and "model" are not specified, the defaults will be used.
Source code in lexos\corpus\__init__.py
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 |
|
add_record(record, cache=False)
¤
Add a Record the Corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
object
|
A Record object. |
required |
cache |
bool
|
Whether or not to cache the record. |
False
|
Source code in lexos\corpus\__init__.py
253 254 255 256 257 258 259 260 261 262 263 |
|
add_records(records, cache=False)
¤
Add multiple docs to the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
records |
List[objct]
|
A list of Record objects. |
required |
cache |
bool
|
Whether or not to cache the record. |
False
|
Source code in lexos\corpus\__init__.py
265 266 267 268 269 270 271 272 273 |
|
get(id)
¤
Get a record from the Corpus by ID.
Tries to get the record from memory; otherwise loads it from file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
int
|
A document id from the Corpus records. |
required |
Source code in lexos\corpus\__init__.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
get_records(ids=None, query=None)
¤
Get multiple records using a list of ids or a pandas query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
List[int]
|
A list of record ids to retrieve |
None
|
query |
str
|
A query string parsable by pandas.DataFrame.query |
None
|
Yields:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
A generator containing the docs matching the ids or query |
Source code in lexos\corpus\__init__.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
get_term_counts()
¤
Get a Counter with the Corpus term counts.
Returns:
Type | Description |
---|---|
Callable
|
A collections.Counter |
Source code in lexos\corpus\__init__.py
311 312 313 314 315 316 317 |
|
meta_table(drop=['docs', 'meta', 'terms'])
¤
Display Corpus metadata, one attribute per row, in a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
drop |
List[str]
|
A list of of rows to drop from the table. |
['docs', 'meta', 'terms']
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: A pandas dataframe containing the table. |
Source code in lexos\corpus\__init__.py
319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
records_table(columns=['id', 'name', 'filename', 'num_tokens', 'num_terms', 'is_active', 'is_parsed'], exclude=None)
¤
Display each document, one per row, in a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
List[str]
|
A list of of columns to include in the table. |
['id', 'name', 'filename', 'num_tokens', 'num_terms', 'is_active', 'is_parsed']
|
exclude |
List[str]
|
A list of columns to exclude from the table. |
None
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pd.DataFrame: A pandas dataframe containing the table. |
Source code in lexos\corpus\__init__.py
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 |
|
remove(id)
¤
Remove a Corpus record by id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
int
|
A record id. |
required |
Source code in lexos\corpus\__init__.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
remove_records(ids)
¤
Remove multiple records from the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
List[int]
|
A list of record ids to remove. |
required |
Source code in lexos\corpus\__init__.py
384 385 386 387 388 389 390 391 |
|
set(id, **props)
¤
Set a property or properties of a record in the Corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
int
|
A document id. |
required |
**props |
dict
|
The dict containing any other properties to set. |
{}
|
Source code in lexos\corpus\__init__.py
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 |
|
lexos.corpus.NullTokenizer
¤
Pass the text back as a spaCy Doc with the text as single token.
Source code in lexos\corpus\__init__.py
30 31 32 33 34 35 36 37 38 39 40 41 |
|
__call__(text)
¤
Return the text as a single token.
Source code in lexos\corpus\__init__.py
37 38 39 40 41 |
|
__init__(vocab)
¤
Initialise the tokeniser.
Source code in lexos\corpus\__init__.py
33 34 35 |
|
lexos.corpus.Record
¤
Bases: BaseModel
The main Record model.
Source code in lexos\corpus\__init__.py
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 |
|
Config
¤
Config for the Record class.
Source code in lexos\corpus\__init__.py
55 56 57 58 59 |
|
__repr__()
¤
Return a string representation of the record.
Source code in lexos\corpus\__init__.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
get_properties()
classmethod
¤
Return a list of the properties of the Record class.
Source code in lexos\corpus\__init__.py
78 79 80 81 82 83 |
|
num_terms()
¤
Return the number of terms.
Source code in lexos\corpus\__init__.py
105 106 107 |
|
num_tokens()
¤
Return the number of tokens.
Source code in lexos\corpus\__init__.py
109 110 111 |
|
preview()
property
¤
Return a preview of the record text.
Source code in lexos\corpus\__init__.py
85 86 87 88 |
|
save()
¤
Serialise the record to disk.
Source code in lexos\corpus\__init__.py
113 114 115 116 |
|
set(k, v)
¤
Set a record property.
Source code in lexos\corpus\__init__.py
118 119 120 |
|
terms()
property
¤
Return the terms in the record.
Source code in lexos\corpus\__init__.py
90 91 92 93 |
|
text()
property
¤
Return the text of the record.
Source code in lexos\corpus\__init__.py
95 96 97 98 |
|
tokens()
property
¤
Return the tokens in the record.
Source code in lexos\corpus\__init__.py
100 101 102 103 |
|