Spaces:
Sleeping
Sleeping
File size: 6,737 Bytes
74e7808 | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | # -*- coding: utf-8 -*-
"""
===================================
Name-to-Code Resolution Engine
===================================
Resolve stock name to code: local mapping + pinyin + AkShare fallback + fuzzy matching.
"""
from __future__ import annotations
import difflib
import logging
import time
from typing import Dict, Optional, Set
from src.data.stock_mapping import STOCK_NAME_MAP
from src.services.stock_code_utils import is_code_like, normalize_code
logger = logging.getLogger(__name__)
# AkShare result cache: (timestamp, name_to_code_dict)
_akshare_cache: Optional[tuple[float, Dict[str, str]]] = None
_AKSHARE_CACHE_TTL = 1800 # 30 MIN
def _contains_cjk(text: str) -> bool:
"""Return True when text contains CJK characters."""
return any("\u3400" <= ch <= "\u9fff" for ch in text)
def _is_code_like(s: str) -> bool:
"""Backward-compatible wrapper of shared code-like check."""
return is_code_like(s)
def _normalize_code(raw: str) -> Optional[str]:
"""Backward-compatible wrapper of shared code normalization."""
return normalize_code(raw)
def _build_reverse_map_no_duplicates(
code_to_name: Dict[str, str],
) -> Dict[str, str]:
"""
Build name -> code map. If a name maps to multiple codes (ambiguous), exclude it.
"""
name_to_codes: Dict[str, Set[str]] = {}
for code, name in code_to_name.items():
if not name or not code:
continue
name = name.strip()
if name not in name_to_codes:
name_to_codes[name] = set()
name_to_codes[name].add(code)
# Only include names with exactly one code
return {name: next(iter(codes)) for name, codes in name_to_codes.items() if len(codes) == 1}
def _get_akshare_name_to_code() -> Optional[Dict[str, str]]:
"""Fetch A-share name->code from AkShare, with cache."""
global _akshare_cache
now = time.time()
if _akshare_cache is not None and (now - _akshare_cache[0]) < _AKSHARE_CACHE_TTL:
return _akshare_cache[1]
try:
import akshare as ak
df = ak.stock_info_a_code_name()
if df is None or df.empty:
return None
code_to_name = {}
for _, row in df.iterrows():
code = row.get("code")
name = row.get("name")
if code is None or name is None:
continue
code_str = str(code).strip()
# Strip .SH/.SZ suffix
if "." in code_str:
base, suffix = code_str.rsplit(".", 1)
if suffix.upper() in ("SH", "SZ", "SS") and base.isdigit():
code_str = base
code_to_name[code_str] = str(name).strip()
result = _build_reverse_map_no_duplicates(code_to_name)
_akshare_cache = (now, result)
logger.info(f"[NameResolver] AkShare cache loaded: {len(result)} name->code mappings")
return result
except Exception as e:
logger.warning(f"[NameResolver] AkShare fallback failed: {e}")
return None
def _is_single_char_typo(input_name: str, candidate_name: str) -> bool:
"""Return True when two names only differ by one character position."""
if not input_name or not candidate_name:
return False
if len(input_name) != len(candidate_name):
return False
# Keep typo fallback conservative: only for names with enough signal.
if len(input_name) < 3:
return False
diff = sum(1 for a, b in zip(input_name, candidate_name) if a != b)
return diff == 1
def resolve_name_to_code(name: str) -> Optional[str]:
"""
Resolve stock name to code.
Strategy (in order):
1. If input looks like a code (5-6 digits or 1-5 letters), return it normalized.
2. Local STOCK_NAME_MAP reverse (exclude ambiguous names).
3. Pinyin match against local names.
4. AkShare online fallback (A-shares).
5. Fuzzy match (difflib).
6. Return None.
Args:
name: Stock name or code string.
Returns:
Resolved stock code, or None if ambiguous/failed.
"""
if not name or not isinstance(name, str):
return None
s = name.strip()
if not s:
return None
# 1. Input looks like code
if _is_code_like(s):
return _normalize_code(s)
# 2. Local reverse map (no duplicates)
local_reverse = _build_reverse_map_no_duplicates(STOCK_NAME_MAP)
if s in local_reverse:
return local_reverse[s]
# 3. Pinyin match (exact)
try:
from pypinyin import lazy_pinyin
input_pinyin = "".join(lazy_pinyin(s)).lower()
for local_name, code in local_reverse.items():
local_pinyin = "".join(lazy_pinyin(local_name)).lower()
if input_pinyin == local_pinyin:
return code
except ImportError:
pass
except Exception as e:
logger.debug(f"[NameResolver] Pinyin match failed: {e}")
# Skip AkShare/fuzzy fallback for non-CJK free text such as random Latin noise.
# These paths are expensive and only meaningfully help Chinese stock names.
if not _contains_cjk(s):
logger.debug(f"[NameResolver] Skip CJK-only fallbacks for non-CJK input: {s}")
return None
# 4. AkShare fallback
akshare_map = _get_akshare_name_to_code()
if akshare_map and s in akshare_map:
logger.debug(f"[NameResolver] 命中 AkShare 映射: {s} -> {akshare_map[s]}")
return akshare_map[s]
# 5. Fuzzy match (local + akshare, local takes precedence)
all_name_to_code = dict(local_reverse)
if akshare_map:
all_name_to_code.update(akshare_map)
# Skip fuzzy matching for very short inputs (<=2 chars) to avoid false positives,
# e.g. '中国' matching arbitrary company names in a pool of 5000+ stocks.
# Use a higher cutoff (0.8) to reduce mis-hits on longer inputs as well.
if len(s) > 2:
names = list(all_name_to_code.keys())
matches = difflib.get_close_matches(s, names, n=1, cutoff=0.8)
if matches:
logger.debug(f"[NameResolver] 命中模糊匹配: input={s}, matched={matches[0]}")
return all_name_to_code[matches[0]]
# Conservative fallback for one-character typo in medium/long names.
# This keeps the strict default threshold while fixing obvious misspellings
# such as "贵州茅苔" -> "贵州茅台".
typo_matches = difflib.get_close_matches(s, names, n=1, cutoff=0.7)
if typo_matches and _is_single_char_typo(s, typo_matches[0]):
logger.debug(f"[NameResolver] 命中单字误写兜底: input={s}, matched={typo_matches[0]}")
return all_name_to_code[typo_matches[0]]
logger.debug(f"[NameResolver] 解析失败: {s}")
return None
|