Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-g committed Aug 18, 2022
2 parents a2c1c70 + 5b0eaa8 commit f5fbc75
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/rhoknp/props/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SemanticsDict(dict[str, Union[str, bool]]):

NIL = "NIL"
PAT = re.compile(rf'(?P<sems>("([^"]|\\")+?")|{NIL})')
SEM_PAT = re.compile(r"(?P<key>[^:]+)(:(?P<value>\S+))?\s?")
SEM_PAT = re.compile(r"(?P<key>[^:\s]+)(:(?P<value>\S+))?(\s|$)")

def __init__(self, semantics: dict[str, Union[str, bool]] = None, is_nil: bool = False):
if semantics is None:
Expand All @@ -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
Expand Down
33 changes: 28 additions & 5 deletions tests/props/test_semantics.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
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():
assert SemanticsDict._item_to_sstring("sem", False) == ""


def test_empty_dict():
semantics = SemanticsDict()
semantics = SemanticsDict({})
assert semantics.to_sstring() == ""


Expand Down

0 comments on commit f5fbc75

Please sign in to comment.