From b806f15cc27d42d441445118531db885ee1c5d48 Mon Sep 17 00:00:00 2001 From: mohaned mashaly Date: Sun, 21 Nov 2021 22:04:36 +0200 Subject: [PATCH 1/5] refactor: refactor prettyprinter to be more readable --- nltk/tree/prettyprinter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nltk/tree/prettyprinter.py b/nltk/tree/prettyprinter.py index 2ce106e055..667d7dcd29 100644 --- a/nltk/tree/prettyprinter.py +++ b/nltk/tree/prettyprinter.py @@ -75,7 +75,7 @@ def __init__(self, tree, sentence=None, highlight=()): leaves = tree.leaves() if ( leaves - and not any(len(a) == 0 for a in tree.subtrees()) + and any(len(a) > 0 for a in tree.subtrees()) and all(isinstance(a, int) for a in leaves) ): sentence = [str(a) for a in leaves] @@ -207,7 +207,7 @@ def dumpmatrix(): raise ValueError("All leaves must be integer indices.") if len(leaves) != len(set(leaves)): raise ValueError("Indices must occur at most once.") - if not all(0 <= n < len(sentence) for n in leaves): + if not all(n in range(0,len(sentence)) for n in leaves): raise ValueError( "All leaves must be in the interval 0..n " "with n=len(sentence)\ntokens: %d indices: " @@ -291,7 +291,7 @@ def dumpmatrix(): matrix[rowidx][i] = ids[m] nodes[ids[m]] = tree[m] # add column to the set of children for its parent - if m != (): + if len(m) > 0: childcols[m[:-1]].add((rowidx, i)) assert len(positions) == 0 From 2d8ad635320f7e7f8660a9159b156a4e83b7dd7a Mon Sep 17 00:00:00 2001 From: mohaned mashaly Date: Sat, 27 Nov 2021 16:30:22 +0200 Subject: [PATCH 2/5] fix tests --- nltk/tree/prettyprinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nltk/tree/prettyprinter.py b/nltk/tree/prettyprinter.py index 667d7dcd29..b4bdf5f7db 100644 --- a/nltk/tree/prettyprinter.py +++ b/nltk/tree/prettyprinter.py @@ -77,7 +77,7 @@ def __init__(self, tree, sentence=None, highlight=()): leaves and any(len(a) > 0 for a in tree.subtrees()) and all(isinstance(a, int) for a in leaves) - ): + ): sentence = [str(a) for a in leaves] else: # this deals with empty nodes (frontier non-terminals) From efd801b24ec7dbaf06dbaa25c4a740dc78cb3ce2 Mon Sep 17 00:00:00 2001 From: mohaned mashaly Date: Sat, 27 Nov 2021 16:33:26 +0200 Subject: [PATCH 3/5] fix tests --- nltk/tree/prettyprinter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nltk/tree/prettyprinter.py b/nltk/tree/prettyprinter.py index b4bdf5f7db..7d04ef2381 100644 --- a/nltk/tree/prettyprinter.py +++ b/nltk/tree/prettyprinter.py @@ -77,7 +77,7 @@ def __init__(self, tree, sentence=None, highlight=()): leaves and any(len(a) > 0 for a in tree.subtrees()) and all(isinstance(a, int) for a in leaves) - ): + ): sentence = [str(a) for a in leaves] else: # this deals with empty nodes (frontier non-terminals) @@ -207,7 +207,7 @@ def dumpmatrix(): raise ValueError("All leaves must be integer indices.") if len(leaves) != len(set(leaves)): raise ValueError("Indices must occur at most once.") - if not all(n in range(0,len(sentence)) for n in leaves): + if not all(n in range(0, len(sentence)) for n in leaves): raise ValueError( "All leaves must be in the interval 0..n " "with n=len(sentence)\ntokens: %d indices: " From dc31b2cab81574a077e5ae234e7e59bdd852e862 Mon Sep 17 00:00:00 2001 From: mohaned mashaly Date: Sun, 28 Nov 2021 10:25:43 +0200 Subject: [PATCH 4/5] refactor to old if condition --- nltk/tree/prettyprinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nltk/tree/prettyprinter.py b/nltk/tree/prettyprinter.py index 7d04ef2381..cd4db6b062 100644 --- a/nltk/tree/prettyprinter.py +++ b/nltk/tree/prettyprinter.py @@ -207,7 +207,7 @@ def dumpmatrix(): raise ValueError("All leaves must be integer indices.") if len(leaves) != len(set(leaves)): raise ValueError("Indices must occur at most once.") - if not all(n in range(0, len(sentence)) for n in leaves): + if not all(0 <= n < len(sentence) for n in leaves): raise ValueError( "All leaves must be in the interval 0..n " "with n=len(sentence)\ntokens: %d indices: " From 3eb6d41e9d620363b9c416302d9d1950fa6f8c47 Mon Sep 17 00:00:00 2001 From: mohaned mashaly Date: Sun, 28 Nov 2021 21:18:17 +0200 Subject: [PATCH 5/5] replace all with any --- nltk/tree/prettyprinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nltk/tree/prettyprinter.py b/nltk/tree/prettyprinter.py index cd4db6b062..24337ae329 100644 --- a/nltk/tree/prettyprinter.py +++ b/nltk/tree/prettyprinter.py @@ -75,7 +75,7 @@ def __init__(self, tree, sentence=None, highlight=()): leaves = tree.leaves() if ( leaves - and any(len(a) > 0 for a in tree.subtrees()) + and all(len(a) > 0 for a in tree.subtrees()) and all(isinstance(a, int) for a in leaves) ): sentence = [str(a) for a in leaves]