forked from d-cloud/d-wiki
120 lines
3.7 KiB
Python
120 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from .yaml_compat import safe_load
|
|
from .tokenize import tokenize
|
|
|
|
QUERY_MATCH_STOP_TOKENS = {
|
|
"如何",
|
|
"怎么",
|
|
"使用",
|
|
"创建",
|
|
"知道",
|
|
"功能",
|
|
"资产",
|
|
}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AliasGroup:
|
|
name: str
|
|
terms: tuple[str, ...]
|
|
intent: str | None
|
|
related: tuple[str, ...]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class QueryEntry:
|
|
path: Path
|
|
title: str
|
|
aliases: tuple[str, ...]
|
|
intent: str | None
|
|
primary_sources: tuple[str, ...]
|
|
supporting_sources: tuple[str, ...]
|
|
answer_requirements: tuple[str, ...]
|
|
|
|
|
|
def load_alias_groups(path: Path | None) -> list[AliasGroup]:
|
|
if path is None or not path.exists():
|
|
return []
|
|
data = safe_load(path.read_text(encoding="utf-8")) or {}
|
|
raw_aliases = data.get("aliases", {})
|
|
if not isinstance(raw_aliases, dict):
|
|
return []
|
|
groups: list[AliasGroup] = []
|
|
for name, raw in raw_aliases.items():
|
|
if not isinstance(raw, dict):
|
|
continue
|
|
groups.append(
|
|
AliasGroup(
|
|
name=str(name),
|
|
terms=tuple(str(term).strip() for term in raw.get("terms", []) if str(term).strip()),
|
|
intent=str(raw["intent"]) if raw.get("intent") else None,
|
|
related=tuple(str(item).strip() for item in raw.get("related", []) if str(item).strip()),
|
|
)
|
|
)
|
|
return groups
|
|
|
|
|
|
def load_query_entries(directory: Path | None) -> list[QueryEntry]:
|
|
if directory is None or not directory.exists():
|
|
return []
|
|
return [_query_entry(path) for path in sorted(directory.rglob("*.md")) if path.is_file()]
|
|
|
|
|
|
def frontmatter(text: str) -> tuple[dict[str, Any], str]:
|
|
if not text.startswith("---\n"):
|
|
return {}, text
|
|
end = text.find("\n---", 4)
|
|
if end < 0:
|
|
return {}, text
|
|
meta = safe_load(text[4:end]) or {}
|
|
if not isinstance(meta, dict):
|
|
meta = {}
|
|
return meta, text[end + 4 :].lstrip("\n")
|
|
|
|
|
|
def query_matches_entry(query: str, entry: QueryEntry) -> bool:
|
|
normalized = normalize(query)
|
|
if any(normalize(alias) in normalized or normalized in normalize(alias) for alias in entry.aliases):
|
|
return True
|
|
query_terms = _meaningful_query_terms(query)
|
|
for candidate in (entry.title, *entry.aliases):
|
|
candidate_terms = _meaningful_query_terms(candidate)
|
|
if len(query_terms & candidate_terms) >= 2:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _meaningful_query_terms(value: str) -> set[str]:
|
|
return {term for term in tokenize(value) if term not in QUERY_MATCH_STOP_TOKENS and len(term.strip()) >= 2}
|
|
|
|
|
|
def normalize(value: str) -> str:
|
|
return re.sub(r"\s+", " ", value.lower()).strip()
|
|
|
|
|
|
def _query_entry(path: Path) -> QueryEntry:
|
|
meta, body = frontmatter(path.read_text(encoding="utf-8"))
|
|
title = str(meta.get("title") or _first_heading(body) or path.stem)
|
|
return QueryEntry(
|
|
path=path,
|
|
title=title,
|
|
aliases=tuple(str(item).strip() for item in meta.get("aliases", []) if str(item).strip()),
|
|
intent=str(meta["intent"]) if meta.get("intent") else None,
|
|
primary_sources=tuple(str(item).strip() for item in meta.get("primary_sources", []) if str(item).strip()),
|
|
supporting_sources=tuple(str(item).strip() for item in meta.get("supporting_sources", []) if str(item).strip()),
|
|
answer_requirements=tuple(str(item).strip() for item in meta.get("answer_requirements", []) if str(item).strip()),
|
|
)
|
|
|
|
|
|
def _first_heading(text: str) -> str | None:
|
|
for line in text.splitlines():
|
|
if match := re.match(r"^#{1,6}\s+(.+)$", line):
|
|
return match.group(1).strip()
|
|
return None
|