3454a55636
cffconvert / validate (push) Has been cancelled
ci-workflow / pre-commit (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (macos-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (ubuntu-latest) (push) Has been cancelled
ci-workflow / Minimal NLTK Download Test (windows-latest) (push) Has been cancelled
ci-workflow / Python 3.10 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.11 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.12 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.13 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14 on macos-latest (push) Has been cancelled
ci-workflow / Python 3.14t on macos-latest (push) Has been cancelled
ci-workflow / Python 3.10 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.11 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.12 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.13 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14 on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.14t on ubuntu-latest (push) Has been cancelled
ci-workflow / Python 3.10 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.11 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.12 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.13 on windows-latest (push) Has been cancelled
ci-workflow / Python 3.14 on windows-latest (push) Has been cancelled
29 lines
873 B
Python
29 lines
873 B
Python
from nltk.tree import Tree
|
|
|
|
|
|
def test_fromstring_allows_escaped_brackets():
|
|
for source in (r"(S \))", r"(S \()", r"(S (NP \)) (VP ok))"):
|
|
tree = Tree.fromstring(source)
|
|
assert str(tree) == source
|
|
|
|
|
|
def test_fromstring_escaped_bracket_leaf_and_label():
|
|
tree = Tree.fromstring(r"(S (\( x))")
|
|
assert tree[0].label() == r"\("
|
|
assert tree.leaves() == ["x"]
|
|
|
|
|
|
def test_fromstring_roundtrips_escaped_bracket_leaf():
|
|
# a real-world case: a leaf holding a smiley whose close paren is escaped
|
|
source = r"(EMOJI :\))"
|
|
tree = Tree.fromstring(source)
|
|
assert tree.label() == "EMOJI"
|
|
assert tree.leaves() == [r":\)"]
|
|
assert str(tree) == source
|
|
|
|
|
|
def test_fromstring_normal_parsing_unaffected():
|
|
tree = Tree.fromstring("(S (NP the cat) (VP sat))")
|
|
assert tree.label() == "S"
|
|
assert tree.leaves() == ["the", "cat", "sat"]
|