Repository navigation
Expand file tree
/
Copy pathdictionary_manager.py
More file actions
202 lines (165 loc) · 9.08 KB
/
Copy pathdictionary_manager.py
File metadata and controls
202 lines (165 loc) · 9.08 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
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
191
192
193
194
195
196
197
198
199
200
201
202
from __future__ import annotations
import logging
from pathlib import Path
import sqlite3
import os
from collections import defaultdict
from utils import config_manager
from core.models import resolve_origin_name_conflict
class DictionaryManager:
def __init__(self):
self.user_dict: dict | None = None
self.community_dict_by_key: dict[str, str] | None = None
self.community_dict_by_origin: dict[str, list[dict]] | None = None
self._cache: dict[str, tuple] = {}
self._community_origin_cache: dict[str, str | None] = {}
self._lower_key_index: dict[str, str] = {}
self._lower_origin_index: dict[str, str] = {}
self._lower_community_key_index: dict[str, str] = {}
self._lower_community_origin_index: dict[str, str] = {}
self._search_index_built = False
def _build_search_index(self):
self._lower_key_index = {k.lower(): k for k in self.user_dict.get('by_key', {})} if self.user_dict else {}
self._lower_origin_index = {k.lower(): k for k in self.user_dict.get('by_origin_name', {})} if self.user_dict else {}
self._lower_community_key_index = {k.lower(): k for k in (self.community_dict_by_key or {})}
self._lower_community_origin_index = {}
if self.community_dict_by_origin:
for origin in self.community_dict_by_origin:
self._lower_community_origin_index[origin.lower()] = origin
self._search_index_built = True
def _ensure_search_index(self):
if not self._search_index_built:
self._build_search_index()
def load_user_dictionary(self) -> dict:
try:
self.user_dict = config_manager.load_user_dict()
logging.debug("用户词典加载成功")
return self.user_dict
except Exception as e:
logging.error(f"加载用户词典失败: {e}")
return {'by_key': {}, 'by_origin_name': {}}
def load_community_dictionary(self, community_dict_dir: str, progress_callback=None) -> tuple[dict[str, str], dict[str, list[dict]]]:
community_dict_by_key: dict[str, str] = {}
community_dict_by_origin: dict[str, list[dict]] = defaultdict(list)
if not community_dict_dir:
self.community_dict_by_key = community_dict_by_key
self.community_dict_by_origin = community_dict_by_origin
self._search_index_built = False
return community_dict_by_key, community_dict_by_origin
try:
dict_file_path = Path(community_dict_dir) / "Dict-Sqlite.db"
if not dict_file_path.is_file():
logging.info(f"社区词典文件不存在: {dict_file_path}")
self.community_dict_by_key = community_dict_by_key
self.community_dict_by_origin = community_dict_by_origin
self._search_index_built = False
return community_dict_by_key, community_dict_by_origin
with sqlite3.connect(f"file:{dict_file_path}?mode=ro", uri=True) as con:
cur = con.cursor()
cur.execute("SELECT COUNT(*) FROM dict")
total_rows = cur.fetchone()[0]
cur.execute("SELECT key, origin_name, trans_name, version FROM dict")
batch_size = 1000
processed_rows = 0
while True:
rows = cur.fetchmany(batch_size)
if not rows:
break
for key, origin_name, trans_name, version in rows:
if key:
community_dict_by_key[key] = trans_name
if origin_name and trans_name:
community_dict_by_origin[origin_name].append({
"trans": trans_name,
"version": version or "0.0.0"
})
processed_rows += len(rows)
if progress_callback and total_rows > 0:
progress = min(int((processed_rows / total_rows) * 100), 100)
progress_callback(f"加载社区词典... {progress}%", progress)
logging.debug(f"社区词典加载成功: {len(community_dict_by_key)}条按键, {len(community_dict_by_origin)}条按原文")
except Exception as e:
logging.error(f"读取社区词典数据库时发生错误: {e}")
self.community_dict_by_key = community_dict_by_key
self.community_dict_by_origin = community_dict_by_origin
self._search_index_built = False
return community_dict_by_key, community_dict_by_origin
def get_all_dictionaries(self, community_dict_dir: str, progress_callback=None) -> tuple[dict, dict[str, str], dict[str, list[dict]]]:
cache_key = f"all_dicts_{community_dict_dir or 'none'}"
# 基于文件 mtime 检查缓存是否仍然有效
if cache_key in self._cache:
dict_file_path = Path(community_dict_dir) / "Dict-Sqlite.db" if community_dict_dir else None
cached_mtime = self._cache.get(f"{cache_key}_mtime", 0)
current_mtime = 0
if dict_file_path and dict_file_path.is_file():
try:
current_mtime = os.path.getmtime(dict_file_path)
except OSError:
pass
if current_mtime == cached_mtime:
logging.debug("从缓存中获取词典数据")
if progress_callback:
progress_callback("加载词典缓存...", 100)
return self._cache[cache_key]
else:
logging.debug("词典文件已更新,重新加载")
self.clear_cache()
user_dict = self.load_user_dictionary()
community_dict_by_key, community_dict_by_origin = self.load_community_dictionary(community_dict_dir, progress_callback)
result = (user_dict, community_dict_by_key, community_dict_by_origin)
self._cache[cache_key] = result
# 存储词典文件 mtime 用于缓存失效检查
dict_file_path = Path(community_dict_dir) / "Dict-Sqlite.db" if community_dict_dir else None
if dict_file_path and dict_file_path.is_file():
try:
self._cache[f"{cache_key}_mtime"] = os.path.getmtime(dict_file_path)
except OSError:
self._cache[f"{cache_key}_mtime"] = 0
else:
self._cache[f"{cache_key}_mtime"] = 0
user_count = len(user_dict.get('by_key', {})) + len(user_dict.get('by_origin_name', {}))
community_count = len(community_dict_by_key) + len(community_dict_by_origin)
logging.info(f"词典加载完成: 用户词典{user_count}条, 社区词典{community_count}条")
return result
def get_community_origin_translation(self, origin_name: str) -> str | None:
if origin_name in self._community_origin_cache:
return self._community_origin_cache[origin_name]
if not self.community_dict_by_origin or origin_name not in self.community_dict_by_origin:
return None
candidates = self.community_dict_by_origin[origin_name]
if not candidates:
return None
translation = resolve_origin_name_conflict(candidates)
self._community_origin_cache[origin_name] = translation
return translation
def clear_cache(self):
self._cache.clear()
self._community_origin_cache.clear()
self._lower_key_index.clear()
self._lower_origin_index.clear()
self._lower_community_key_index.clear()
self._lower_community_origin_index.clear()
self._search_index_built = False
logging.debug("词典缓存已清除")
def search_dictionary(self, query: str, search_type: str = 'both') -> list[dict]:
results: list[dict] = []
if not self.user_dict:
self.load_user_dictionary()
self._ensure_search_index()
query_lower = query.lower()
if search_type in ('key', 'both'):
for lower_key, orig_key in self._lower_key_index.items():
if query_lower in lower_key:
results.append({'type': 'user_key', 'key': orig_key, 'value': self.user_dict['by_key'][orig_key]})
for lower_key, orig_key in self._lower_community_key_index.items():
if query_lower in lower_key:
results.append({'type': 'community_key', 'key': orig_key, 'value': self.community_dict_by_key[orig_key]})
if search_type in ('origin', 'both'):
for lower_origin, orig_origin in self._lower_origin_index.items():
if query_lower in lower_origin:
results.append({'type': 'user_origin', 'key': orig_origin, 'value': self.user_dict['by_origin_name'][orig_origin]})
for lower_origin, orig_origin in self._lower_community_origin_index.items():
if query_lower in lower_origin:
for entry in self.community_dict_by_origin[orig_origin]:
results.append({'type': 'community_origin', 'key': orig_origin, 'value': entry['trans']})
return results