LOGOS-SPCW-Matroska / query_kb.py
GitHub Copilot
Feature: Add ARCHITECTURE.md and display it in new UI tab
aeaae89
import json
import argparse
import os
def query_knowledge_base(query, kb_file="logos_knowledge_base.json"):
if not os.path.exists(kb_file):
print(f"Knowledge base not found at {kb_file}. Run build_kb.py first.")
return
with open(kb_file, 'r', encoding='utf-8') as f:
kb = json.load(f)
print(f"\nSearching for: '{query}' in {len(kb)} documents...\n")
print("-" * 60)
hits = []
query = query.lower()
for entry in kb:
filename = entry['filename']
full_text = entry['full_text'].lower()
if query in full_text:
# Find context
idx = full_text.find(query)
start = max(0, idx - 50)
end = min(len(full_text), idx + 50 + len(query))
context = entry['full_text'][start:end].replace('\n', ' ')
hits.append({
'filename': filename,
'path': entry['path'],
'context': f"...{context}...",
'score': full_text.count(query)
})
# Sort by number of occurrences
hits.sort(key=lambda x: x['score'], reverse=True)
if not hits:
print("No matches found.")
else:
for hit in hits[:10]: # Top 10
print(f"๐Ÿ“„ {hit['filename']} (Matches: {hit['score']})")
print(f" Path: {hit['path']}")
print(f" Context: {hit['context']}")
print("-" * 60)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Query LOGOS Knowledge Base")
parser.add_argument("query", help="Search term")
args = parser.parse_args()
query_knowledge_base(args.query)