File size: 1,680 Bytes
aeaae89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

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)