32 lines
885 B
Python
32 lines
885 B
Python
"""Portable entity/relation normalization used by both knowledge products."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
|
|
def normalize_name(value: str) -> str:
|
|
text = unicodedata.normalize("NFKC", value or "").strip().casefold()
|
|
text = text.strip("`'\"“”‘’《》〈〉[](){}")
|
|
text = re.sub(r"\s+", " ", text)
|
|
return text
|
|
|
|
|
|
def normalize_predicate(value: str) -> str:
|
|
normalized = normalize_name(value).replace(" ", "_")
|
|
aliases = {
|
|
"依赖": "depends_on",
|
|
"需要": "depends_on",
|
|
"requires": "depends_on",
|
|
"调用": "uses",
|
|
"使用": "uses",
|
|
"uses": "uses",
|
|
"生成": "produces",
|
|
"输出": "produces",
|
|
"读取": "consumes",
|
|
"输入": "consumes",
|
|
"引用": "references",
|
|
}
|
|
return aliases.get(normalized, normalized or "related_to")
|