remove merge_common_prefixes until the best implementation found
Browse files
app.py
CHANGED
|
@@ -27,33 +27,6 @@ MODEL_LIST = [
|
|
| 27 |
"Epiculous/Violet_Twilight-v0.2",
|
| 28 |
]
|
| 29 |
|
| 30 |
-
def merge_common_prefixes(suggestions, min_len=2):
|
| 31 |
-
"""
|
| 32 |
-
合併具有共同前綴的建議:
|
| 33 |
-
- 找出所有長度 ≥ min_len 的共同前綴
|
| 34 |
-
- 將這些前綴作為新建議,移除原有被合併的項目
|
| 35 |
-
"""
|
| 36 |
-
prefixes = []
|
| 37 |
-
to_remove = set()
|
| 38 |
-
|
| 39 |
-
for i in range(len(suggestions)):
|
| 40 |
-
for j in range(i+1, len(suggestions)):
|
| 41 |
-
s1, s2 = suggestions[i], suggestions[j]
|
| 42 |
-
# 計算字元級共同前綴
|
| 43 |
-
common = ''.join(c1 for c1, c2 in zip(s1, s2) if c1 == c2)
|
| 44 |
-
if len(common) >= min_len:
|
| 45 |
-
prefixes.append(common)
|
| 46 |
-
to_remove.update([s1, s2])
|
| 47 |
-
|
| 48 |
-
# 去重前綴
|
| 49 |
-
unique_prefixes = []
|
| 50 |
-
for p in prefixes:
|
| 51 |
-
if p not in unique_prefixes:
|
| 52 |
-
unique_prefixes.append(p)
|
| 53 |
-
|
| 54 |
-
# 剩下沒有被合併的建議
|
| 55 |
-
remainder = [s for s in suggestions if s not in to_remove]
|
| 56 |
-
return unique_prefixes + remainder
|
| 57 |
|
| 58 |
@lru_cache(maxsize=8)
|
| 59 |
def get_pipeline(model_name):
|
|
@@ -111,7 +84,7 @@ def suggest_next(text, model_name, k, m, num_beam_groups, diversity_penalty):
|
|
| 111 |
|
| 112 |
# 合併共同前綴
|
| 113 |
cprint(f'unique_suggestions: {unique_suggestions}','blue')
|
| 114 |
-
merged_prefixes =
|
| 115 |
cprint(f"merged_prefixes: {merged_prefixes}",'red')
|
| 116 |
|
| 117 |
# 最終去重並移除空項 (基於 strip 後內容)
|
|
|
|
| 27 |
"Epiculous/Violet_Twilight-v0.2",
|
| 28 |
]
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@lru_cache(maxsize=8)
|
| 32 |
def get_pipeline(model_name):
|
|
|
|
| 84 |
|
| 85 |
# 合併共同前綴
|
| 86 |
cprint(f'unique_suggestions: {unique_suggestions}','blue')
|
| 87 |
+
merged_prefixes = unique_suggestions
|
| 88 |
cprint(f"merged_prefixes: {merged_prefixes}",'red')
|
| 89 |
|
| 90 |
# 最終去重並移除空項 (基於 strip 後內容)
|