code
✅
from whoosh.index import open_dir
from whoosh.qparser import QueryParser
from whoosh.searcher import MultifieldSearcher
schema = Schema(content=TEXT(stored=True))
ix1 = create_in("悩み", schema)
ix2 = create_in("転職活動", schema)
writer = ix.writer()
# 複数のインデックスを開く
kaiix1 = open_dir("悩み")
kaiix2 = open_dir("転職活動")
for text in text_data:
writer.add_document(content=text)
writer.commit()
# 検索
query = QueryParser("content", ix.schema).parse('検索ワード')
with ix.searcher() as searcher:
results = searcher.search(query)
for result in results:
print(result['content'])
# 検索条件が二つの時
searcher = MultifieldSearcher([kaiix1.searcher(), kaiix2.searcher()])
# 検索
query = QueryParser("content", ix1.schema).parse("検索ワード")
results = searcher.search(query)
for result in results:
print(result['content'])
いるもん
✅
pip install whoosh
Tkインスト
CFLAGS="-I$(brew --prefix tcl-tk)/include" \
LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
pyenv install --force 3.13.1
Tkあるか確認
☑️
import tkinter as tk
root = tk.Tk()
root.mainloop()
Tkないと出るエラー
インスト
GUI作成
Tk
import tkinter as tk
from tkinter import messagebox
def search():
search_query = entry.get()
# Whoosh検索コードをここで実行
# 例: results = whoosh_search_function(search_query)
messagebox.showinfo("検索結果", "検索結果を表示")
window = tk.Tk()
window.title("検索エンジン")
label = tk.Label(window, text="検索:")
label.pack()
entry = tk.Entry(window)
entry.pack()
button = tk.Button(window, text="検索", command=search)
button.pack()
window.mainloop()
Pyqt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLineEdit
class SearchWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('検索エンジン')
self.layout = QVBoxLayout()
self.search_input = QLineEdit(self)
self.layout.addWidget(self.search_input)
self.search_button = QPushButton('検索', self)
self.layout.addWidget(self.search_button)
self.setLayout(self.layout)
self.search_button.clicked.connect(self.search)
def search(self):
search_query = self.search_input.text()
# Whoosh検索コードをここで実行
# 例: results = whoosh_search_function(search_query)
print(f'検索結果: {search_query}')
app = QApplication([])
window = SearchWindow()
window.show()
app.exec_()
TkとPyqtの違い
tk : 軽量 複雑な実装はできない / 簡単
Pyqt
複雑レイアウト、データベースと連携、グラフ描画 / ちょいむずい
権限追加
chmod 644 /path/to/url.json