0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel / PDFに検索エンジンつける

Posted at

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ないと出るエラー

スクリーンショット 2025-04-01 21.33.36.png

インスト

スクリーンショット 2025-04-01 22.00.55.png


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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?