API Reference¶
Main functions¶
hyperbase
¶
hedge(source)
¶
Create a hyperedge.
Source code in src/hyperbase/hyperedge.py
Hyperedge module¶
hyperbase.hyperedge
¶
Hyperedge
¶
Bases: tuple
Non-atomic hyperedge.
Source code in src/hyperbase/hyperedge.py
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 | |
atom
property
¶
True if edge is an atom.
not_atom
property
¶
True if edge is not an atom.
t
property
¶
Edge type. (this porperty is a shortcut for Hyperedge.type())
mt
property
¶
Edge main type. (this porperty is a shortcut for Hyperedge.mtype())
ct
property
¶
Edge connector type. (this porperty is a shortcut for Hyperedge.connector_type())
cmt
property
¶
Edge connector main type. (this porperty is a shortcut for Hyperedge.mconnector_type())
is_atom()
¶
.. deprecated:: 0.6.0 Please use the properties .atom and .not_atom instead.
Checks if edge is an atom.
to_str(roots_only=False)
¶
Converts edge to its string representation.
Keyword argument: roots_only -- only the roots of the atoms will be used to create the string representation.
Source code in src/hyperbase/hyperedge.py
label()
¶
Generate human-readable label for edge.
Source code in src/hyperbase/hyperedge.py
inner_atom()
¶
The inner atom inside of a modifier structure.
For example, condider: (red/M shoes/C) The inner atom is: shoes/C Or, the more complex case: ((and/J slow/M steady/M) go/P) Yields: gp/P
This method should not be used on structures that contain more than one inner atom, for example concepts constructed with builders or relations.
The inner atom of an atom is itself.
Source code in src/hyperbase/hyperedge.py
connector_atom()
¶
The inner atom of the connector.
For example, condider: (does/M (not/M like/P.so) john/C chess/C) The connector atom is: like/P.so
The connector atom of an atom is None.
Source code in src/hyperbase/hyperedge.py
atoms()
¶
Returns the set of atoms contained in the edge.
For example, consider the edge: (the/md (of/br mayor/cc (the/md city/cs))) in this case, edge.atoms() returns: [the/md, of/br, mayor/cc, city/cs]
Source code in src/hyperbase/hyperedge.py
all_atoms()
¶
Returns a list of all the atoms contained in the edge. Unlike atoms(), which does not return repeated atoms, all_atoms() does return repeated atoms if they are different objects.
For example, consider the edge: (the/md (of/br mayor/cc (the/md city/cs))) in this case, edge.all_atoms() returns: [the/md, of/br, mayor/cc, the/md, city/cs]
Source code in src/hyperbase/hyperedge.py
size()
¶
depth()
¶
roots()
¶
contains(needle, deep=False)
¶
Checks if 'needle' is contained in edge.
Keyword argument: deep -- search recursively (default False)
Source code in src/hyperbase/hyperedge.py
subedges()
¶
Returns all the subedges contained in the edge, including atoms and itself.
insert_first_argument(argument)
¶
Returns an edge built by placing 'argument' as the first item after the connector of this edge. If this edge is an atom, then it becomes the connector of the returned edge.
For example, considering the 'edge' (a) and the 'argument' (b), this function returns: (a b)
Considering the 'edge' (a b c) and the 'argument' (d e), it returns: (a (d e) b c)
Source code in src/hyperbase/hyperedge.py
connect(arguments)
¶
Returns an edge built by adding the items in 'arguments' to the end of this edge. 'arguments' must be a collection.
For example, connecting the edge (a b) with the 'arguments' (c d) produces: (a b c d)
Source code in src/hyperbase/hyperedge.py
sequence(entity, before, flat=True)
¶
Returns an edge built by sequencing the 'entity', if it's an atom, or the elements of 'entity' if it is an edge, either before or after the elements of this edge.
If flat is False, then both this edge and 'entity' are treated as self-contained edges when building the new edge.
For example, connecting the edge (a b) and the 'entity' c produces, if before is True: (c a b) and if before is False: (a b c) Connecting the edge (a b) and the 'entity' (c d) produces, if before is True: (c d a b) and if before is False: (a b c d) This last example, if 'flat' is False, becomes respectively: ((c d) (a b)) ((a b) (c d))
Source code in src/hyperbase/hyperedge.py
replace_atom(old, new, unique=False)
¶
Returns edge built by replacing every instance of 'old' in this edge with 'new'.
Keyword argument: unique -- match only the exact same instance of the atom, i.e. UniqueAtom(self) == UniqueAtom(old) (default: False)
Source code in src/hyperbase/hyperedge.py
simplify(subtypes=False, argroles=False, namespaces=True)
¶
Returns a version of the edge with simplified atoms, for example removing subtypes, subroles or namespaces.
Keyword arguments: subtypes -- include subtypes (default: False). argroles --include argroles (default: False). namespaces -- include namespaces (default: True).
Source code in src/hyperbase/hyperedge.py
type()
¶
Returns the type of this edge as a string. Type inference is performed.
Source code in src/hyperbase/hyperedge.py
connector_type()
¶
Returns the type of the edge's connector. If the edge has no connector (i.e. it's an atom), then None is returned.
mtype()
¶
Returns the main type of this edge as a string of one character. Type inference is performed.
connector_mtype()
¶
Returns the main type of the edge's connector. If the edge has no connector (i.e. it's an atom), then None is returned.
Source code in src/hyperbase/hyperedge.py
atom_with_type(atom_type)
¶
Returns the first atom found in the edge that has the given 'atom_type', or whose type starts with 'atom_type'. If no such atom is found, returns None.
For example, given the edge (+/B a/Cn b/Cp) and the 'atom_type' c, this function returns: a/Cn If the 'atom_type' is 'Cp', the it will return: b/Cp
Source code in src/hyperbase/hyperedge.py
contains_atom_type(atom_type)
¶
Checks if the edge contains any atom with the given type. The edge is searched recursively, so the atom can appear at any depth.
Source code in src/hyperbase/hyperedge.py
argroles()
¶
Returns the argument roles string of the edge, if it exists. Otherwise returns empty string.
Argument roles can be return for the entire edge that they apply to, which can be a relation (R) or a concept (C). For example:
((not/M is/P.sc) bob/C sad/C) has argument roles "sc", (of/B.ma city/C berlin/C) has argument roles "ma".
Argument roles can also be returned for the connectors that define the outer edge, which can be of type predicate (P) or builder (B). For example:
(not/M is/P.sc) has argument roles "sc", of/B.ma has argument roles "ma".
Source code in src/hyperbase/hyperedge.py
has_argroles()
¶
replace_argroles(argroles)
¶
Returns an edge with the argroles of the connector atom replaced with the provided string. Returns same edge if the atom does not contain a role part.
Source code in src/hyperbase/hyperedge.py
insert_argrole(argrole, pos)
¶
Returns an edge with the given argrole inserted at the specified position in the argroles of the connector atom. Same restrictions as in replace_argroles() apply.
Source code in src/hyperbase/hyperedge.py
insert_edge_with_argrole(edge, argrole, pos)
¶
Returns a new edge with the provided edge and its argroles inserted at the specified position.
Source code in src/hyperbase/hyperedge.py
edges_with_argrole(argrole)
¶
Returns the list of edges with the given argument role.
Source code in src/hyperbase/hyperedge.py
main_concepts()
¶
Returns the list of main concepts in an concept edge. A main concept is a central concept in a built concept, e.g.: in ('s/Bp.am zimbabwe/Cp economy/Cn.s), economy/Cn.s is the main concept.
If entity is not an edge, or its connector is not of type builder, or the builder does not contain concept role annotations, or no concept is annotated as the main one, then an empty list is returned.
Source code in src/hyperbase/hyperedge.py
replace_main_concept(new_main)
¶
TODO: document and test
Source code in src/hyperbase/hyperedge.py
Atom
¶
Bases: Hyperedge
Atomic hyperedge.
Source code in src/hyperbase/hyperedge.py
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 951 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 | |
parens
property
¶
Whether this atom has parentheses.
atom
property
¶
True if edge is an atom.
not_atom
property
¶
True if edge is not an atom.
is_atom()
¶
.. deprecated:: 0.6.0 Please use the properties .atom and .not_atom instead.
Checks if edge is an atom.
parts()
¶
root()
¶
replace_atom_part(part_pos, part)
¶
Build a new atom by replacing an atom part in a given atom.
Source code in src/hyperbase/hyperedge.py
to_str(roots_only=False)
¶
Converts atom to its string representation.
Keyword argument: roots_only -- only the roots of the atoms will be used to create the string representation.
Source code in src/hyperbase/hyperedge.py
label()
¶
Generate human-readable label from entity.
Source code in src/hyperbase/hyperedge.py
inner_atom()
¶
The inner atom inside of a modifier structure.
For example, condider: (red/M shoes/C) The inner atom is: shoes/C Or, the more complex case: ((and/J slow/M steady/M) go/P) Yields: gp/P
This method should not be used on structures that contain more than one inner atom, for example concepts constructed with builders or relations.
The inner atom of an atom is itself.
Source code in src/hyperbase/hyperedge.py
connector_atom()
¶
The inner atom of the connector.
For example, condider: (does/M (not/M like/P.so) john/C chess/C) The connector atom is: like/P.so
The connector atom of an atom is None.
Source code in src/hyperbase/hyperedge.py
atoms()
¶
Returns the set of atoms contained in the edge.
For example, consider the edge: (the/Md (of/Br mayor/Cc (the/Md city/Cs))) in this case, edge.atoms() returns: [the/Md, of/Br, mayor/Cc, city/Cs]
Source code in src/hyperbase/hyperedge.py
all_atoms()
¶
Returns a list of all the atoms contained in the edge. Unlike atoms(), which does not return repeated atoms, all_atoms() does return repeated atoms if they are different objects.
For example, consider the edge: (the/Md (of/Br mayor/Cc (the/Md city/Cs))) in this case, edge.all_atoms() returns: [the/Md, of/Br, mayor/Cc, the/Md, city/Cs]
Source code in src/hyperbase/hyperedge.py
size()
¶
depth()
¶
roots()
¶
contains(needle, deep=False)
¶
Checks if 'needle' is contained in edge.
Keyword argument: deep -- search recursively (default: False)
subedges()
¶
insert_first_argument(argument)
¶
Returns an edge built by placing 'argument' as the first item after the connector of this edge. If this edge is an atom, then it becomes the connector of the returned edge.
For example, considering the 'edge' (a) and the 'argument' (b), this function returns: (a b)
Considering the 'edge' (a b c) and the 'argument' (d e), it returns: (a (d e) b c)
Source code in src/hyperbase/hyperedge.py
replace_atom(old, new, unique=False)
¶
Returns edge built by replacing every instance of 'old' in this edge with 'new'.
Keyword argument: unique -- match only the exact same instance of the atom, i.e. UniqueAtom(self) == UniqueAtom(old) (default: False)
Source code in src/hyperbase/hyperedge.py
role()
¶
Returns the role of this atom as a list of the subrole strings.
The role of an atom is its second part, right after the root. A dot notation is used to separate the subroles. For example, the role of hyperbase/Cp.s/1 is:
Cp.s
For this case, this function returns:
['Cp', 's']
If the atom only has a root, it is assumed to be a conjunction. In this case, this function returns the role with just the generic conjunction type:
['J'].
Source code in src/hyperbase/hyperedge.py
simplify(subtypes=False, argroles=False, namespaces=True)
¶
Returns a simplified version of the atom, for example removing subtypes, subroles or namespaces.
Keyword arguments: subtypes -- include subtype (default: False). argroles --include argroles (default: False). namespaces -- include namespaces (default: True).
Source code in src/hyperbase/hyperedge.py
type()
¶
Returns the type of the atom.
The type of an atom is its first subrole. For example, the type of hyperbase/Cp.s/1 is 'Cp'.
If the atom only has a root, it is assumed to be a conjunction. In this case, this function returns the generic conjunction type: 'J'.
Source code in src/hyperbase/hyperedge.py
connector_type()
¶
Returns the type of the edge's connector. If the edge has no connector (i.e. it's an atom), then None is returned.
atom_with_type(atom_type)
¶
Returns the first atom found in the edge that has the given 'atom_type', or whose type starts with 'atom_type'. If no such atom is found, returns None.
For example, given the edge (+/B a/Cn b/Bp) and the 'atom_type' C, this function returns: a/Cn If the 'atom_type' is 'Cp', the it will return: b/Cp
Source code in src/hyperbase/hyperedge.py
argroles()
¶
Returns the argument roles string of the edge, if it exists. Otherwise returns empty string.
Argument roles can be return for the entire edge that they apply to, which can be a relation (R) or a concept (C). For example:
((not/M is/P.sc) bob/C sad/C) has argument roles "sc", (of/B.ma city/C berlin/C) has argument roles "ma".
Argument roles can also be returned for the connectors that define the outer edge, which can be of type predicate (P) or builder (B). For example:
(not/M is/P.sc) has argument roles "sc", of/B.ma has argument roles "ma".
Source code in src/hyperbase/hyperedge.py
replace_argroles(argroles)
¶
Returns an atom with the argroles replaced with the provided string.
Source code in src/hyperbase/hyperedge.py
remove_argroles()
¶
Returns an atom with the argroles removed.
Source code in src/hyperbase/hyperedge.py
insert_argrole(argrole, pos)
¶
Returns an atom with the given argrole inserted at the specified position. Same restrictions as in replace_argroles() apply.
Source code in src/hyperbase/hyperedge.py
edges_with_argrole(argrole)
¶
main_concepts()
¶
Returns the list of main concepts in an concept edge. A main concept is a central concept in a built concept, e.g.: in ('s/Bp.am zimbabwe/Mp economy/Cn.s), economy/Cn.s is the main concept.
If entity is not an edge, or its connector is not of type builder, or the builder does not contain concept role annotations, or no concept is annotated as the main one, then an empty list is returned.
Source code in src/hyperbase/hyperedge.py
str2atom(s)
¶
Converts a string into a valid atom.
Source code in src/hyperbase/hyperedge.py
split_edge_str(edge_str)
¶
Shallow split into tokens of a string representation of an edge, without outer parenthesis.
Source code in src/hyperbase/hyperedge.py
hedge(source)
¶
Create a hyperedge.
Source code in src/hyperbase/hyperedge.py
build_atom(text, *parts)
¶
Build an atom from text and other parts.
Source code in src/hyperbase/hyperedge.py
Patterns module¶
hyperbase.patterns
¶
match_pattern(edge, pattern, curvars=None)
¶
Matches an edge to a pattern. This means that, if the edge fits the pattern, then a list of dictionaries will be returned. If the pattern specifies variables, then the returned dictionaries will be populated with the values for each pattern variable. There can be more than one dictionary in the list if there are multiple ways of matching the variables. If the pattern specifies no variables but the edge matches it, then a list with a single empty dictionary is returned. If the edge does not match the pattern, an empty list is returned.
Patterns are themselves edges. They can match families of edges by employing special atoms:
-> '*' represents a general wildcard (matches any entity)
-> '.' represents an atomic wildcard (matches any atom)
-> '(*)' represents an edge wildcard (matches any edge)
-> '...' at the end indicates an open-ended pattern.
The wildcards ('*', '.' and '(*)') can be used to specify variables, for example '*x', '(CLAIM)' or '.ACTOR'. In case of a match, these variables are assigned the hyperedge they correspond to. For example,
(1) the edge: (is/Pd (my/Mp name/Cn) mary/Cp) applied to the pattern: (is/Pd (my/Mp name/Cn) *NAME) produces the result: [{'NAME', mary/Cp}]
(2) the edge: (is/Pd (my/Mp name/Cn) mary/Cp) applied to the pattern: (is/Pd (my/Mp name/Cn) (NAME)) produces the result: [{}]
(3) the edge: (is/Pd (my/Mp name/Cn) mary/Cp) applied to the pattern: (is/Pd . *NAME) produces the result: []
Source code in src/hyperbase/patterns/entrypoints.py
edge_matches_pattern(edge, pattern, **kwargs)
¶
Check if an edge matches a pattern.
Patterns are themselves edges. They can match families of edges by employing special atoms:
-> '*' represents a general wildcard (matches any entity)
-> '.' represents an atomic wildcard (matches any atom)
-> '(*)' represents an edge wildcard (matches any edge)
-> '...' at the end indicates an open-ended pattern.
The pattern can be any valid hyperedge, including the above special atoms. Examples: (is/Pd hyperbase/C .) (says/Pd * ...)
Source code in src/hyperbase/patterns/entrypoints.py
is_wildcard(atom)
¶
Check if this atom defines a wildcard, i.e. if its root is a pattern matcher. (*, ., ..., if it is surrounded by parenthesis or variable label starting with an uppercase letter)
Source code in src/hyperbase/patterns/properties.py
is_pattern(edge)
¶
Check if this edge defines a pattern, i.e. if it includes at least one pattern matcher.
Pattern matcher are: - '*', '.', '(*)', '...' - variables (atom label starting with an uppercase letter) - argument role matcher (unordered argument roles surrounded by curly brackets) - functional patterns (var, atoms, lemma, ...)
Source code in src/hyperbase/patterns/properties.py
is_full_pattern(edge)
¶
Check if every atom is a pattern matcher.
Pattern matcher are: '*', '.', '(*)', '...', variables (atom label starting with an uppercase letter) and functional patterns.
Source code in src/hyperbase/patterns/properties.py
is_unordered_pattern(edge)
¶
Check if this edge defines an unordered pattern, i.e. if it includes at least one instance of unordered argument roles surrounded by curly brackets.
Source code in src/hyperbase/patterns/properties.py
Parsers package¶
hyperbase.parsers
¶
Parser
¶
Source code in src/hyperbase/parsers/parser.py
parse_batch(sentences)
¶
Parse multiple sentences. Subclasses may override with a true batched implementation (e.g. a single CT2 call).
Source code in src/hyperbase/parsers/parser.py
parse_text(text, batch_size=8, progress=False)
¶
Sentensize text, then parse all sentences in batches.
Returns a flat list of parse results across all sentences.
Source code in src/hyperbase/parsers/parser.py
list_parsers()
¶
Return all installed parser plugins.
Each plugin registers via the hyperbase.parsers entry-point group
in its pyproject.toml::
[project.entry-points."hyperbase.parsers"]
alphabeta = "hyperparser_alphabeta:ParserAlphaBeta"
Source code in src/hyperbase/parsers/__init__.py
get_parser(name, **kwargs)
¶
Instantiate a parser plugin by name.
Looks up name in the hyperbase.parsers entry-point group and
returns an instance of the registered :class:Parser subclass.
Raises :class:ValueError if the parser is not installed.
Source code in src/hyperbase/parsers/__init__.py
Parser module¶
hyperbase.parsers.parser
¶
Parser
¶
Source code in src/hyperbase/parsers/parser.py
parse_batch(sentences)
¶
Parse multiple sentences. Subclasses may override with a true batched implementation (e.g. a single CT2 call).
Source code in src/hyperbase/parsers/parser.py
parse_text(text, batch_size=8, progress=False)
¶
Sentensize text, then parse all sentences in batches.
Returns a flat list of parse results across all sentences.