Upload 2 files
Browse files- danbooru_e621_merged.txt +0 -0
- search_and_copy.py +88 -65
danbooru_e621_merged.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
search_and_copy.py
CHANGED
|
@@ -1,88 +1,111 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import shutil
|
| 3 |
import tkinter as tk
|
| 4 |
from tkinter import filedialog, messagebox
|
|
|
|
| 5 |
|
| 6 |
-
def process_files(
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# 如果目錄不存在,創建目錄
|
| 15 |
-
if not os.path.exists(output_directory):
|
| 16 |
-
os.makedirs(output_directory)
|
| 17 |
-
|
| 18 |
-
# 準備輸出的檔案
|
| 19 |
temp_output_filename = "temp_" + output_filename
|
|
|
|
|
|
|
| 20 |
with open(temp_output_filename, 'w', encoding='utf-8') as outfile:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# 將臨時檔案移動到目標目錄
|
| 37 |
final_output_path = os.path.join(output_directory, output_filename)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def on_process():
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
messagebox.showerror("錯誤", "輸入的單詞不能超過十個")
|
| 49 |
return
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def browse_directory():
|
| 60 |
-
|
| 61 |
-
if
|
| 62 |
output_dir_entry.delete(0, tk.END)
|
| 63 |
-
output_dir_entry.insert(0,
|
| 64 |
|
| 65 |
-
#
|
| 66 |
root = tk.Tk()
|
| 67 |
root.title("詞彙檢測工具")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
tk.
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
tk.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
process_button = tk.Button(root, text="開始處理", command=on_process)
|
| 85 |
-
process_button.pack(pady=20)
|
| 86 |
|
| 87 |
-
# 運行主循環
|
| 88 |
root.mainloop()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import shutil
|
| 4 |
import tkinter as tk
|
| 5 |
from tkinter import filedialog, messagebox
|
| 6 |
+
import pyperclip
|
| 7 |
|
| 8 |
+
def process_files(files, positive_words, negative_words, output_directory):
|
| 9 |
+
# 生成輸出檔名,移除非法字元
|
| 10 |
+
safe_name = "_".join([re.sub(r'[\\/*?:"<>|]', '', w.strip()) for w in positive_words if w.strip()])
|
| 11 |
+
output_filename = f"{safe_name}.txt"
|
| 12 |
+
|
| 13 |
+
# 建立輸出目錄
|
| 14 |
+
os.makedirs(output_directory, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
temp_output_filename = "temp_" + output_filename
|
| 16 |
+
|
| 17 |
+
# 累積符合行
|
| 18 |
with open(temp_output_filename, 'w', encoding='utf-8') as outfile:
|
| 19 |
+
for filename in files:
|
| 20 |
+
try:
|
| 21 |
+
with open(filename, 'r', encoding='utf-8') as infile:
|
| 22 |
+
for line in infile:
|
| 23 |
+
line_stripped = line.strip()
|
| 24 |
+
if not line_stripped:
|
| 25 |
+
continue
|
| 26 |
+
# 符合所有正面詞且不含反面詞
|
| 27 |
+
if all(word in line_stripped for word in positive_words) and not any(word in line_stripped for word in negative_words):
|
| 28 |
+
outfile.write(line)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"無法讀取檔案 {filename}: {e}")
|
| 31 |
+
|
| 32 |
+
# 移動到目標資料夾
|
|
|
|
|
|
|
| 33 |
final_output_path = os.path.join(output_directory, output_filename)
|
| 34 |
+
try:
|
| 35 |
+
if os.path.exists(final_output_path):
|
| 36 |
+
os.remove(final_output_path)
|
| 37 |
+
shutil.move(temp_output_filename, final_output_path)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
messagebox.showerror("錯誤", f"移動檔案時發生錯誤: {e}")
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
# 嘗試複製結果路徑到剪貼簿
|
| 43 |
+
try:
|
| 44 |
+
pyperclip.copy(final_output_path)
|
| 45 |
+
except Exception:
|
| 46 |
+
print("無法複製路徑到剪貼簿。")
|
| 47 |
+
|
| 48 |
+
messagebox.showinfo("完成", f"結果已輸出至:\n{final_output_path}\n路徑已複製至剪貼簿。")
|
| 49 |
|
| 50 |
def on_process():
|
| 51 |
+
positive_input = positive_entry.get().strip()
|
| 52 |
+
positive_words = [w.strip() for w in positive_input.split(",") if w.strip()]
|
| 53 |
+
|
| 54 |
+
if not positive_words:
|
| 55 |
+
messagebox.showerror("錯誤", "必須輸入至少一個正面單詞。")
|
|
|
|
| 56 |
return
|
| 57 |
+
if len(positive_words) > 10:
|
| 58 |
+
messagebox.showerror("錯誤", "正面單詞不能超過十個。")
|
| 59 |
+
return
|
| 60 |
+
|
| 61 |
+
negative_input = negative_entry.get().strip()
|
| 62 |
+
negative_words = [w.strip() for w in negative_input.split(",") if w.strip()]
|
| 63 |
+
|
| 64 |
+
files_input = files_entry.get().strip()
|
| 65 |
+
files = [f.strip() for f in files_input.split(",") if f.strip()]
|
| 66 |
+
if not files:
|
| 67 |
+
files = [f for f in os.listdir('.') if f.endswith('.txt')]
|
| 68 |
+
|
| 69 |
+
output_dir = output_dir_entry.get().strip()
|
| 70 |
+
if not output_dir:
|
| 71 |
+
output_dir = r"I:\stable-diffusion-webui-reForge\extensions\sd-dynamic-prompts\wildcards"
|
| 72 |
+
|
| 73 |
+
process_files(files, positive_words, negative_words, output_dir)
|
| 74 |
+
|
| 75 |
+
def browse_files():
|
| 76 |
+
paths = filedialog.askopenfilenames(filetypes=[("Text files", "*.txt")])
|
| 77 |
+
if paths:
|
| 78 |
+
files_entry.delete(0, tk.END)
|
| 79 |
+
files_entry.insert(0, ",".join(paths))
|
| 80 |
|
| 81 |
def browse_directory():
|
| 82 |
+
path = filedialog.askdirectory()
|
| 83 |
+
if path:
|
| 84 |
output_dir_entry.delete(0, tk.END)
|
| 85 |
+
output_dir_entry.insert(0, path)
|
| 86 |
|
| 87 |
+
# GUI 介面
|
| 88 |
root = tk.Tk()
|
| 89 |
root.title("詞彙檢測工具")
|
| 90 |
|
| 91 |
+
tk.Label(root, text="正面單詞 (最多10個, 以逗號分隔):").pack(pady=5)
|
| 92 |
+
positive_entry = tk.Entry(root, width=60)
|
| 93 |
+
positive_entry.pack(pady=3)
|
| 94 |
+
|
| 95 |
+
tk.Label(root, text="反面單詞 (可選, 以逗號分隔):").pack(pady=5)
|
| 96 |
+
negative_entry = tk.Entry(root, width=60)
|
| 97 |
+
negative_entry.pack(pady=3)
|
| 98 |
|
| 99 |
+
tk.Label(root, text="輸入檔案 (可多個, 以逗號分隔):").pack(pady=5)
|
| 100 |
+
files_entry = tk.Entry(root, width=60)
|
| 101 |
+
files_entry.pack(pady=3)
|
| 102 |
+
tk.Button(root, text="選擇檔案", command=browse_files).pack(pady=3)
|
| 103 |
|
| 104 |
+
tk.Label(root, text="輸出目錄 (可留空自動建立):").pack(pady=5)
|
| 105 |
+
output_dir_entry = tk.Entry(root, width=60)
|
| 106 |
+
output_dir_entry.pack(pady=3)
|
| 107 |
+
tk.Button(root, text="選擇資料夾", command=browse_directory).pack(pady=3)
|
| 108 |
|
| 109 |
+
tk.Button(root, text="開始處理", command=on_process).pack(pady=10)
|
|
|
|
|
|
|
| 110 |
|
|
|
|
| 111 |
root.mainloop()
|