-
Notifications
You must be signed in to change notification settings - Fork 0
Codex-generated pull request #45
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
Merged
rad1092
merged 1 commit into
main
from
codex/add-synonym-definitions-in-schema_semantics_ko.json
Feb 15, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| SCHEMA_SEMANTICS_PATH = Path(__file__).resolve().parents[1] / "resources" / "schema_semantics_ko.json" | ||
|
|
||
|
|
||
| @dataclass | ||
| class AliasConcept: | ||
| canonical: str | ||
| aliases: list[str] | ||
| column_aliases: list[str] | ||
|
|
||
|
|
||
| @dataclass | ||
| class AliasMatch: | ||
| user_term: str | ||
| status: str | ||
| canonical: str | None = None | ||
| matched_column: str | None = None | ||
| candidates: list[str] | None = None | ||
|
|
||
| def to_dict(self) -> dict[str, Any]: | ||
| return { | ||
| "user_term": self.user_term, | ||
| "status": self.status, | ||
| "canonical": self.canonical, | ||
| "matched_column": self.matched_column, | ||
| "candidates": self.candidates or [], | ||
| } | ||
|
|
||
|
|
||
| def _norm(text: str) -> str: | ||
| return "".join(ch for ch in str(text).strip().lower() if not ch.isspace() and ch not in "_-") | ||
|
|
||
|
|
||
| def load_schema_semantics(path: str | Path | None = None) -> list[AliasConcept]: | ||
| semantics_path = Path(path) if path else SCHEMA_SEMANTICS_PATH | ||
| raw = json.loads(semantics_path.read_text(encoding="utf-8")) | ||
| items = raw.get("concepts", []) if isinstance(raw, dict) else [] | ||
|
|
||
| concepts: list[AliasConcept] = [] | ||
| for item in items: | ||
| if not isinstance(item, dict): | ||
| continue | ||
| canonical = str(item.get("canonical", "")).strip() | ||
| if not canonical: | ||
| continue | ||
| aliases = [str(x).strip() for x in item.get("aliases", []) if str(x).strip()] | ||
| col_aliases = [str(x).strip() for x in item.get("column_aliases", []) if str(x).strip()] | ||
| concepts.append(AliasConcept(canonical=canonical, aliases=aliases, column_aliases=col_aliases)) | ||
| return concepts | ||
|
|
||
|
|
||
| def match_alias_to_column(user_term: str, columns: list[str], concepts: list[AliasConcept]) -> AliasMatch: | ||
| term_key = _norm(user_term) | ||
| if not term_key: | ||
| return AliasMatch(user_term=user_term, status="failed") | ||
|
|
||
| concept: AliasConcept | None = None | ||
| for c in concepts: | ||
| vocab = {_norm(c.canonical), *(_norm(a) for a in c.aliases)} | ||
| if term_key in vocab: | ||
| concept = c | ||
| break | ||
|
|
||
| if concept is None: | ||
| return AliasMatch(user_term=user_term, status="failed") | ||
|
|
||
| candidate_keys = {_norm(concept.canonical), *(_norm(a) for a in concept.column_aliases), *(_norm(a) for a in concept.aliases)} | ||
| candidates = [col for col in columns if _norm(col) in candidate_keys] | ||
|
|
||
| if len(candidates) == 1: | ||
| return AliasMatch( | ||
| user_term=user_term, | ||
| status="success", | ||
| canonical=concept.canonical, | ||
| matched_column=candidates[0], | ||
| candidates=candidates, | ||
| ) | ||
| if len(candidates) > 1: | ||
| return AliasMatch( | ||
| user_term=user_term, | ||
| status="ambiguous", | ||
| canonical=concept.canonical, | ||
| candidates=candidates, | ||
| ) | ||
| return AliasMatch(user_term=user_term, status="failed", canonical=concept.canonical) | ||
|
|
||
|
|
||
| def normalize_question_entities(question: str, columns: list[str], concepts: list[AliasConcept]) -> dict[str, Any]: | ||
| normalized_question = question | ||
| mappings: list[AliasMatch] = [] | ||
|
|
||
| for concept in concepts: | ||
| terms = [concept.canonical, *concept.aliases] | ||
| for term in terms: | ||
| if term and term in question: | ||
| match = match_alias_to_column(term, columns, concepts) | ||
| mappings.append(match) | ||
| if match.status == "success" and match.matched_column: | ||
| normalized_question = normalized_question.replace(term, match.matched_column) | ||
| break | ||
|
|
||
| deduped: dict[str, AliasMatch] = {} | ||
| for item in mappings: | ||
| deduped[item.user_term] = item | ||
|
|
||
| return { | ||
| "normalized_question": normalized_question, | ||
| "mappings": [m.to_dict() for m in deduped.values()], | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "concepts": [ | ||
| { | ||
| "canonical": "시군구", | ||
| "aliases": ["구군", "지역구"], | ||
| "column_aliases": ["sigungu_col", "sigungu", "시군구명", "구군명"] | ||
| }, | ||
| { | ||
| "canonical": "세차유형", | ||
| "aliases": ["서비스타입"], | ||
| "column_aliases": ["service_type_col", "service_type", "세차유형코드", "서비스타입"] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from bitnet_tools.schema_semantics import ( | ||
| load_schema_semantics, | ||
| match_alias_to_column, | ||
| normalize_question_entities, | ||
| ) | ||
|
|
||
|
|
||
| def test_schema_semantics_success_mapping(): | ||
| concepts = load_schema_semantics() | ||
| match = match_alias_to_column('시군구', ['sigungu_col', 'service_type_col'], concepts) | ||
|
|
||
| assert match.status == 'success' | ||
| assert match.matched_column == 'sigungu_col' | ||
|
|
||
|
|
||
| def test_schema_semantics_failed_mapping(): | ||
| concepts = load_schema_semantics() | ||
| match = match_alias_to_column('없는용어', ['sigungu_col', 'service_type_col'], concepts) | ||
|
|
||
| assert match.status == 'failed' | ||
| assert match.matched_column is None | ||
|
|
||
|
|
||
| def test_schema_semantics_ambiguous_mapping(): | ||
| concepts = load_schema_semantics() | ||
| match = match_alias_to_column('세차유형', ['service_type_col', 'service_type'], concepts) | ||
|
|
||
| assert match.status == 'ambiguous' | ||
| assert sorted(match.candidates or []) == ['service_type', 'service_type_col'] | ||
|
|
||
|
|
||
| def test_normalize_question_entities_replaces_with_column_name(): | ||
| concepts = load_schema_semantics() | ||
| result = normalize_question_entities('시군구 별 세차유형 비율을 보여줘', ['sigungu_col', 'service_type_col'], concepts) | ||
|
|
||
| assert result['normalized_question'] == 'sigungu_col 별 service_type_col 비율을 보여줘' | ||
| assert len(result['mappings']) == 2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalize_question_entitiesmatches aliases with plain substring checks and then runs globalstr.replace, so terms that appear inside longer words are rewritten incorrectly. For example, with column시군구명and question시군구명 별 ..., the canonical term시군구matches and replacement produces시군구명명 ..., which then feeds a corrupted question into the prompt. This can silently degrade analysis whenever a concept term is a prefix/subsequence of another token.Useful? React with 👍 / 👎.