ccat
Classes and functions to convert giellatekno xml formatted files to text.
XMLPrinter
Convert giellatekno xml formatted files to plain text.
Source code in corpustools/ccat.py
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 | |
__init__(lang=None, all_paragraphs=False, title=False, listitem=False, table=False, correction=False, error=False, errorort=False, errorortreal=False, errormorphsyn=False, errorsyn=False, errorlex=False, errorlang=False, foreign=False, errorformat=False, noforeign=False, withforeign=False, typos=False, print_filename=False, one_word_per_line=False, disambiguation=False, dependency=False, hyph_replacement='', orthography=None)
Setup all the options.
The handling of error elements are governed by the error, noforeign, correction, typos and one_word_per_line arguments.
If one_word_per_line and typos are False and correction is True, the content of the correct attribute should be printed instead of the .text part of the error element.
If one_word_per_line or typos are True, the .text part, the correct attribute and the other attributes of the error* element should be printed out on one line.
If typos is True and some of the error* options are True, only the elements that are True should be output
If one_word_per_line is True and some of the error* options are True, only the elements that are True should get the error treatment, the other ones get treated as plain elements.
If noforeign is True, neither the errorlang.text part nor the correct attribute should be printed.
Source code in corpustools/ccat.py
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 | |
collect_inline_errors(element, textlist, parentlang)
Add the "correct" element to the list textlist.
Source code in corpustools/ccat.py
277 278 279 280 281 282 283 | |
collect_not_inline_errors(element, textlist)
Add the formatted errors as strings to the textlist list.
Source code in corpustools/ccat.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
collect_text(element, parentlang, buffer)
Collect text from element, and write the contents to buffer.
Source code in corpustools/ccat.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
combine(text, text_list)
staticmethod
Combine a text with a parto f the text_list.
Source code in corpustools/ccat.py
252 253 254 255 | |
corrected_texts(error_element)
staticmethod
Yield corrected versions of the error element.
Source code in corpustools/ccat.py
224 225 226 227 228 229 230 | |
error_not_inline(element)
Collect and format parts of the element.
Also scan the children if there is no error filtering or if the element is filtered
Source code in corpustools/ccat.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
get_contents(elt_contents, textlist, elt_lang)
Get the contents of a xml document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elt_contents
|
str
|
the text of an etree element. |
required |
textlist
|
list of str
|
text will be added this list. |
required |
elt_lang
|
str
|
language of the element. |
required |
Source code in corpustools/ccat.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
get_element_language(element, parentlang)
staticmethod
Get the language of element.
Elements inherit the parents language if not explicitely set
Source code in corpustools/ccat.py
196 197 198 199 200 201 202 203 204 205 | |
get_error_attributes(correct_element)
Collect and format the attributes + the filename.
Source code in corpustools/ccat.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
get_lang()
Get the lang of the file.
Source code in corpustools/ccat.py
192 193 194 | |
handle_hyph()
Replace hyph tags.
Source code in corpustools/ccat.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
include_this_error(element)
Determine whether element should be visited.
Source code in corpustools/ccat.py
390 391 392 393 394 395 396 397 398 399 400 401 402 | |
is_correct_lang(elt_lang)
Check if elt_lang is a wanted language.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elt_lang
|
str
|
a three character language. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
boolean |
Source code in corpustools/ccat.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
parse_file(filename)
Parse the xml document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
path to the filename. |
required |
Source code in corpustools/ccat.py
404 405 406 407 408 409 410 411 412 | |
print_element(element, buffer)
Write the text of the element to the buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
_Element
|
|
required |
buffer
|
|
required |
Source code in corpustools/ccat.py
450 451 452 453 454 455 456 457 458 | |
print_file(file_)
Print a xml file to stdout. Returns True if something was printed, False otherwise.
Source code in corpustools/ccat.py
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 | |
process_file()
Process the given file, adding the text into buffer.
Returns the buffer
Source code in corpustools/ccat.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
visit_children(element, textlist, parentlang)
Visit the children of element, adding their content to textlist.
Source code in corpustools/ccat.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
visit_error_inline(element)
Determine whether element should be visited.
Source code in corpustools/ccat.py
382 383 384 385 386 387 388 | |
visit_error_not_inline(element)
Determine whether element should be visited.
Source code in corpustools/ccat.py
373 374 375 376 377 378 379 380 | |
visit_nonerror_element(element, textlist, parentlang)
Visit and extract text from non error element.
Source code in corpustools/ccat.py
350 351 352 353 354 355 356 357 358 | |
visit_this_node(element)
Return True if the element should be visited.
Source code in corpustools/ccat.py
360 361 362 363 364 365 366 367 368 369 370 371 | |
ccatter(path)
Turn an xml formatted file into clean text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
CorpusPath
|
The path to the file |
required |
Raises:
| Type | Description |
|---|---|
UserWarning
|
if there is no text, raise a UserWarning |
Returns:
| Type | Description |
|---|---|
str
|
The content of ccat output |
Source code in corpustools/ccat.py
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | |
find_files(targets, extension)
Search for files with extension in targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
list of str
|
files or directories |
required |
extension
|
str
|
interesting files has this extension. |
required |
Yields:
| Type | Description |
|---|---|
str
|
path to the interesting file |
Source code in corpustools/ccat.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |
main()
Set up the XMLPrinter class with the given command line options.
Process the given files and directories Print the output to stdout
Source code in corpustools/ccat.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | |
parse_options()
Parse the options given to the program.
Source code in corpustools/ccat.py
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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
suppress_broken_pipe_msg(function)
Suppress message after a broken pipe error.
This code is fetched from: http://stackoverflow.com/questions/14207708/ioerror-errno-32-broken-pipe-python
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
function
|
the function that should be wrapped by this function. |
required |
Source code in corpustools/ccat.py
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 | |