From 4eccbf1684187684f52047f8cb1df9fcdc69a083 Mon Sep 17 00:00:00 2001 From: Alex Hedges Date: Sun, 4 Jun 2023 18:46:40 -0400 Subject: [PATCH] Ignore keyword order for keyword-based grammars The order of the input list affected the final grammar, which is unintuitive behavior. The keywords are now sorted to prevent unnecessary differences between grammars. In addition, I removed the documentation that `keywords` could be a list of `DAIDEObject`s because the code itself does not support that. --- src/daidepp/grammar/grammar_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/daidepp/grammar/grammar_utils.py b/src/daidepp/grammar/grammar_utils.py index c90144d..4bfae0c 100644 --- a/src/daidepp/grammar/grammar_utils.py +++ b/src/daidepp/grammar/grammar_utils.py @@ -226,8 +226,7 @@ def create_grammar_from_press_keywords( Parameters ---------- keywords : List[PressKeywords] - List of press keywords. Although the type hint says List[PressKeywords], - this can be a list of string literals or DAIDEObjects (to avoid circular imports). + List of press keywords. allow_just_arrangement : bool, optional if set to True, the parser accepts strings that are only arrangements, in addition to press messages. So, for example, the parser could parse, by default False @@ -247,6 +246,9 @@ def create_grammar_from_press_keywords( if allow_just_arrangement and string_type == "message": string_type = "arrangement" + # The input order or any duplicate elements should not affect the generated grammar + keywords = sorted(set(keywords)) + full_grammar = create_daide_grammar( level=MAX_DAIDE_LEVEL, string_type=string_type,