From 699ba51684000f25e8063082d007a02856a3b6ee Mon Sep 17 00:00:00 2001 From: nobu-g Date: Thu, 18 Aug 2022 21:42:11 +0900 Subject: [PATCH 1/2] hotfix: fix SEM_PAT --- src/rhoknp/props/semantics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rhoknp/props/semantics.py b/src/rhoknp/props/semantics.py index 169a44b8..2fd2f27c 100644 --- a/src/rhoknp/props/semantics.py +++ b/src/rhoknp/props/semantics.py @@ -7,7 +7,7 @@ class SemanticsDict(dict[str, Union[str, bool]]): NIL = "NIL" PAT = re.compile(rf'(?P("([^"]|\\")+?")|{NIL})') - SEM_PAT = re.compile(r"(?P[^:]+)(:(?P\S+))?\s?") + SEM_PAT = re.compile(r"(?P[^:\s]+)(:(?P\S+))?(\s|$)") def __init__(self, semantics: dict[str, Union[str, bool]] = None, is_nil: bool = False): if semantics is None: From 5b0eaa89adc8acd18a202fe07b3a50252aa5341f Mon Sep 17 00:00:00 2001 From: nobu-g Date: Thu, 18 Aug 2022 21:43:04 +0900 Subject: [PATCH 2/2] add tests for SemanticsDict --- src/rhoknp/props/semantics.py | 4 +--- tests/props/test_semantics.py | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/rhoknp/props/semantics.py b/src/rhoknp/props/semantics.py index 2fd2f27c..bd53fc65 100644 --- a/src/rhoknp/props/semantics.py +++ b/src/rhoknp/props/semantics.py @@ -32,10 +32,8 @@ def from_sstring(cls, sstring: str) -> "SemanticsDict": def to_sstring(self) -> str: """意味情報文字列に変換.""" - if self.is_nil: - return self.NIL if len(self) == 0: - return "" + return "" if self.is_nil is False else self.NIL return f'"{" ".join(self._item_to_sstring(k, v) for k, v in self.items())}"' @staticmethod diff --git a/tests/props/test_semantics.py b/tests/props/test_semantics.py index c6d8f9c5..89ccb2ac 100644 --- a/tests/props/test_semantics.py +++ b/tests/props/test_semantics.py @@ -1,12 +1,35 @@ +from typing import Any + import pytest from rhoknp.props import SemanticsDict +CASES = [ + { + "sstring": '"代表表記:天気/てんき カテゴリ:抽象物"', + "dict_": {"代表表記": "天気/てんき", "カテゴリ": "抽象物"}, + }, + { + "sstring": '"代表表記:新/しん 内容語 NE:ORGANIZATION:head"', + "dict_": {"代表表記": "新/しん", "内容語": True, "NE": "ORGANIZATION:head"}, + }, + { + "sstring": "NIL", + "dict_": {}, + }, +] + + +@pytest.mark.parametrize("case", CASES) +def test_from_fstring(case: dict[str, Any]) -> None: + semantics = SemanticsDict.from_sstring(case["sstring"]) + assert dict(semantics) == case["dict_"] -@pytest.mark.parametrize("sstring", ['"代表表記:天気/てんき カテゴリ:抽象物"', "NIL"]) -def test_from_fstring(sstring: str) -> None: - semantics = SemanticsDict.from_sstring(sstring) - assert semantics.to_sstring() == sstring + +@pytest.mark.parametrize("case", CASES) +def test_to_fstring(case: dict[str, Any]) -> None: + semantics = SemanticsDict(case["dict_"], is_nil=True) + assert semantics.to_sstring() == case["sstring"] def test_false(): @@ -14,7 +37,7 @@ def test_false(): def test_empty_dict(): - semantics = SemanticsDict() + semantics = SemanticsDict({}) assert semantics.to_sstring() == ""