maque 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. maque/__init__.py +30 -0
  2. maque/__main__.py +926 -0
  3. maque/ai_platform/__init__.py +0 -0
  4. maque/ai_platform/crawl.py +45 -0
  5. maque/ai_platform/metrics.py +258 -0
  6. maque/ai_platform/nlp_preprocess.py +67 -0
  7. maque/ai_platform/webpage_screen_shot.py +195 -0
  8. maque/algorithms/__init__.py +78 -0
  9. maque/algorithms/bezier.py +15 -0
  10. maque/algorithms/bktree.py +117 -0
  11. maque/algorithms/core.py +104 -0
  12. maque/algorithms/hilbert.py +16 -0
  13. maque/algorithms/rate_function.py +92 -0
  14. maque/algorithms/transform.py +27 -0
  15. maque/algorithms/trie.py +272 -0
  16. maque/algorithms/utils.py +63 -0
  17. maque/algorithms/video.py +587 -0
  18. maque/api/__init__.py +1 -0
  19. maque/api/common.py +110 -0
  20. maque/api/fetch.py +26 -0
  21. maque/api/static/icon.png +0 -0
  22. maque/api/static/redoc.standalone.js +1782 -0
  23. maque/api/static/swagger-ui-bundle.js +3 -0
  24. maque/api/static/swagger-ui.css +3 -0
  25. maque/cli/__init__.py +1 -0
  26. maque/cli/clean_invisible_chars.py +324 -0
  27. maque/cli/core.py +34 -0
  28. maque/cli/groups/__init__.py +26 -0
  29. maque/cli/groups/config.py +205 -0
  30. maque/cli/groups/data.py +615 -0
  31. maque/cli/groups/doctor.py +259 -0
  32. maque/cli/groups/embedding.py +222 -0
  33. maque/cli/groups/git.py +29 -0
  34. maque/cli/groups/help.py +410 -0
  35. maque/cli/groups/llm.py +223 -0
  36. maque/cli/groups/mcp.py +241 -0
  37. maque/cli/groups/mllm.py +1795 -0
  38. maque/cli/groups/mllm_simple.py +60 -0
  39. maque/cli/groups/quant.py +210 -0
  40. maque/cli/groups/service.py +490 -0
  41. maque/cli/groups/system.py +570 -0
  42. maque/cli/mllm_run.py +1451 -0
  43. maque/cli/script.py +52 -0
  44. maque/cli/tree.py +49 -0
  45. maque/clustering/__init__.py +52 -0
  46. maque/clustering/analyzer.py +347 -0
  47. maque/clustering/clusterers.py +464 -0
  48. maque/clustering/sampler.py +134 -0
  49. maque/clustering/visualizer.py +205 -0
  50. maque/constant.py +13 -0
  51. maque/core.py +133 -0
  52. maque/cv/__init__.py +1 -0
  53. maque/cv/image.py +219 -0
  54. maque/cv/utils.py +68 -0
  55. maque/cv/video/__init__.py +3 -0
  56. maque/cv/video/keyframe_extractor.py +368 -0
  57. maque/embedding/__init__.py +43 -0
  58. maque/embedding/base.py +56 -0
  59. maque/embedding/multimodal.py +308 -0
  60. maque/embedding/server.py +523 -0
  61. maque/embedding/text.py +311 -0
  62. maque/git/__init__.py +24 -0
  63. maque/git/pure_git.py +912 -0
  64. maque/io/__init__.py +29 -0
  65. maque/io/core.py +38 -0
  66. maque/io/ops.py +194 -0
  67. maque/llm/__init__.py +111 -0
  68. maque/llm/backend.py +416 -0
  69. maque/llm/base.py +411 -0
  70. maque/llm/server.py +366 -0
  71. maque/mcp_server.py +1096 -0
  72. maque/mllm_data_processor_pipeline/__init__.py +17 -0
  73. maque/mllm_data_processor_pipeline/core.py +341 -0
  74. maque/mllm_data_processor_pipeline/example.py +291 -0
  75. maque/mllm_data_processor_pipeline/steps/__init__.py +56 -0
  76. maque/mllm_data_processor_pipeline/steps/data_alignment.py +267 -0
  77. maque/mllm_data_processor_pipeline/steps/data_loader.py +172 -0
  78. maque/mllm_data_processor_pipeline/steps/data_validation.py +304 -0
  79. maque/mllm_data_processor_pipeline/steps/format_conversion.py +411 -0
  80. maque/mllm_data_processor_pipeline/steps/mllm_annotation.py +331 -0
  81. maque/mllm_data_processor_pipeline/steps/mllm_refinement.py +446 -0
  82. maque/mllm_data_processor_pipeline/steps/result_validation.py +501 -0
  83. maque/mllm_data_processor_pipeline/web_app.py +317 -0
  84. maque/nlp/__init__.py +14 -0
  85. maque/nlp/ngram.py +9 -0
  86. maque/nlp/parser.py +63 -0
  87. maque/nlp/risk_matcher.py +543 -0
  88. maque/nlp/sentence_splitter.py +202 -0
  89. maque/nlp/simple_tradition_cvt.py +31 -0
  90. maque/performance/__init__.py +21 -0
  91. maque/performance/_measure_time.py +70 -0
  92. maque/performance/_profiler.py +367 -0
  93. maque/performance/_stat_memory.py +51 -0
  94. maque/pipelines/__init__.py +15 -0
  95. maque/pipelines/clustering.py +252 -0
  96. maque/quantization/__init__.py +42 -0
  97. maque/quantization/auto_round.py +120 -0
  98. maque/quantization/base.py +145 -0
  99. maque/quantization/bitsandbytes.py +127 -0
  100. maque/quantization/llm_compressor.py +102 -0
  101. maque/retriever/__init__.py +35 -0
  102. maque/retriever/chroma.py +654 -0
  103. maque/retriever/document.py +140 -0
  104. maque/retriever/milvus.py +1140 -0
  105. maque/table_ops/__init__.py +1 -0
  106. maque/table_ops/core.py +133 -0
  107. maque/table_viewer/__init__.py +4 -0
  108. maque/table_viewer/download_assets.py +57 -0
  109. maque/table_viewer/server.py +698 -0
  110. maque/table_viewer/static/element-plus-icons.js +5791 -0
  111. maque/table_viewer/static/element-plus.css +1 -0
  112. maque/table_viewer/static/element-plus.js +65236 -0
  113. maque/table_viewer/static/main.css +268 -0
  114. maque/table_viewer/static/main.js +669 -0
  115. maque/table_viewer/static/vue.global.js +18227 -0
  116. maque/table_viewer/templates/index.html +401 -0
  117. maque/utils/__init__.py +56 -0
  118. maque/utils/color.py +68 -0
  119. maque/utils/color_string.py +45 -0
  120. maque/utils/compress.py +66 -0
  121. maque/utils/constant.py +183 -0
  122. maque/utils/core.py +261 -0
  123. maque/utils/cursor.py +143 -0
  124. maque/utils/distance.py +58 -0
  125. maque/utils/docker.py +96 -0
  126. maque/utils/downloads.py +51 -0
  127. maque/utils/excel_helper.py +542 -0
  128. maque/utils/helper_metrics.py +121 -0
  129. maque/utils/helper_parser.py +168 -0
  130. maque/utils/net.py +64 -0
  131. maque/utils/nvidia_stat.py +140 -0
  132. maque/utils/ops.py +53 -0
  133. maque/utils/packages.py +31 -0
  134. maque/utils/path.py +57 -0
  135. maque/utils/tar.py +260 -0
  136. maque/utils/untar.py +129 -0
  137. maque/web/__init__.py +0 -0
  138. maque/web/image_downloader.py +1410 -0
  139. maque-0.2.1.dist-info/METADATA +450 -0
  140. maque-0.2.1.dist-info/RECORD +143 -0
  141. maque-0.2.1.dist-info/WHEEL +4 -0
  142. maque-0.2.1.dist-info/entry_points.txt +3 -0
  143. maque-0.2.1.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,183 @@
1
+ BLUE_E = "#1C758A"
2
+ BLUE_D = "#29ABCA"
3
+ BLUE_C = "#58C4DD"
4
+ BLUE_B = "#9CDCEB"
5
+ BLUE_A = "#C7E9F1"
6
+ TEAL_E = "#49A88F"
7
+ TEAL_D = "#55C1A7"
8
+ TEAL_C = "#5CD0B3"
9
+ TEAL_B = "#76DDC0"
10
+ TEAL_A = "#ACEAD7"
11
+ GREEN_E = "#699C52"
12
+ GREEN_D = "#77B05D"
13
+ GREEN_C = "#83C167"
14
+ GREEN_B = "#A6CF8C"
15
+ GREEN_A = "#C9E2AE"
16
+ YELLOW_E = "#E8C11C"
17
+ YELLOW_D = "#F4D345"
18
+ YELLOW_C = "#FFFF00"
19
+ YELLOW_B = "#FFEA94"
20
+ YELLOW_A = "#FFF1B6"
21
+ GOLD_E = "#C78D46"
22
+ GOLD_D = "#E1A158"
23
+ GOLD_C = "#F0AC5F"
24
+ GOLD_B = "#F9B775"
25
+ GOLD_A = "#F7C797"
26
+ RED_E = "#CF5044"
27
+ RED_D = "#E65A4C"
28
+ RED_C = "#FC6255"
29
+ RED_B = "#FF8080"
30
+ RED_A = "#F7A1A3"
31
+ MAROON_E = "#94424F"
32
+ MAROON_D = "#A24D61"
33
+ MAROON_C = "#C55F73"
34
+ MAROON_B = "#EC92AB"
35
+ MAROON_A = "#ECABC1"
36
+ PURPLE_E = "#644172"
37
+ PURPLE_D = "#715582"
38
+ PURPLE_C = "#9A72AC"
39
+ PURPLE_B = "#B189C6"
40
+ PURPLE_A = "#CAA3E8"
41
+ GREY_E = "#222222"
42
+ GREY_D = "#444444"
43
+ GREY_C = "#888888"
44
+ GREY_B = "#BBBBBB"
45
+ GREY_A = "#DDDDDD"
46
+ WHITE = "#FFFFFF"
47
+ BLACK = "#000000"
48
+ GREY_BROWN = "#736357"
49
+ DARK_BROWN = "#8B4513"
50
+ LIGHT_BROWN = "#CD853F"
51
+ PINK = "#D147BD"
52
+ LIGHT_PINK = "#DC75CD"
53
+ GREEN_SCREEN = "#00FF00"
54
+ ORANGE = "#FF862F"
55
+
56
+ # Abbreviated names for the "median" colors
57
+ BLUE = BLUE_C
58
+ TEAL = TEAL_C
59
+ GREEN = GREEN_C
60
+ YELLOW = YELLOW_C
61
+ GOLD = GOLD_C
62
+ RED = RED_C
63
+ MAROON = MAROON_C
64
+ PURPLE = PURPLE_C
65
+ GREY = GREY_C
66
+
67
+ # ---------------------------
68
+ red_lighter = "#FFEBEE"
69
+ red_light = "#EF9A9A"
70
+ red = "#EF5350"
71
+ red_dark = "#E53935"
72
+ red_darker = "#C62828"
73
+
74
+ pink_lighter = "#FCE4EC"
75
+ pink_light = "#F48FB1"
76
+ pink = "#EC407A"
77
+ pink_dark = "#D81B60"
78
+ pink_darker = "#AD1457"
79
+
80
+ purple_lighter = "#F3E5F5"
81
+ purple_light = "#CE93D8"
82
+ purple = "#AB47BC"
83
+ purple_dark = "#8E24AA"
84
+ purple_darker = "#6A1B9A"
85
+
86
+ deep_purple_lighter = "#EDE7F6"
87
+ deep_purple_light = "#B39DDB"
88
+ deep_purple = "#7E57C2"
89
+ deep_purple_dark = "#5E35B1"
90
+ deep_purple_darker = "#4527A0"
91
+
92
+ indigo_lighter = "#E8EAF6"
93
+ indigo_light = "#9FA8DA"
94
+ indigo = "#5C6BC0"
95
+ indigo_dark = "#3949AB"
96
+ indigo_darker = "#283593"
97
+
98
+ blue_lighter = "#E3F2FD"
99
+ blue_light = "#90CAF9"
100
+ blue = "#42A5F5"
101
+ blue_dark = "#1E88E5"
102
+ blue_darker = "#1565C0"
103
+
104
+ light_blue_lighter = "#E1F5FE"
105
+ light_blue_light = "#81D4FA"
106
+ light_blue = "#29B6F6"
107
+ light_blue_dark = "#039BE5"
108
+ light_blue_darker = "#0277BD"
109
+
110
+ cyan_lighter = "#E0F7FA"
111
+ cyan_light = "#80DEEA"
112
+ cyan = "#26C6DA"
113
+ cyan_dark = "#00ACC1"
114
+ cyan_darker = "#00838F"
115
+
116
+ teal_lighter = "#E0F2F1"
117
+ teal_light = "#80CBC4"
118
+ teal = "#26A69A"
119
+ teal_dark = "#00897B"
120
+ teal_darker = "#00695C"
121
+
122
+ green_lighter = "#E8F5E9"
123
+ green_light = "#A5D6A7"
124
+ green = "#66BB6A"
125
+ green_dark = "#43A047"
126
+ green_darker = "#2E7D32"
127
+
128
+ light_green_lighter = "#F1F8E9"
129
+ light_green_light = "#C5E1A5"
130
+ light_green = "#9CCC65"
131
+ light_green_dark = "#7CB342"
132
+ light_green_darker = "#558B2F"
133
+
134
+ lime_lighter = "#F9FBE7"
135
+ lime_light = "#E6EE9C"
136
+ lime = "#D4E157"
137
+ lime_dark = "#C0CA33"
138
+ lime_darker = "#9E9D24"
139
+
140
+ yellow_lighter = "#FFFDE7"
141
+ yellow_light = "#FFF59D"
142
+ yellow = "#FFEE58"
143
+ yellow_dark = "#FDD835"
144
+ yellow_darker = "#F9A825"
145
+
146
+ amber_lighter = "#FFF8E1"
147
+ amber_light = "#FFE082"
148
+ amber = "#FFCA28"
149
+ amber_dark = "#FFB300"
150
+ amber_darker = "#FF8F00"
151
+
152
+ orange_lighter = "#FFF3E0"
153
+ orange_light = "#FFCC80"
154
+ orange = "#FFA726"
155
+ orange_dark = "#FB8C00"
156
+ orange_darker = "#EF6C00"
157
+
158
+ salmon_lighter = "#FBE9E7"
159
+ salmon_light = "#FFAB91"
160
+ salmon = "#FF7043"
161
+ salmon_dark = "#F4511E"
162
+ salmon_darker = "#D84315"
163
+
164
+ brown_lighter = "#EFEBE9"
165
+ brown_light = "#BCAAA4"
166
+ brown = "#8D6E63"
167
+ brown_dark = "#6D4C41"
168
+ brown_darker = "#4E342E"
169
+
170
+ grey_lighter = "#FAFAFA"
171
+ grey_light = "#EEEEEE"
172
+ grey = "#BDBDBD"
173
+ grey_dark = "#757575"
174
+ grey_darker = "#424242"
175
+
176
+ blue_grey_lighter = "#ECEFF1"
177
+ blue_grey_light = "#B0BEC5"
178
+ blue_grey = "#78909C"
179
+ blue_grey_dark = "#546E7A"
180
+ blue_grey_darker = "#37474F"
181
+
182
+ black = "#000000"
183
+ white = "#FFFFFF"
maque/utils/core.py ADDED
@@ -0,0 +1,261 @@
1
+ from .color_string import rgb_string, color_const
2
+ from functools import wraps, update_wrapper
3
+ import asyncio
4
+ import logging
5
+ import inspect
6
+ import time
7
+ from typing import Union
8
+
9
+
10
+ def async_retry(
11
+ retry_times: int = 3,
12
+ retry_delay: float = 1.0,
13
+ exceptions: tuple = (Exception,),
14
+ logger = None,
15
+ ):
16
+ """
17
+ 异步重试装饰器
18
+
19
+ Args:
20
+ retry_times: 最大重试次数
21
+ retry_delay: 重试间隔时间(秒)
22
+ exceptions: 需要重试的异常类型
23
+ logger: 日志记录器
24
+ """
25
+ if logger is None:
26
+ from loguru import logger
27
+
28
+ def decorator(func):
29
+ @wraps(func)
30
+ async def wrapper(*args, **kwargs):
31
+ for attempt in range(retry_times):
32
+ try:
33
+ return await func(*args, **kwargs)
34
+ except exceptions as e:
35
+ if attempt == retry_times - 1:
36
+ raise
37
+ logger.warning(f"Attempt {attempt + 1} failed: {str(e)}")
38
+ await asyncio.sleep(retry_delay)
39
+ return await func(*args, **kwargs)
40
+
41
+ return wrapper
42
+
43
+ return decorator
44
+
45
+
46
+ def benchmark(times=10, logger=None, level=logging.INFO):
47
+ # if func is None:
48
+ # return partial(time_it, times=times)
49
+ def decorate(func):
50
+ @wraps(func)
51
+ def wrapper(*args, **kwargs):
52
+ start = time.time()
53
+ value = None
54
+ for i in range(times):
55
+ value = func(*args, **kwargs)
56
+ end = time.time()
57
+ average_cost_time = (end - start) / times
58
+ time_str = f"{average_cost_time:.3f}"
59
+
60
+ if logger:
61
+ logger.log(
62
+ level,
63
+ f"Run {rgb_string(str(times), color_const.GREEN)} times, "
64
+ f"the average time is {rgb_string(time_str, color_const.GREEN)} seconds."
65
+ )
66
+ else:
67
+ print(
68
+ f"Run {rgb_string(str(times), color_const.GREEN)} times, "
69
+ f"the average time is {rgb_string(time_str, color_const.GREEN)} seconds."
70
+ )
71
+ return value
72
+
73
+ return wrapper
74
+
75
+ return decorate
76
+
77
+
78
+ def measure_time(logger=None, level=logging.INFO):
79
+ def decorate(func):
80
+ """Log the runtime of the decorated function."""
81
+
82
+ @wraps(func)
83
+ def wrapper(*args, **kwargs):
84
+ start = time.time()
85
+ value = func(*args, **kwargs)
86
+ end = time.time()
87
+ cost_time = end - start
88
+ time_str = f"{cost_time:.3f}"
89
+ if logger:
90
+ logger.log(level,
91
+ f"Finished {rgb_string(func.__name__, color_const.RED)} in {rgb_string(time_str, color_const.GREEN)} secs."
92
+ )
93
+ else:
94
+ print(f"Finished {rgb_string(func.__name__, color_const.RED)} in {rgb_string(time_str, color_const.GREEN)} secs.")
95
+ return value
96
+
97
+ return wrapper
98
+
99
+ return decorate
100
+
101
+
102
+ def repeat(n=2):
103
+ """repeat decorated function `n` times."""
104
+
105
+ def decorator(func):
106
+ @wraps(func)
107
+ def wrapper(*args, **kwargs):
108
+ result = None
109
+ for i in range(n):
110
+ result = func(*args, **kwargs)
111
+ return result
112
+
113
+ return wrapper
114
+
115
+ return decorator
116
+
117
+
118
+ def optional_debug(func):
119
+ if "debug" in inspect.signature(func).parameters:
120
+ raise TypeError("debug argument already defined")
121
+
122
+ debug_default = True
123
+
124
+ @wraps(func)
125
+ def wrapper(*args, debug=debug_default, **kwargs):
126
+ if debug:
127
+ args_repr = [repr(a) for a in args]
128
+ kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
129
+ signature = ", ".join(args_repr + kwargs_repr)
130
+ print(f"Calling '{func.__name__}({signature})'")
131
+ value = func(*args, **kwargs)
132
+ if debug:
133
+ print(f"{func.__name__!r} returned {value!r}")
134
+ return value
135
+
136
+ sig = inspect.signature(func)
137
+ parms = list(sig.parameters.values())
138
+ parms.append(
139
+ inspect.Parameter(
140
+ "debug", inspect.Parameter.KEYWORD_ONLY, default=debug_default
141
+ )
142
+ )
143
+ wrapper.__signature__ = sig.replace(parameters=parms)
144
+ return wrapper
145
+
146
+
147
+ def count_calls(func):
148
+ """Count the number of calls made to the decorated function."""
149
+
150
+ @wraps(func)
151
+ def wrapper(*args, **kwargs):
152
+ wrapper.num_calls += 1
153
+ print(f"Call {wrapper.num_calls} of {func.__name__!r}")
154
+ return func(*args, **kwargs)
155
+
156
+ wrapper.num_calls = 0
157
+ return wrapper
158
+
159
+
160
+ class CountCalls:
161
+ """Count the number of calls made to the decorated function."""
162
+
163
+ def __init__(self, func):
164
+ update_wrapper(self, func)
165
+ self.func = func
166
+ self.num_calls = 0
167
+
168
+ def __call__(self, *args, **kwargs):
169
+ self.num_calls += 1
170
+ print(f"Call {self.num_calls} of {self.func.__name__!r}")
171
+ return self.func(*args, **kwargs)
172
+
173
+
174
+ class CallReminder:
175
+ def __init__(self, func):
176
+ self._func = func
177
+ self._num_calls = 0
178
+
179
+ def __call__(self, *args, **kwargs):
180
+ self._num_calls += 1
181
+ return self._func(*args, **kwargs)
182
+
183
+ @property
184
+ def count_calls(self):
185
+ return self._num_calls
186
+
187
+
188
+ def logged(level, name=None, message=None):
189
+ """
190
+ Add logging to a function. level is the logging
191
+ level, name is the logger name, and message is the
192
+ log message. If name and message aren't specified,
193
+ they default to the function's module and name.
194
+ """
195
+
196
+ def decorate(func):
197
+ logname = name if name else func.__module__
198
+ log = logging.getLogger(logname)
199
+ logmsg = message if message else func.__name__
200
+
201
+ @wraps(func)
202
+ def wrapper(*args, **kwargs):
203
+ log.log(level, logmsg)
204
+ return func(*args, **kwargs)
205
+
206
+ return wrapper
207
+
208
+ return decorate
209
+
210
+
211
+ def singleton(cls):
212
+ """
213
+ Usage
214
+ -----
215
+ @singleton
216
+ class Cls:
217
+ pass
218
+ """
219
+ _instance = {}
220
+
221
+ def inner():
222
+ if cls not in _instance:
223
+ _instance[cls] = cls()
224
+ return _instance[cls]
225
+
226
+ return inner
227
+
228
+
229
+ class Singleton:
230
+ """
231
+ Usage
232
+ -----
233
+ @Singleton
234
+ class Cls:
235
+ pass
236
+ """
237
+
238
+ def __init__(self, cls):
239
+ self._cls = cls
240
+ self._instance = {}
241
+
242
+ def __call__(self):
243
+ if self._cls not in self._instance:
244
+ self._instance[self._cls] = self._cls()
245
+ return self._instance[self._cls]
246
+
247
+
248
+ class MetaSingleton(type):
249
+ """
250
+ Usage
251
+ -----
252
+ class Cls(metaclass=MetaSingleton):
253
+ pass
254
+ """
255
+
256
+ _instances = {}
257
+
258
+ def __call__(cls, *args, **kwargs):
259
+ if cls not in cls._instances:
260
+ cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
261
+ return cls._instances[cls]
maque/utils/cursor.py ADDED
@@ -0,0 +1,143 @@
1
+ import abc
2
+ import sys
3
+
4
+ CSI = '\033['
5
+ OSC = '\033]'
6
+ OFF = CSI + '0m'
7
+
8
+
9
+ class BaseCursor(metaclass=abc.ABCMeta):
10
+ @abc.abstractmethod
11
+ def FORWARD(self, *args, **kwargs):
12
+ '''Forward move'''
13
+ pass
14
+
15
+ @abc.abstractmethod
16
+ def BACK(self, *args, **kwargs):
17
+ '''Backward move'''
18
+ pass
19
+
20
+ @abc.abstractmethod
21
+ def PreLINE(self, *args, **kwargs):
22
+ """Moves cursor to beginning of the line n (default 1) lines up.
23
+ No any character will be cleared.
24
+ """
25
+ pass
26
+
27
+ @abc.abstractmethod
28
+ def EraseLine(self, *args, **kwargs):
29
+ """Erases part of the line.
30
+ If n is 0, clear from cursor to the end of the line.
31
+ If n is 1, clear from cursor to beginning of the line.
32
+ If n is 2, clear entire line. Cursor position does not change.
33
+ """
34
+ pass
35
+
36
+ # @abc.abstractmethod
37
+ # def EraseUpLine(self, *args, **kwargs):
38
+ # """Erase n lines and cursor move up n lines"""
39
+ # pass
40
+
41
+ @abc.abstractmethod
42
+ def savePOS_x(self, *args, **kwargs):
43
+ """Save current position"""
44
+ pass
45
+
46
+ @abc.abstractmethod
47
+ def restorePOS_x(self, *args, **kwargs):
48
+ """Restore saved position"""
49
+ pass
50
+
51
+ @abc.abstractmethod
52
+ def getPOS(self):
53
+ pass
54
+
55
+
56
+ def send(code):
57
+ sys.stdout.write(code)
58
+ sys.stdout.flush()
59
+
60
+
61
+ def csi(n, code):
62
+ return CSI + str(n) + code
63
+
64
+
65
+ class Cursor(BaseCursor):
66
+ '''See: http://en.wikipedia.org/wiki/ANSI_escape_code'''
67
+
68
+ def UP(self, n=1):
69
+ """Moves the cursor n (default 1) cells in the given direction."""
70
+ return csi(n, 'A')
71
+
72
+ def DOWN(self, n=1):
73
+ """Moves the cursor n (default 1) cells in the given direction."""
74
+ return csi(n, 'B')
75
+
76
+ def FORWARD(self, n=1):
77
+ """Moves the cursor n (default 1) cells in the given direction."""
78
+ return csi(n, 'C')
79
+
80
+ def BACK(self, n=1):
81
+ """Moves the cursor n (default 1) cells in the given direction."""
82
+ return csi(n, 'D')
83
+
84
+ def NextLINE(self, n=1):
85
+ return csi(n, 'E')
86
+
87
+ def PreLINE(self, n=1):
88
+ """Moves cursor to beginning of the line n (default 1) lines up.
89
+ No any character are cleared.
90
+ """
91
+ return csi(n, 'F')
92
+
93
+ def HorizCol(self, n=1):
94
+ """Moves the cursor to column n"""
95
+ return csi(n, 'G')
96
+
97
+ def POS(self, x=1, y=1):
98
+ """Moves the cursor to row n, column m.
99
+ The values are 1-based, and default to 1 (top left corner) if omitted."""
100
+ return CSI + str(y) + ';' + str(x) + 'H'
101
+
102
+ def EraseLine(self, n=0):
103
+ """Erases part of the line.
104
+ If n is 0, clear from cursor to the end of the line.
105
+ If n is 1, clear from cursor to beginning of the line.
106
+ If n is 2, clear entire line. Cursor position does not change.
107
+ """
108
+ if n == 0:
109
+ return CSI + 'K'
110
+ else:
111
+ return csi(n, 'K')
112
+
113
+ def EraseDisplay(self, n=0):
114
+ """Clears part of the screen.
115
+ If n is 0, clear from cursor to end of screen.
116
+ If n is 1, clear from cursor to beginning of the screen.
117
+ If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).
118
+ If n is 3, clear entire screen and delete all lines saved in the scrollback buffer.
119
+ """
120
+ return csi(n, 'J')
121
+
122
+ def ScrollUp(self, n=1):
123
+ """Scroll whole page up by n (default 1) lines.
124
+ New lines are added at the bottom.
125
+ """
126
+ return csi(n, "S")
127
+
128
+ def ScrollDown(self, n=0):
129
+ """Scroll whole page down by n (default 1) lines.
130
+ New lines are added at the top. """
131
+ return csi(n, "T")
132
+
133
+ def savePOS_x(self):
134
+ return "\033[s" # "\0337"
135
+
136
+ def restorePOS_x(self):
137
+ return "\033[u" # "\0338"
138
+
139
+ def getPOS(self):
140
+ return CSI + "6n"
141
+
142
+ def hiddenCursor(self):
143
+ return "\033[?25l"
@@ -0,0 +1,58 @@
1
+ import numpy as np
2
+ from difflib import SequenceMatcher
3
+
4
+ """
5
+ Some common distance function
6
+ """
7
+
8
+ # 尝试导入 Levenshtein 库(更快),否则使用标准库 difflib
9
+ try:
10
+ import Levenshtein as _Levenshtein
11
+ _USE_LEVENSHTEIN = True
12
+ except ImportError:
13
+ _USE_LEVENSHTEIN = False
14
+
15
+
16
+ def euclidean_dist(vec1, vec2):
17
+ assert vec1.shape == vec2.shape
18
+ return np.sqrt(np.sum((vec1 - vec2) ** 2))
19
+
20
+
21
+ def manhattan_dist(vec1, vec2):
22
+ return np.sum(np.abs(vec1 - vec2))
23
+
24
+
25
+ def chebyshev_dist(vec1, vec2):
26
+ return np.max(np.abs(vec1 - vec2))
27
+
28
+
29
+ def minkowski_dist(vec1, vec2, p=2):
30
+ """
31
+ :param: `p` The meaning of norm.
32
+ p=1: dist = manhattan_dist
33
+ p=2: dist = euclidean_dist
34
+ p=inf: dist = chebyshev_dist
35
+ """
36
+ s = np.sum(np.power(vec2 - vec1, p))
37
+ return np.power(s, 1 / p)
38
+
39
+
40
+ def cosine_dist(vec1, vec2, p=2):
41
+ # np.linalg.norm(vec, ord=1) 计算p=1范数,默认p=2
42
+ return (vec1.T @ vec2) / (np.linalg.norm(vec1, ord=p) * np.linalg.norm(vec2, ord=p))
43
+
44
+
45
+ def distance(a, b):
46
+ """计算两个字符串的相似度 (0-1),1表示完全相同"""
47
+ if _USE_LEVENSHTEIN:
48
+ return _Levenshtein.ratio(a, b)
49
+ else:
50
+ return SequenceMatcher(None, a, b).ratio()
51
+
52
+
53
+ def hamming(x, y):
54
+ return np.sum(x != y) / len(x)
55
+
56
+
57
+ def jaccard():
58
+ pass