Library Reference
This page documents how to include CiteURL in your Python programming projects.
The first step is to instantiate a Citator, which by default contains all of CiteURL's built-in Templates:
from citeurl import Citator
citator = Citator()
After that, you can feed it text to return a list of Citations it finds:
text = """
Federal law provides that courts should award prevailing civil rights plaintiffs reasonable attorneys fees, 42 USC § 1988(b), and, by discretion, expert fees, id. at (c). This is because the importance of civil rights litigation cannot be measured by a damages judgment. See Riverside v. Rivera, 477 U.S. 561 (1986). But Evans v. Jeff D. upheld a settlement where the plaintiffs got everything they wanted, on condition that they waive attorneys' fees. 475 U.S. 717 (1986). This ruling lets savvy defendants create a wedge between plaintiffs and their attorneys, discouraging civil rights suits and undermining the court's logic in Riverside, 477 U.S. at 574-78.
"""
citations = citator.list_cites(text)
Once you have a list of citations, you can get information about each one:
print(citations[0].text)
# 42 USC § 1988(b)
print(citations[0].tokens)
# {'Title': '42', 'Section': '1988', 'subsection': '(b)'}
print(citations[0].URL)
# https://www.law.cornell.edu/uscode/text/42/1988#b
You can also compare citations to one another, to determine whether they reference the same material or a subsection thereof:
art_I = citator.cite('U.S. Const. Art. I')
also_art_I = citator.cite('Article I of the U.S. Constitution')
art_I_sec_3 = citator.cite('U.S. Const. Art. I, § 3')
assert art_I == also_art_I
assert art_I_sec_3 in art_I
If you don't want to bother with all the details, you can also just use insert_links() to turn all the citations in a text into hyperlinks:
from citeurl import insert_links
text = "42 USC § 1988. <i>Id.</i> at (b)."
output = insert_links(text)
assert output == '<a class="citation" href="https://www.law.cornell.edu/uscode/text/42/1988" title="42 U.S.C. § 1988">42 USC § 1988</a>. <a class="citation" href="https://www.law.cornell.edu/uscode/text/42/1988#b" title="42 U.S.C. § 1988(b)"><i>Id.</i> at (b)</a>.'
Citator
A collection of citation templates, and the tools to match text against them en masse.
Attributes:
Name | Type | Description |
---|---|---|
templates |
a dictionary of citation templates that this citator will try to match against |
Source code in citeurl/citator.py
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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
__init__(defaults=['caselaw', 'general federal law', 'specific federal laws', 'state law', 'secondary sources'], yaml_paths=[], templates={})
Create a citator from any combination of CiteURL's default template sets (by default, all of them), plus any custom templates you want, either by pointing to custom YAML files or making Template objects at runtime.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
defaults |
names of files to load from the citeurl/templates folder. Each file contains one or more of CiteURL's built-in templates relevant to the given topic. |
['caselaw', 'general federal law', 'specific federal laws', 'state law', 'secondary sources']
|
|
yaml_paths |
list[str]
|
paths to custom YAML files to load templates from. These are loaded after the defaults, so they can inherit and/or overwrite them. If |
[]
|
templates |
dict[str, Template]
|
optional list of Template objects to load directly. These are loaded last, after the defaults and any yaml_paths. |
{}
|
Source code in citeurl/citator.py
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 |
|
cite(text, broad=True)
Check the given text against each of the citator's templates and return the first citation detected, or None.
If broad is true, matching is case-insensitive and each template's broad regexes are used in addition to its normal regexes.
Source code in citeurl/citator.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
from_yaml(yaml)
classmethod
Create a citator from scratch (i.e. without the default templates) by loading templates from the specified YAML string.
Source code in citeurl/citator.py
372 373 374 375 376 377 378 379 380 |
|
insert_links(text, attrs={'class': 'citation'}, add_title=True, URL_optional=False, redundant_links=True, id_breaks=None, ignore_markup=True, markup_format='html')
Scan a text for citations, and return a text with each citation converted to a hyperlink.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
the string to scan for citations. |
required |
attrs |
dict
|
various HTML link attributes to give to each link. Only relevant when markup_format is html |
{'class': 'citation'}
|
add_title |
bool
|
whether to use citation.name for link titles. Only relevant when markup_format is html |
True
|
URL_optional |
bool
|
False
|
|
redundant_links |
bool
|
whether to insert a hyperlink if it would point to the same URL as the previous link |
True
|
id_breaks |
Pattern
|
wherever this regex appears, interrupt chains of "Id."-style citations. |
None
|
ignore_markup |
bool
|
whether to preprocess and postprocess the text so that CiteURL can detect citations even when they contain inline markup, like "Id. at 32" |
True
|
markup_format |
Either 'html' or 'markdown'. Determines what markup to ignore, and also what format to use for inserted links. |
'html'
|
Returns:
Type | Description |
---|---|
str
|
text, with an HTML |
Source code in citeurl/citator.py
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 623 624 625 626 |
|
list_authorities(text, ignored_tokens=['subsection', 'clause', 'pincite', 'paragraph'], known_authorities=[], sort_by_cites=True, id_breaks=None)
Find each distinct authority mentioned in the given text, and
return Authority objects whose citations
attribute lists the
references to each.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The string to be scanned for citations |
required |
ignored_tokens |
the names of tokens whose values are irrelevant to whether the citation matches an authority, because they just designate portions within a single authority |
['subsection', 'clause', 'pincite', 'paragraph']
|
|
sort_by_cites |
bool
|
Whether to sort the resulting list of authorities by the number of citations to each one |
True
|
Source code in citeurl/citator.py
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 |
|
list_cites(text, id_breaks=None)
Find all citations in the given text, whether longform, shortform, or idform. They will be listed in order of appearance. If any two citations overlap, the shorter one will be deleted.
Wherever the id_breaks pattern appears, it will interrupt chains of id-form citations. This is helpful for handling unrecognized citations that would otherwise cause CiteURL's notion of "id." to get out of sync with what the text is talking about.
Source code in citeurl/citator.py
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 |
|
load_yaml(yaml)
Load templates from the given YAML, overwriting any existing templates with the same name.
Source code in citeurl/citator.py
387 388 389 390 391 392 393 394 395 |
|
to_yaml()
Save this citator to a YAML string to load later
Source code in citeurl/citator.py
382 383 384 385 |
|
Citation
A legal reference found in text.
Attributes:
Name | Type | Description |
---|---|---|
tokens |
dictionary of the values that define this citation, such as its volume and page number, or its title, section, and subsection, etc |
|
URL |
str
|
the location, if any, where this citation can be found online, defined by the template's URL_builder |
name |
str
|
a uniform, human-readable representation of this citation, written by the template's name_builder |
text |
the actual text of this citation as found in the source text |
|
source_text |
the full text that this citation was found in |
|
template |
the template whose regexes found this citation or its parent |
|
parent |
the earlier citation, if any, that this citation is a shortform or idform child of |
|
raw_tokens |
dictionary of tokens as captured in the original regex match, before normalization. Note that for child citations, raw_tokens will include any raw_tokens inferred from the parent citation. |
|
idform_regexes |
list of regex pattern objects to find child citations later in the text, valid until the next different citation appears. |
|
shortform_regexes |
list of regex pattern objects to find child citations anywhere in the subsequent text |
Source code in citeurl/citation.py
10 11 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 |
|
__contains__(other_cite)
Returns True if both citations are from templates with the same name, and the only difference between their tokens is that the other one has a more specific (i.e. higher-indexed) token than any of this one's. Severable tokens are considered a match if the other token's value starts with this one's.
Source code in citeurl/citation.py
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 |
|
__eq__(other_cite)
Returns True if both citations are from templates with the same name, and they have the exact same token values.
Source code in citeurl/citation.py
229 230 231 232 233 234 235 236 237 |
|
Template
A pattern to recognize a single kind of citation and extract information from it.
Source code in citeurl/citator.py
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 317 318 |
|
__init__(name, tokens={}, meta={}, patterns=[], broad_patterns=[], shortform_patterns=[], idform_patterns=[], name_builder=None, URL_builder=None, inherit_template=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the name of this template |
required |
tokens |
dict[str, TokenType]
|
The full dictionary of TokenTypes that citations from this template can contain. These must be listed in order from least-specific to most. For instance, the U.S. Constitution's template puts 'article' before 'section' before 'clause', because articles contain sections, and sections contain clauses. |
{}
|
patterns |
list[str]
|
Patterns are essentially regexes to recognize recognize long-form citations to this template. However, wherever a token would appear in the regex, it should be replaced by the name of the token, enclosed in curly braces. Patterns are matched in the order that they are listed, so if there is a pattern that can only find a subset of tokens, it should be listed after the more-complete pattern so that the better match won't be precluded. |
[]
|
broad_patterns |
list[str]
|
Same as |
[]
|
shortform_patterns |
list[str]
|
Same as |
[]
|
idform_patterns |
list[str]
|
Same as |
[]
|
URL_builder |
StringBuilder
|
|
None
|
name_builder |
StringBuilder
|
|
None
|
meta |
dict[str, str]
|
Optional metadata relating to this template. Patterns and StringBuilders can access metadata fields as if they were tokens, though fields can be overridden by tokens with the same name. |
{}
|
inherit_template |
another |
None
|
Source code in citeurl/citator.py
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 |
|
cite(text, broad=True, span=(0))
Return the first citation that matches this template. If 'broad' is True, case-insensitive matching and broad regex patterns will be used. If no matches are found, return None.
Source code in citeurl/citator.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
from_dict(name, values, inheritables={})
classmethod
Return a template from a dictionary of values, like a dictionary created by parsing a template from YAML format.
Source code in citeurl/citator.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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
list_longform_cites(text, broad=False, span=(0))
Get a list of all long-form citations to this template found in the given text.
Source code in citeurl/citator.py
268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
to_dict()
save this Template to a dictionary of values
Source code in citeurl/citator.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
to_yaml()
save this Template to a YAML string
Source code in citeurl/citator.py
244 245 246 247 248 249 250 |
|
TokenType
These objects represent categories of tokens that might be found in a citation.
Attributes:
Name | Type | Description |
---|---|---|
regex |
A regular expression that matches the actual text of the token as found in any document, like the "42" in "42 USC § 1983" or the "Fourteenth" in "The Fourteenth Amendment". This regex will automatically be enclosed in a named capture group and inserted into any of the template's match patterns wherever the token's name appears in curly braces. |
|
edits |
Steps to normalize the token as captured in the regex into a value that is consistent across multiple styles. |
|
default |
Set the token to this value if it is not found in the citation. |
|
severable |
If two citations only differ based on this token,
and only because one of the tokens extends longer than the
other, e.g. "(b)(2)" and "(b)(2)(A)", then |
Source code in citeurl/tokens.py
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 |
|
from_dict(name, data)
classmethod
load a TokenType from a dictionary of values
Source code in citeurl/tokens.py
256 257 258 259 260 261 262 263 264 265 266 267 |
|
to_dict()
save this TokenType to a dictionary for storage in YAML format
Source code in citeurl/tokens.py
269 270 271 272 273 274 275 276 277 278 279 280 |
|
TokenOperation
A function to perform a predefined string manipulation
Source code in citeurl/tokens.py
6 7 8 9 10 11 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 |
|
__init__(action, data, mandatory=True, token=None, output=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action |
str
|
The kind of string manipulation that this operation will perform, using the given data. There are a few different options: 'sub': Regex substitution to perform on the text. Needs a list of two values: [PATTERN, REPLACEMENT] 'lookup': Check if the token matches any of the given
regexes (via case-insensitive matching), and if so,
replace it with the corresponding value. Needs a
dictionary of 'case': Capitalize the token in the specified way. Options are 'upper', 'lower', and 'title'. 'lpad': Left pad the token with zeros until it is the specified number of characters long. Requires an int specifying the number of characters. You can also specify the padding character by providing a tuple: (MINIMUM_LENGTH, PADDING_CHARACTER). 'number_style': Assume that the token is a number, either in the form of digits, Roman numerals, or number words like "thirty-seven". Convert it into the specified number format, which can be any of these:
|
required |
data |
any data that a given action needs specified, as described above |
required | |
mandatory |
bool
|
whether a failed lookup or format action should invalidate the entire citation |
True
|
token |
str
|
Necessary for operations in StringBuilders. This value lets you provide the name of input token to use, allowing you to then use the modify_dict() method. |
None
|
output |
str
|
If this value is set, modify_dict() will save the operation's output to the dictionary key with this name instead of modifying the input token in place. |
None
|
Source code in citeurl/tokens.py
8 9 10 11 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 |
|
from_dict(data)
classmethod
load a TokenOperation from a dictionary of values
Source code in citeurl/tokens.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
modify_dict(tokens)
apply this operation to a dictionary of tokens, editing them as appropriate
Source code in citeurl/tokens.py
138 139 140 141 142 143 144 145 146 147 148 |
|
to_dict()
save this TokenOperation to a dictionary of values
Source code in citeurl/tokens.py
124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
StringBuilder
A function to take a dictionary of values and use it to construct a piece of text from them. This is used for citation templates' name builders and URL builders.
Attributes:
Name | Type | Description |
---|---|---|
parts |
A list of strings that will be concatenated to create the string. Parts may contain bracketed references to citations' token values as well as templates' metadata. If a part references a token whose value is not set, the part will be omitted from the created string. |
|
edits |
A list of TokenOperations that will be performed on the
provided tokens before the string is constructed. If the
edits have |
|
defaults |
A dictionary of default token values to use when not overwritten by the citation. Generally these are provided by the template's meta attribute. |
Source code in citeurl/tokens.py
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 |
|
from_dict(data)
classmethod
load StringBuilder from dictionary of values
Source code in citeurl/tokens.py
335 336 337 338 339 340 341 342 343 344 |
|
to_dict()
save StringBuilder to a dictionary of values
Source code in citeurl/tokens.py
346 347 348 349 350 351 |
|
insert_links()
Convenience function to hyperlink all citations in a text. For more info, see Citator.insert_links().
Source code in citeurl/citator.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
cite()
Convenience function to find a single citation in text, or None. See Citator.cite() for more info.
Source code in citeurl/citator.py
644 645 646 647 648 649 650 651 652 653 654 |
|
list_cites()
Convenience function to list all citations in a text. For more info, see Citator.list_cites().
Source code in citeurl/citator.py
656 657 658 659 660 661 662 |
|
DEFAULT_CITATOR
The insert_links, cite, and list_cites functions all make use of a built-in citator that is not defined by the library user. By default, this is the citator that is returned when you run Citator()
. However, it is possible to add additional templates to this default citator, by installing the wonderful AppDirs library and placing the templates in one of the following directories:
Linux: ~/.config/citeurl
Mac: ~/Library/Preferences/citeurl
Windows 7+: C:\Users\<username>\AppData\Local\raindrum\citeurl