record¤
Module Description¤
The record module provides the Record class, which is the building block for every document in your corpus. Each Record wraps your text (or a parsed spaCy Doc) and metadata and offers a suite of methods for serialization, statistics, and manipulation.
Record
pydantic-model
¤
Bases: BaseModel
The main Record model.
Config:
arbitrary_types_allowed:Truevalidate_assignment:Truejson_schema_extra:DocJSONSchema.schema()
Fields:
-
id(int | UUID4) -
name(Optional[str]) -
is_active(Optional[bool]) -
content(Optional[Doc | str]) -
model(Optional[str]) -
extensions(list[str]) -
data_source(Optional[str]) -
meta(dict[str, Any])
Source code in lexos/corpus/record.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 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 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 | |
is_parsed: bool
cached
property
¤
Return whether the record is parsed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the record content is a spaCy Doc, False otherwise. |
preview: str
cached
property
¤
Return a preview of the record text.
Returns:
| Type | Description |
|---|---|
str
|
str | None: A shortened preview of the record content, or None if content is None. |
terms: Counter
cached
property
¤
Return the terms in the record.
Returns:
| Name | Type | Description |
|---|---|---|
Counter |
Counter
|
Collection mapping term -> count for the record. |
text: str
property
¤
Return the text of the record.
Returns:
| Type | Description |
|---|---|
str
|
str | None: The record text as string or None if no content is present. |
tokens: list[str]
cached
property
¤
Return the tokens in the record.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of token strings extracted from the parsed content. |
__repr__()
¤
Return a string representation of the record.
Source code in lexos/corpus/record.py
__str__() -> str
¤
Return a user-friendly string representation of the record for printing.
Source code in lexos/corpus/record.py
from_bytes(bytestring: bytes, model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None, verify_hash: bool = True) -> None
¤
Deserialise the record from bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytestring
|
bytes
|
The bytes to load the record from. |
required |
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
verify_hash
|
bool
|
Whether to verify data integrity hash. Defaults to True. |
True
|
Source code in lexos/corpus/record.py
from_disk(path: Path | str, model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None) -> None
¤
Load the record from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to load the record from. |
required |
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
Source code in lexos/corpus/record.py
least_common_terms(n: Optional[int] = None) -> list[tuple[str, int]]
¤
Return the least common terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Optional[int]
|
The number of least common terms to return. If None, return all terms. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, int]]
|
list[tuple[str, int]]: A list of (term, count) pairs sorted by least frequent. |
Source code in lexos/corpus/record.py
most_common_terms(n: Optional[int] = None) -> list[tuple[str, int]]
¤
Return the most common terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Optional[int]
|
The number of most common terms to return. If None, return all terms. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, int]]
|
list[tuple[str, int]]: A list of (term, count) pairs sorted by most frequent. |
Source code in lexos/corpus/record.py
num_terms() -> int
¤
Return the number of terms.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of unique terms in this record. |
num_tokens() -> int
¤
Return the number of tokens.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of token elements in this record. |
serialize_content(content: Doc | str) -> bytes | str
¤
Serialize the content to bytes if it is a Doc object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Doc | str
|
The content to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes | str
|
bytes | str: The serialized content as bytes if it is a Doc, otherwise the original string. |
Source code in lexos/corpus/record.py
serialize_id(id, _info) -> str
¤
Always serialize ID as string for JSON compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID | int | str
|
The ID value being serialized. |
required |
_info
|
Any
|
Encoder info (pydantic serializer internals). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The serialized ID as a string. |
Source code in lexos/corpus/record.py
serialize_meta(meta: dict[str, Any]) -> dict[str, Any]
¤
Ensure metadata is JSON-serializable by converting special types to strings.
set(**props: Any) -> None
¤
Set a record property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**props
|
Any
|
A dict containing the properties to set on the record. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in lexos/corpus/record.py
to_bytes(extensions: Optional[list[str]] = [], include_hash: bool = True) -> bytes
¤
Serialize the record to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extensions
|
list[str]
|
A list of extension names to include in the serialization. |
[]
|
include_hash
|
bool
|
Whether to include data integrity hash. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The serialized record. |
Source code in lexos/corpus/record.py
to_disk(path: Path | str, extensions: Optional[list[str]] = None) -> None
¤
Save the record to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the record to. |
required |
extensions
|
list[str]
|
A list of extension names to include in the serialization. |
None
|
Source code in lexos/corpus/record.py
vocab_density() -> float
¤
Return the vocabulary density.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The vocabulary density of the record. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
serialize_content(content: Doc | str) -> bytes | str
¤
Serialize the content to bytes if it is a Doc object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Doc | str
|
The content to serialize. |
required |
Returns:
| Type | Description |
|---|---|
bytes | str
|
bytes | str: The serialized content as bytes if it is a Doc, otherwise the original string. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
serialize_id(id, _info) -> str
¤
Always serialize ID as string for JSON compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID | int | str
|
The ID value being serialized. |
required |
_info
|
Any
|
Encoder info (pydantic serializer internals). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The serialized ID as a string. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
serialize_meta(meta: dict[str, Any]) -> dict[str, Any]
¤
Ensure metadata is JSON-serializable by converting special types to strings.
rendering:
show_root_heading: true
heading_level: 3
_sanitize_metadata(metadata: dict[str, Any]) -> dict[str, Any]
¤
Convert non-JSON-serializable types to strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict[str, Any]
|
Original metadata dictionary |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Sanitized metadata dictionary with JSON-serializable values |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
__repr__()
¤
Return a string representation of the record.
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
__str__() -> str
¤
Return a user-friendly string representation of the record for printing.
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
is_parsed: bool
cached
property
¤
Return whether the record is parsed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the record content is a spaCy Doc, False otherwise. |
rendering:
show_root_heading: true
heading_level: 3
preview: str
cached
property
¤
Return a preview of the record text.
Returns:
| Type | Description |
|---|---|
str
|
str | None: A shortened preview of the record content, or None if content is None. |
rendering:
show_root_heading: true
heading_level: 3
terms: Counter
cached
property
¤
Return the terms in the record.
Returns:
| Name | Type | Description |
|---|---|---|
Counter |
Counter
|
Collection mapping term -> count for the record. |
rendering:
show_root_heading: true
heading_level: 3
text: str
property
¤
Return the text of the record.
Returns:
| Type | Description |
|---|---|
str
|
str | None: The record text as string or None if no content is present. |
rendering:
show_root_heading: true
heading_level: 3
tokens: list[str]
cached
property
¤
Return the tokens in the record.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of token strings extracted from the parsed content. |
rendering:
show_root_heading: true
heading_level: 3
_doc_from_bytes(content: bytes, model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None) -> Doc
¤
Convert bytes to a Doc object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes
|
The bytes to convert. |
required |
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Doc |
Doc
|
The content as a Doc object. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
_doc_to_bytes() -> bytes
¤
Convert the content to bytes if it is a Doc object.
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The content as bytes. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
_get_vocab(model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None) -> Vocab
¤
Get the vocabulary from the model or model cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Vocab |
Vocab
|
The vocabulary of the model. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
from_bytes(bytestring: bytes, model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None, verify_hash: bool = True) -> None
¤
Deserialise the record from bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bytestring
|
bytes
|
The bytes to load the record from. |
required |
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
verify_hash
|
bool
|
Whether to verify data integrity hash. Defaults to True. |
True
|
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
from_disk(path: Path | str, model: Optional[str] = None, model_cache: Optional[LexosModelCache] = None) -> None
¤
Load the record from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to load the record from. |
required |
model
|
Optional[str]
|
The spaCy model to use for loading the Doc. |
None
|
model_cache
|
Optional[LexosModelCache]
|
An optional cache for spaCy models. |
None
|
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
least_common_terms(n: Optional[int] = None) -> list[tuple[str, int]]
¤
Return the least common terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Optional[int]
|
The number of least common terms to return. If None, return all terms. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, int]]
|
list[tuple[str, int]]: A list of (term, count) pairs sorted by least frequent. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
most_common_terms(n: Optional[int] = None) -> list[tuple[str, int]]
¤
Return the most common terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Optional[int]
|
The number of most common terms to return. If None, return all terms. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, int]]
|
list[tuple[str, int]]: A list of (term, count) pairs sorted by most frequent. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
num_terms() -> int
¤
Return the number of terms.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of unique terms in this record. |
rendering:
show_root_heading: true
heading_level: 3
num_tokens() -> int
¤
Return the number of tokens.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The count of token elements in this record. |
rendering:
show_root_heading: true
heading_level: 3
set(**props: Any) -> None
¤
Set a record property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**props
|
Any
|
A dict containing the properties to set on the record. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
to_bytes(extensions: Optional[list[str]] = [], include_hash: bool = True) -> bytes
¤
Serialize the record to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extensions
|
list[str]
|
A list of extension names to include in the serialization. |
[]
|
include_hash
|
bool
|
Whether to include data integrity hash. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The serialized record. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
to_disk(path: Path | str, extensions: Optional[list[str]] = None) -> None
¤
Save the record to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to save the record to. |
required |
extensions
|
list[str]
|
A list of extension names to include in the serialization. |
None
|
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3
vocab_density() -> float
¤
Return the vocabulary density.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The vocabulary density of the record. |
Source code in lexos/corpus/record.py
rendering:
show_root_heading: true
heading_level: 3