Teanga Corpus Module
Corpus
Bases: ImmutableCorpus
Corpus class for storing and processing text data.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
Source code in teanga/corpus.py
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 |
|
doc_ids
property
Return the document ids of the corpus.
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> list(corpus.doc_ids)
['Kjco']
docs
property
Get all the documents in the corpus
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> list(corpus.docs)
[Document('Kjco', {'text': 'This is a document.'})]
meta
property
writable
Return the meta data of the corpus.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> corpus.meta
{'text': LayerDesc(layer_type='characters', base=None, data=None, link_types=None, target=None, default=None, meta={})}
__eq__(other)
Compare two Teanga Corpora for equality
Source code in teanga/corpus.py
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 |
|
add_doc(*args, **kwargs)
Add a document to the corpus.
Args:
If the corpus has only a single layer, the document can be added as a
string. If the corpus has multiple layers, the document must be added
by specifying the names of the layers and the data for each layer as
keyword arguments.
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> corpus = parallel_corpus(["en", "nl"])
>>> doc = corpus.add_doc(en="This is a document.", nl="Dit is een document.")
Source code in teanga/corpus.py
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 |
|
add_layer_meta(name=None, layer_type='characters', base=None, data=None, link_types=None, target=None, default=None, meta={})
Add a layer to the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str Name of the layer. |
None
|
layer_type
|
str
|
str The type of the layer, can be "characters", "span", "seq", "element" or "div". |
'characters'
|
base
|
str
|
str The name of the layer on which the new layer is based. |
None
|
data
|
list The data of the layer, this can be the value "string", "link" or a list of strings, for an enumeration of values |
None
|
|
link_types
|
list[str]
|
list The types of the links, if the data is links. |
None
|
target
|
str
|
str The name of the target layer, if the data is links. |
None
|
default
|
A default value if none is given |
None
|
|
meta
|
dict
|
dict Metadata properties of the layer. |
{}
|
Source code in teanga/corpus.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 |
|
add_meta_from_service(service)
Add the meta data of a service to the corpus. This is normally required to call apply to a service
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
Service
|
The service to add. |
required |
Examples:
>>> corpus = Corpus()
>>> class ExampleService:
... def requires(self):
... return {"text": {"type": "characters"}}
... def produces(self):
... return {"first_char": {"type": "characters"}}
>>> corpus.add_meta_from_service(ExampleService())
Returns:
Type | Description |
---|---|
A number representing the arithmetic sum of |
Source code in teanga/corpus.py
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
apply(service)
Apply a service to each document in the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
Service
|
The service to apply. |
required |
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> corpus.add_layer_meta("first_char", layer_type="element", base="text")
>>> doc = corpus.add_doc(text="This is a document.")
>>> from teanga.service import Service
>>> class FirstCharService(Service):
... def requires(self):
... return {"text": { "type": "characters"}}
... def produces(self):
... return {"first_char": {"type": "element", "base": "text"}}
... def execute(self, input):
... input["first_char"] = [0]
... return input
>>> corpus.apply(FirstCharService())
Source code in teanga/corpus.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 |
|
doc_by_id(doc_id)
Get a document by its id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc_id
|
str
|
str The id of the document. |
required |
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> corpus.doc_by_id("Kjco")
Document('Kjco', {'text': 'This is a document.'})
>>> if TEANGA_PYO3:
... corpus = Corpus("tmp",new=True)
... corpus.add_layer_meta("text")
... doc = corpus.add_doc("This is a document.")
Source code in teanga/corpus.py
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 |
|
search(query=None, **kwargs)
Search for documents in the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs,
|
query
|
The search criteria. The keys are the layer names and the values can be either a string, a list of strings or a dictionary with values describing the search criteria. If the value is a string the search is interpreted as an exact match. If the layer has no data this is applied to the text otherwise it is applied to the data. If the value is a list of strings, the search is interpreted as a search for any of the strings in the list. For dictionaries, the following keys are supported:
|
required |
Returns:
Type | Description |
---|---|
Iterator[str]
|
An iterator over the document ids that match the search criteria. |
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> corpus.add_layer_meta("words", layer_type="span", base="text")
>>> corpus.add_layer_meta("pos", layer_type="seq", base="words",
... data=["NOUN", "VERB", "ADJ"])
>>> corpus.add_layer_meta("lemma", layer_type="seq", base="words",
... data="string")
>>> doc = corpus.add_doc("Colorless green ideas sleep furiously.")
>>> doc.words = [(0, 9), (10, 15), (16, 21), (22, 27), (28, 37)]
>>> doc.pos = ["ADJ", "ADJ", "NOUN", "VERB", "ADV"]
>>> doc.lemma = ["colorless", "green", "idea", "sleep", "furiously"]
>>> list(corpus.search(pos="NOUN"))
['9wpe']
>>> list(corpus.search(pos=["NOUN", "VERB"]))
['9wpe']
>>> list(corpus.search(pos={"$in": ["NOUN", "VERB"]}))
['9wpe']
>>> list(corpus.search(pos={"$regex": "N.*"}))
['9wpe']
>>> list(corpus.search(pos="VERB", lemma="sleep"))
['9wpe']
>>> list(corpus.search(pos="VERB", words="idea"))
[]
>>> list(corpus.search(pos="VERB", words="ideas"))
['9wpe']
>>> list(corpus.search({"pos": "VERB", "lemma": "sleep"}))
['9wpe']
>>> list(corpus.search({"$and": {"pos": "VERB", "lemma": "sleep"}}))
['9wpe']
Source code in teanga/corpus.py
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
|
to_cuac(path)
Write the corpus to a Cuac file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
str The path to the Cuac file. |
required |
Source code in teanga/corpus.py
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
|
to_json(path_or_buf)
Write the corpus to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str The path to the json file or a buffer. |
required |
Source code in teanga/corpus.py
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 |
|
to_json_str()
Write the corpus to a JSON string.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
>>> corpus.to_json_str()
'{"_meta": {"text": {"type": "characters"}}, "_order": ["Kjco"], "Kjco": {"text": "This is a document."}}'
Source code in teanga/corpus.py
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 |
|
to_yaml(path_or_buf)
Write the corpus to a yaml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str
|
str The path to the yaml file or a buffer. |
required |
Source code in teanga/corpus.py
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
|
to_yaml_str()
Write the corpus to a yaml string.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
>>> corpus.to_yaml_str()
'_meta:\n text:\n type: characters\nKjco:\n text: This is a document.\n'
Source code in teanga/corpus.py
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
|
update_doc(old_id, doc)
Replace a particular document indicated by an identifier with a new document object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
old_id
|
str
|
str The identifier of the document to replace. |
required |
doc
|
Document
|
Document The new document object. |
required |
Returns:
Type | Description |
---|---|
str
|
The identifier of the new document. |
Source code in teanga/corpus.py
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 |
|
ImmutableCorpus
Bases: ABC
An abstract base class for immutable corpora. This class provides a read-only interface to the corpus, allowing access to documents and their metadata without modification.
Source code in teanga/corpus.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 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 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 653 654 655 656 657 658 659 660 661 662 663 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 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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 |
|
doc_ids
abstractmethod
property
Return the document ids of the corpus.
docs
property
Get all the documents in the corpus.
Returns:
Type | Description |
---|---|
Iterator[Document]
|
An iterator over Document objects. |
meta
abstractmethod
property
Return the metadata of the corpus.
Returns:
Type | Description |
---|---|
dict[str, LayerDesc]
|
A dictionary with layer names as keys and LayerDesc objects as values. |
order
property
Return a list of the document ids in the order they appear in the corpus.
Returns:
Type | Description |
---|---|
list[str]
|
A list of document ids in the order they appear in the corpus. |
__getitem__(key)
Get a document by its id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Union[str, int, slice] The id of the document. Strings use document identifiers, while integers use the order of the documents in the corpus. |
required |
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> corpus["Kjco"]
Document('Kjco', {'text': 'This is a document.'})
>>> corpus[0]
Document('Kjco', {'text': 'This is a document.'})
>>> corpus[:1]
[Document('Kjco', {'text': 'This is a document.'})]
Source code in teanga/corpus.py
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 |
|
__str__()
Return a string representation of the corpus.
Source code in teanga/corpus.py
948 949 950 |
|
all(layer_name)
Get the combined value of a single layer in the order of the corpus. This will return the characters for layers without data and the data for layers with data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name
|
str
|
str The name of the layer to get the values from. |
required |
Returns:
Type | Description |
---|---|
Iterator
|
An iterator over the values of the layer in the order of the corpus. |
Examples:
>>> corpus = text_corpus()
>>> corpus.add_layer_meta("pos", layer_type="seq", base="tokens",
... data=["NOUN", "VERB", "ADJ"])
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc1.tokens = [(0, 4), (5, 7), (8, 9), (10, 18)]
>>> doc1.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc2.tokens = [(0, 4), (5, 7), (8, 15), (16, 24)]
>>> doc2.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> list(corpus.all("text"))
['This is a document.', 'This is another document.']
>>> list(corpus.all("tokens"))
['This', 'is', 'a', 'document', 'This', 'is', 'another', 'document']
>>> list(corpus.all("pos"))
['ADJ', 'VERB', 'NOUN', 'VERB', 'ADJ', 'VERB', 'NOUN', 'VERB']
Source code in teanga/corpus.py
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 653 654 655 656 |
|
all_data(layer_name)
Get the combined data of a single layer in the order of the corpus. This will return the data for layers with data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name
|
str
|
str The name of the layer to get the values from. |
required |
Returns:
Type | Description |
---|---|
Iterator
|
An iterator over the data values of the layer in the order of the corpus. |
Examples:
>>> corpus = text_corpus()
>>> corpus.add_layer_meta("pos", layer_type="seq", base="tokens",
... data=["NOUN", "VERB", "ADJ"])
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc1.tokens = [(0, 4), (5, 7), (8, 9), (10, 18)]
>>> doc1.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc2.tokens = [(0, 4), (5, 7), (8, 15), (16, 24)]
>>> doc2.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> list(corpus.all_data("pos"))
['ADJ', 'VERB', 'NOUN', 'VERB', 'ADJ', 'VERB', 'NOUN', 'VERB']
Source code in teanga/corpus.py
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 |
|
all_text(layer_name)
Get the combined text of a single layer in the order of the corpus. This will return the characters for layers without data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer_name
|
str
|
str The name of the layer to get the values from. |
required |
Returns:
Type | Description |
---|---|
Iterator[str]
|
An iterator over the text values of the layer in the order of the corpus. |
Examples:
>>> corpus = text_corpus()
>>> corpus.add_layer_meta("pos", layer_type="seq", base="tokens",
... data=["NOUN", "VERB", "ADJ"])
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc1.tokens = [(0, 4), (5, 7), (8, 9), (10, 18)]
>>> doc1.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc2.tokens = [(0, 4), (5, 7), (8, 15), (16, 24)]
>>> doc2.pos = ["ADJ", "VERB", "NOUN", "VERB"]
>>> list(corpus.all_text("tokens"))
['This', 'is', 'a', 'document', 'This', 'is', 'another', 'document']
>>> list(corpus.all_text("pos"))
['This', 'is', 'a', 'document', 'This', 'is', 'another', 'document']
Source code in teanga/corpus.py
658 659 660 661 662 663 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 689 |
|
by(layer)
Group the corpus according to which documents have specific values of a layer. Mostly used for metadata layers (e.g., "author", "genre")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer
|
str
|
str The name of the layer to group by. |
required |
Returns: A GroupedCorpus object that groups documents by the values in the specified layer. Examples: >>> corpus = text_corpus() >>> corpus.add_layer_meta("author") >>> doc1 = corpus.add_doc(text="This is a document", author="John") >>> doc2 = corpus.add_doc(text="This is another document", author="Mary") >>> group = corpus.by("author") >>> group["John"][0].text 'This is a document'
Source code in teanga/corpus.py
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 |
|
by_doc()
Group the corpus by document to enable analysis such as frequency analysis on a per document basis.
Examples:
>>> corpus = text_corpus()
>>> doc1 = corpus.add_doc("This is a document")
>>> doc1.tokens = [(0, 4), (5, 7), (8, 9), (10, 18)]
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc2.tokens = [(0, 4), (5, 7), (8, 15), (16, 24)]
>>> group = corpus.by_doc()
>>> group.text_freq("tokens")
{'HP5c': Counter({'This': 1, 'is': 1, 'a': 1, 'document': 1}), 'eDFn': Counter({'This': 1, 'is': 1, 'another': 1, 'document': 1})}
Source code in teanga/corpus.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
doc_by_id(doc_id)
abstractmethod
Get a document by its id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc_id
|
str
|
str The id of the document. |
required |
Returns:
Type | Description |
---|---|
Document
|
A Document object. |
Source code in teanga/corpus.py
47 48 49 50 51 52 53 54 55 56 57 58 |
|
filter(filter_func)
Create a new corpus that is a filtered version of the current corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter_func
|
Callable[Document, bool]
|
Callable[Document, bool] A function that takes a Document object and returns True if the document should be included in the filtered corpus. |
required |
Returns:
Type | Description |
---|---|
ImmutableCorpus
|
A new Corpus object that contains only the documents that match the filter. |
Examples:
>>> corpus = text_corpus()
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc3 = corpus.add_doc("This is yet another document.")
>>> filter_func = lambda doc: "y" in doc.text
>>> filtered_corpus = corpus.filter(filter_func)
>>> list(filtered_corpus.doc_ids)
['fpwP']
Source code in teanga/corpus.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
lower()
Lowercase all the text in the corpus.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
>>> corpus = corpus.lower()
>>> list(corpus.docs)
[Document('Kjco', {'text': 'this is a document.'})]
Source code in teanga/corpus.py
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 |
|
normalise_query(query)
Normalise a query by replacing all field values with either $eq
or
$text
Source code in teanga/corpus.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
sample(k)
Create a new corpus that is a random sample of the current corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
int
|
int The number of documents to sample from the corpus. |
required |
Returns:
Type | Description |
---|---|
ImmutableCorpus
|
A new Corpus object that contains a random sample of k documents. |
Examples:
>>> corpus = text_corpus()
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc3 = corpus.add_doc("This is yet another document.")
>>> sampled_corpus = corpus.sample(2)
>>> len(list(sampled_corpus.doc_ids))
2
Source code in teanga/corpus.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 |
|
search(query=None, **kwargs)
Search for documents in the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs,
|
query
|
The search criteria. The keys are the layer names and the values can be either a string, a list of strings or a dictionary with values describing the search criteria. If the value is a string the search is interpreted as an exact match. If the layer has no data this is applied to the text otherwise it is applied to the data. If the value is a list of strings, the search is interpreted as a search for any of the strings in the list. For dictionaries, the following keys are supported:
|
required |
Returns:
Type | Description |
---|---|
Iterator[str]
|
An iterator over the document ids that match the search criteria. |
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> corpus.add_layer_meta("words", layer_type="span", base="text")
>>> corpus.add_layer_meta("pos", layer_type="seq", base="words",
... data=["NOUN", "VERB", "ADJ"])
>>> corpus.add_layer_meta("lemma", layer_type="seq", base="words",
... data="string")
>>> doc = corpus.add_doc("Colorless green ideas sleep furiously.")
>>> doc.words = [(0, 9), (10, 15), (16, 21), (22, 27), (28, 37)]
>>> doc.pos = ["ADJ", "ADJ", "NOUN", "VERB", "ADV"]
>>> doc.lemma = ["colorless", "green", "idea", "sleep", "furiously"]
>>> list(corpus.search(pos="NOUN"))
['9wpe']
>>> list(corpus.search(pos=["NOUN", "VERB"]))
['9wpe']
>>> list(corpus.search(pos={"$in": ["NOUN", "VERB"]}))
['9wpe']
>>> list(corpus.search(pos={"$regex": "N.*"}))
['9wpe']
>>> list(corpus.search(pos="VERB", lemma="sleep"))
['9wpe']
>>> list(corpus.search(pos="VERB", words="idea"))
[]
>>> list(corpus.search(pos="VERB", words="ideas"))
['9wpe']
>>> list(corpus.search({"pos": "VERB", "lemma": "sleep"}))
['9wpe']
>>> list(corpus.search({"$and": {"pos": "VERB", "lemma": "sleep"}}))
['9wpe']
Source code in teanga/corpus.py
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 |
|
subset(values)
Create a new corpus that is a subset of the current corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Union[Iterable[str], Iterable[int]]
|
Union[iterable[str], iterable[int]] The document ids or indices to include in the subset. |
required |
Returns:
Type | Description |
---|---|
ImmutableCorpus
|
A new Corpus object that contains only the documents specified by |
Examples:
>>> corpus = text_corpus()
>>> doc1 = corpus.add_doc("This is a document.")
>>> doc2 = corpus.add_doc("This is another document.")
>>> doc3 = corpus.add_doc("This is yet another document.")
>>> subset = corpus.subset(["Kjco", "eDFn"])
>>> list(subset.doc_ids)
['Kjco', 'eDFn']
>>> subset = corpus.subset(range(2))
>>> list(subset.doc_ids)
['Kjco', 'eDFn']
Source code in teanga/corpus.py
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 |
|
text_freq(layer, condition=None)
Get the frequency of a text string in the corpus.
Returns:
Type | Description |
---|---|
dict[str, int]
|
A dictionary with the frequency of each string. |
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> doc.tokens = [(0, 4), (5, 7), (8, 9), (10, 18)]
>>> corpus.text_freq("tokens")
Counter({'This': 1, 'is': 1, 'a': 1, 'document': 1})
>>> corpus.text_freq("tokens", lambda x: "i" in x)
Counter({'This': 1, 'is': 1})
Source code in teanga/corpus.py
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 |
|
to_cuac(path)
Write the corpus to a Cuac file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
str The path to the Cuac file. |
required |
Source code in teanga/corpus.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
|
to_json(path_or_buf)
Write the corpus to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str The path to the json file or a buffer. |
required |
Source code in teanga/corpus.py
802 803 804 805 806 807 808 809 810 811 812 813 814 |
|
to_json_str()
Write the corpus to a JSON string.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
>>> corpus.to_json_str()
'{"_meta": {"text": {"type": "characters"}}, "_order": ["Kjco"], "Kjco": {"text": "This is a document."}}'
Source code in teanga/corpus.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
|
to_yaml(path_or_buf)
Write the corpus to a yaml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str
|
str The path to the yaml file or a buffer. |
required |
Source code in teanga/corpus.py
722 723 724 725 726 727 728 729 730 731 732 733 734 |
|
to_yaml_str()
Write the corpus to a yaml string.
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> doc = corpus.add_doc("This is a document.")
>>> corpus.to_yaml_str()
'_meta:\n text:\n type: characters\nKjco:\n text: This is a document.\n'
Source code in teanga/corpus.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
transform(layer, transform)
Transform a layer in the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer
|
str
|
str The name of the layer to transform. |
required |
transform
|
Callable[[str], str]
|
Callable[[str], str] The transformation function. |
required |
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> corpus = corpus.transform("text", lambda x: x[:10])
>>> list(corpus.docs)
[Document('Kjco', {'text': 'This is a '})]
Source code in teanga/corpus.py
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
|
upper()
Uppercase all the text in the corpus.
Examples:
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> corpus = corpus.upper()
>>> list(corpus.docs)
[Document('Kjco', {'text': 'THIS IS A DOCUMENT.'})]
Source code in teanga/corpus.py
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 |
|
val_freq(layer, condition=None)
Get the frequency of a value in a layer.
Returns:
Type | Description |
---|---|
Counter
|
A dictionary with the frequency of each value. |
Examples:
>>> corpus = Corpus()
>>> corpus.add_layer_meta("text")
>>> corpus.add_layer_meta("words", layer_type="span", base="text")
>>> corpus.add_layer_meta("pos", layer_type="seq", base="words",
... data=["NOUN", "VERB", "ADJ"])
>>> doc = corpus.add_doc("Colorless green ideas sleep furiously.")
>>> doc.words = [(0, 9), (10, 15), (16, 21), (22, 28), (29, 37)]
>>> doc.pos = ["ADJ", "ADJ", "NOUN", "VERB", "ADV"]
>>> corpus.val_freq("pos")
Counter({'ADJ': 2, 'NOUN': 1, 'VERB': 1, 'ADV': 1})
>>> corpus.val_freq("pos", ["NOUN", "VERB"])
Counter({'NOUN': 1, 'VERB': 1})
>>> corpus.val_freq("pos", lambda x: x[0] == "A")
Counter({'ADJ': 2, 'ADV': 1})
Source code in teanga/corpus.py
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 |
|
view(*args)
Create a view on the corpus.
args: str
The names of the layers to view.
Examples:
corpus = Corpus() corpus.add_layer_meta("text") corpus.add_layer_meta("words", layer_type="span", base="text") corpus.add_layer_meta("sentences", layer_type="div", base="words") doc = corpus.add_doc("This is a sentence. This is another sentence.") doc.words = [(0, 4), (5, 7), (8, 9), (10, 18), (20, 24), (25, 27), ... (28, 35), (36, 44)] doc.sentences = [0, 4] doc.view("words", "sentences") [['This', 'is', 'a', 'sentence'], ['This', 'is', 'another', 'sentence']]
Source code in teanga/corpus.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
writer(buf)
Create a writer object that can serialize documents in a streaming fashion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buf
|
str The buffer to write to. |
required |
Examples:
>>> import io
>>> corpus = text_corpus()
>>> doc = corpus.add_doc("This is a document.")
>>> string = io.StringIO()
>>> with corpus.writer(string) as writer:
... for doc in corpus.docs:
... writer.write(doc)
Source code in teanga/corpus.py
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
|
download(name, db_file=None)
Load a corpus by name from a remote server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
str The name of the corpus to download. |
required |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Source code in teanga/corpus.py
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 |
|
from_url(url, db_file=None)
Read a corpus from a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
str The URL to read the corpus from. |
required |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Source code in teanga/corpus.py
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 |
|
parallel_corpus(languages, db_file=None, alignments=None)
Create a corpus with a character layer and token layer for each language
Parameters:
Name | Type | Description | Default |
---|---|---|---|
languages
|
list[str]
|
list[str] The languages to create the corpus for |
required |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
alignments
|
Optional[List[Tuple[str, str]]]
|
A list of pairs of language to add an alignment field for. |
None
|
Returns:
Type | Description |
---|---|
Corpus
|
A corpus with a character layer and token layer for each language |
Examples:
>>> corpus = parallel_corpus(["en","de","nl"],
... alignments=[("en","de"),("en","nl")])
>>> doc = corpus.add_doc(en="hello, world", de="Hallo, Welt!")
>>> doc.en_tokens = [(0,5), (7,12)]
>>> doc.de_tokens = [(0,5), (7,11)]
>>> doc.en_de_alignments = [(0,0),(1,1)]
Source code in teanga/corpus.py
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 |
|
parse(path_or_buf)
Parse a corpus incrementally from a file or buffer. Note that you will need to load this into a Corpus object directly
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str
|
str The path to the file or a buffer. |
required |
Examples:
>>> import io
>>> yaml_str = '''_meta:
... text:
... type: characters
... Kjco:
... text: This is a document.'''
>>> stream = parse(io.StringIO(yaml_str))
>>> corpus = Corpus()
>>> corpus._meta = stream.meta
>>> for doc in stream:
... _ = corpus.add_doc(doc)
Source code in teanga/corpus.py
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 |
|
read_cuac(file, db_file=None)
Read a corpus from a Cuac file. Requires teanga_pyo3 module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
str The path to the Cuac file. |
required |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Source code in teanga/corpus.py
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 |
|
read_json(path_or_buf, db_file=None)
Read a corpus from a json file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str The path to the json file or a buffer. |
required | |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Source code in teanga/corpus.py
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
|
read_json_str(json_str, db_file=None)
Read a corpus from a json string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_str
|
str
|
str The json string. |
required |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Examples:
>>> corpus = read_json_str('{"_meta": {"text": {"type": "characters"}},"Kjco": {"text": "This is a document."}}')
Source code in teanga/corpus.py
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
|
read_yaml(path_or_buf, db_file=None)
Read a corpus from a yaml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_buf
|
str The path to the yaml file or a buffer. |
required | |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Source code in teanga/corpus.py
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 |
|
read_yaml_str(yaml_str, db_file=None)
Read a corpus from a yaml string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_str
|
str The yaml string. |
required | |
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Examples:
>>> yaml_str = '''_meta:
... text:
... type: characters
... Kjco:
... text: This is a document.'''
>>> corpus = read_yaml_str(yaml_str)
Source code in teanga/corpus.py
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 |
|
teanga_db_fail()
Source code in teanga/corpus.py
1726 1727 1728 1729 1730 |
|
text_corpus(db_file=None)
Create a corpus with a text
and tokens
layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db_file
|
str
|
str The path to the database file, if the corpus should be stored in a database. |
None
|
Returns:
Type | Description |
---|---|
Corpus
|
A corpus with a |
Source code in teanga/corpus.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 |
|