# データ
documents = [
"Python is a popular programming language.",
"It is known for its simplicity and readability.",
"Python has a large community of developers.",
"Python is widely used in web development."
]
# データの前処理
def preprocess(text):
return text.lower()
# インデックスの作成
def create_index(documents):
index = {}
for doc_id, doc in enumerate(documents):
doc = preprocess(doc)
words = doc.split()
for word in words:
if word not in index:
index[word] = []
index[word].append(doc_id)
return index
# クエリ処理
def search(query, index):
query = preprocess(query)
words = query.split()
result = set(range(len(documents)))
for word in words:
if word in index:
result &= set(index[word])
return [documents[i] for i in result]
# メイン
if __name__ == "__main__":
index = create_index(documents)
while True:
query = input("検索クエリを入力してください (終了するには 'exit' を入力): ")
if query == 'exit':
break
results = search(query, index)
if results:
for i, result in enumerate(results, start=1):
print(f"{i}. {result}")
else:
print("一致する文書はありません。")