Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn hash_comment != "analyze" if output format is json #215

Merged
merged 5 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ginza/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def run(
files: List[str] = None,
):
assert model_path is None or ensure_model is None
if output_format in ["3", "json"] and hash_comment != "analyze":
print(
f'hash_comment="{hash_comment}" not permitted for JSON output. Forced to use hash_comment="analyze".',
file=sys.stderr
hiroshi-matsuda-rit marked this conversation as resolved.
Show resolved Hide resolved
)

if parallel_level <= 0:
level = max(1, cpu_count() + parallel_level)
Expand Down
18 changes: 15 additions & 3 deletions ginza/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ def output_file(tmpdir: Path) -> Path:

def _conllu_parsable(result: str):
for line in result.split("\n"):
if line.startswith("# text = ") or line.strip() == "":
if line.startswith("#") or line.strip() == "":
continue
if not len(line.strip().split("\t")) == 10:
raise Exception


def _cabocha_parsable(result: str):
for line in result.split("\n"):
if line.strip() in ("", "EOS") or line.startswith("*"):
if line.strip() in ("", "EOS") or line.startswith("*") or line.startswith("#"):
continue
if not len(line.split("\t")) == 3:
raise Exception
Expand All @@ -74,7 +74,7 @@ def _cabocha_parsable(result: str):

def _mecab_parsable(result: str):
for line in result.split("\n"):
if line.strip() in ("", "EOS"):
if line.strip() in ("", "EOS") or line.startswith("#"):
continue
if not len(line.split("\t")) == 2:
raise Exception
Expand Down Expand Up @@ -210,6 +210,18 @@ def test_output_format(self, output_format, result_parsable, input_file):
assert p.returncode == 0
result_parsable(p.stdout.strip())

@pytest.mark.parametrize(
"hash_comment", ["print", "skip"]
)
def test_warn_if_json_hash_comment_not_analyze(self, hash_comment, input_file):
p = run_cmd(["ginza", "-c", hash_comment, "-f", "json", input_file], stderr=sp.PIPE)
assert p.returncode == 0
msg = (
f'hash_comment={hash_comment} may break output json if input contains a line starts with "#".\n'
'In order to keep the json in proper format, please use hash_comment=analyze or remove the lines start with "#" from input.'
)
assert msg in p.stderr

def test_require_gpu(self, input_file):
p = run_cmd(["ginza", "-g", input_file])
gpu_available = int(os.environ.get("CUDA_VISIBLE_DEVICES", -1)) > 0
Expand Down