# string-matching **Repository Path**: zhengxuhong/string-matching ## Basic Information - **Project Name**: string-matching - **Description**: 字符串匹配,精确,模糊,嵌入 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-11-04 - **Last Updated**: 2026-11-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # string-matching 轻量、可组合的字符串匹配库。当前提供精确匹配和基于 RapidFuzz 的字符模糊匹配;语义匹配只定义外部 `Embedder` 接口,不负责维护嵌入模型。 ## 安装 ```bash uv sync ``` 作为其他项目的 Git 依赖: ```toml "string-matching @ git+https://gitee.com/zhengxuhong/string-matching.git" ``` ## 精确匹配 默认去首尾空白并区分大小写: ```python from string_matching import ExactMatcher candidates = ["alpha", "beta", "Alpha"] matcher = ExactMatcher() matcher.match(" alpha ", candidates) # 命中 alpha matcher.match("ALPHA", candidates) # 不命中 ``` 可选功能: ```python matcher = ExactMatcher( strip_whitespace=True, # 默认开启 collapse_whitespace=True, # 默认关闭:连续空白折叠为一个空格 case_sensitive=False, # 默认开启区分大小写;这里改为 casefold 比较 ) ``` `match()` 返回所有命中;`best()` 返回第一个命中或 `None`。结果保留原始候选和候选下标。 ## 模糊匹配 ```python from string_matching import FuzzyMatcher candidates = ["familial Mediterranean fever", "hypertension"] matcher = FuzzyMatcher( scorer="ratio", min_score=0.6, case_sensitive=False, collapse_whitespace=True, ) results = matcher.match("Familial Mediteranean Fever", candidates, top_k=5) best = matcher.best("Familial Mediteranean Fever", candidates) ``` `score` 范围为 0–1,越大越相似。`top_k=None` 返回所有达到 `min_score` 的候选;相同分数保持候选原始顺序。 内置算法: | scorer | 用途 | |---|---| | `ratio` | 默认,归一化 Indel 相似度,较保守 | | `levenshtein` | 归一化编辑距离,适合拼写错误 | | `token_sort_ratio` | 忽略词序差异 | | `token_set_ratio` | 比较词集合;词集合为子集时可能得到高分 | | `partial_ratio` | 短字符串与长字符串局部对齐,可能因子串命中得到高分 | 也可传入自定义 `scorer(left, right) -> float`,返回值必须位于 0–1。 ## 共享字符串清洗 清洗功能直接引用 [string-cleaning](https://gitee.com/zhengxuhong/string-cleaning),不重复实现。精确匹配和模糊匹配共用: - `strip_whitespace=True`:默认去首尾空白; - `collapse_whitespace=False`:可选折叠空格、制表符、换行等连续空白; - `case_sensitive=True`:默认区分大小写,关闭时使用 Unicode `casefold()`。 ## 外部嵌入接口 语义模块目前只有最小协议,不内置本地模型、API 客户端、HTTP 服务或向量数据库: ```python from string_matching import Embedder class MyEmbedder: def encode(self, texts: list[str]) -> list[list[float]]: ... # 由外部模型、服务或未来嵌入框架实现 ``` 接口要求返回向量顺序与输入一致。后续语义检索和距离指标将基于这个协议协调接入。 ## 开发校验 ```bash uv run ruff check src uv run mypy uv build ```