Returns a new Constraint from the given string. Uppercase words indicate either a tag ("NN", "JJ", "VP") or a taxonomy term (e.g., "PRODUCT", "PERSON"). Syntax: ( defines an optional constraint, e.g., "(JJ)". [ defines a constraint with sp
(cls, s, **kwargs)
| 461 | |
| 462 | @classmethod |
| 463 | def fromstring(cls, s, **kwargs): |
| 464 | """ Returns a new Constraint from the given string. |
| 465 | Uppercase words indicate either a tag ("NN", "JJ", "VP") |
| 466 | or a taxonomy term (e.g., "PRODUCT", "PERSON"). |
| 467 | Syntax: |
| 468 | ( defines an optional constraint, e.g., "(JJ)". |
| 469 | [ defines a constraint with spaces, e.g., "[Mac OS X | Windows Vista]". |
| 470 | _ is converted to spaces, e.g., "Windows_Vista". |
| 471 | | separates different options, e.g., "ADJP|ADVP". |
| 472 | ! can be used as a word prefix to disallow it. |
| 473 | * can be used as a wildcard character, e.g., "soft*|JJ*". |
| 474 | ? as a suffix defines a constraint that is optional, e.g., "JJ?". |
| 475 | + as a suffix defines a constraint that can span multiple words, e.g., "JJ+". |
| 476 | ^ as a prefix defines a constraint that can only match the first word. |
| 477 | These characters need to be escaped if used as content: "\(". |
| 478 | """ |
| 479 | C = cls(**kwargs) |
| 480 | s = s.strip() |
| 481 | s = s.strip("{}") |
| 482 | s = s.strip() |
| 483 | for i in range(3): |
| 484 | # Wrapping order of control characters is ignored: |
| 485 | # (NN+) == (NN)+ == NN?+ == NN+? == [NN+?] == [NN]+? |
| 486 | if s.startswith("^"): |
| 487 | s = s[1: ]; C.first = True |
| 488 | if s.endswith("+") and not s.endswith("\+"): |
| 489 | s = s[0:-1]; C.multiple = True |
| 490 | if s.endswith("?") and not s.endswith("\?"): |
| 491 | s = s[0:-1]; C.optional = True |
| 492 | if s.startswith("(") and s.endswith(")"): |
| 493 | s = s[1:-1]; C.optional = True |
| 494 | if s.startswith("[") and s.endswith("]"): |
| 495 | s = s[1:-1] |
| 496 | s = re.sub(r"^\\\^", "^", s) |
| 497 | s = re.sub(r"\\\+$", "+", s) |
| 498 | s = s.replace("\_", "&uscore;") |
| 499 | s = s.replace("_"," ") |
| 500 | s = s.replace("&uscore;", "_") |
| 501 | s = s.replace("&lparen;", "(") |
| 502 | s = s.replace("&rparen;", ")") |
| 503 | s = s.replace("[", "[") |
| 504 | s = s.replace("]", "]") |
| 505 | s = s.replace("&lcurly;", "{") |
| 506 | s = s.replace("&rcurly;", "}") |
| 507 | s = s.replace("\(", "(") |
| 508 | s = s.replace("\)", ")") |
| 509 | s = s.replace("\[", "[") |
| 510 | s = s.replace("\]", "]") |
| 511 | s = s.replace("\{", "{") |
| 512 | s = s.replace("\}", "}") |
| 513 | s = s.replace("\*", "*") |
| 514 | s = s.replace("\?", "?") |
| 515 | s = s.replace("\+", "+") |
| 516 | s = s.replace("\^", "^") |
| 517 | s = s.replace("\|", "⊢") |
| 518 | s = s.split("|") |
| 519 | s = [v.replace("⊢", "|").strip() for v in s] |
| 520 | for v in s: |