Oosayam commited on
Commit
7742c21
·
verified ·
1 Parent(s): 9ca1a9d

Upload 2 files

Browse files
Files changed (2) hide show
  1. danbooru_e621_merged.txt +0 -0
  2. 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(words, output_directory):
7
- # 生成輸出的檔名,將逗號替換為底線
8
- output_filename = "_".join(words) + ".txt"
9
-
10
- # 確保檔案名稱最後不會有下劃線
11
- if output_filename.endswith("_.txt"):
12
- output_filename = output_filename[:-5] + ".txt"
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
- for filename in os.listdir('.'):
23
- # 只處理純文字檔案
24
- if filename.endswith('.txt') and filename != temp_output_filename:
25
- try:
26
- with open(filename, 'r', encoding='utf-8') as infile:
27
- # 處理每一行
28
- for line in infile:
29
- # 檢查行是否包含所有的輸入單詞
30
- if all(word in line for word in words):
31
- # 將符合條件的行寫入輸出檔案
32
- outfile.write(line)
33
- except Exception as e:
34
- print(f"無法讀取檔案 {filename}: {e}")
35
-
36
- # 將臨時檔案移動到目標目錄
37
  final_output_path = os.path.join(output_directory, output_filename)
38
- shutil.move(temp_output_filename, final_output_path)
39
-
40
- messagebox.showinfo("完成", f"符合條件的行已寫入檔案 {final_output_path}")
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def on_process():
43
- input_words = input_entry.get()
44
- words = [word.strip() for word in input_words.split(",")]
45
-
46
- # 檢查單詞是否超過十個
47
- if len(words) > 10:
48
- messagebox.showerror("錯誤", "輸入的單詞不能超過十個")
49
  return
50
-
51
- output_directory = output_dir_entry.get().strip()
52
-
53
- # 如果沒有輸入,使用預設目錄
54
- if not output_directory:
55
- output_directory = r"I:\sd-forge\extensions\sd-dynamic-prompts\wildcards"
56
-
57
- process_files(words, output_directory)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def browse_directory():
60
- directory = filedialog.askdirectory()
61
- if directory:
62
  output_dir_entry.delete(0, tk.END)
63
- output_dir_entry.insert(0, directory)
64
 
65
- # 創建主窗口
66
  root = tk.Tk()
67
  root.title("詞彙檢測工具")
68
 
69
- # 輸入詞彙框
70
- tk.Label(root, text="請輸入一組單詞(最多十個單詞,用逗號分隔):").pack(pady=5)
71
- input_entry = tk.Entry(root, width=50)
72
- input_entry.pack(pady=5)
 
 
 
73
 
74
- # 輸出目錄框
75
- tk.Label(root, text="請輸入輸出目錄(如果不存在,將會自動創建):").pack(pady=5)
76
- output_dir_entry = tk.Entry(root, width=50)
77
- output_dir_entry.pack(pady=5)
78
 
79
- # 瀏覽目錄按鈕
80
- browse_button = tk.Button(root, text="瀏覽...", command=browse_directory)
81
- browse_button.pack(pady=5)
 
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()