forked from Yinglianchun/Ombre-Brain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_understanding.py
More file actions
45 lines (32 loc) · 1.33 KB
/
Copy pathquery_understanding.py
File metadata and controls
45 lines (32 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
from typing import Any
DEFAULT_QUERY_INTENTS_PATH = Path(__file__).resolve().parent / "resources" / "query_intents.json"
@lru_cache(maxsize=1)
def query_intent_lexicon() -> dict[str, Any]:
with DEFAULT_QUERY_INTENTS_PATH.open("r", encoding="utf-8") as handle:
payload = json.load(handle)
return payload if isinstance(payload, dict) else {}
def query_intent_value(path: str, default: Any = None) -> Any:
current: Any = query_intent_lexicon()
for part in str(path or "").split("."):
if not part:
continue
if not isinstance(current, dict) or part not in current:
return default
current = current[part]
return current
def query_intent_terms(path: str) -> tuple[str, ...]:
value = query_intent_value(path, [])
if not isinstance(value, list):
return ()
return tuple(str(item).strip() for item in value if str(item or "").strip())
def query_intent_term_set(path: str) -> frozenset[str]:
return frozenset(query_intent_terms(path))
def query_intent_rules(path: str) -> tuple[dict[str, Any], ...]:
value = query_intent_value(path, [])
if not isinstance(value, list):
return ()
return tuple(item for item in value if isinstance(item, dict))