File size: 2,574 Bytes
a5778f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
import os
from pathlib import Path
from typing import Dict, List, Optional

class KnowledgeBaseManager:
    def __init__(self, knowledge_base_path: str = "knowledge_base"):
        self.kb_path = Path(knowledge_base_path)
        self.documents = {}
        self.kb_path.mkdir(exist_ok=True)

    def scan_documents(self) -> Dict:
        documents = {}
        categories = {
            'faqs': 'Frequently Asked Questions',
            'policies': 'Company Policies',
            'product_manuals': 'Product Manuals',
            # 'development' intentionally omitted
        }
        for category, desc in categories.items():
            cat_path = self.kb_path / category
            if cat_path.exists():
                docs = []
                for f in cat_path.glob('*.md'):
                    docs.append(self._analyze_document(f, category))
                documents[category] = {'description': desc, 'documents': docs}
        self.documents = documents
        return documents

    def _analyze_document(self, file_path: Path, category: str) -> Dict:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        title = next((line.strip('# ').strip() for line in content.split('\n') if line.startswith('# ')), file_path.stem)
        return {
            'filename': file_path.name,
            'title': title,
            'category': category,
            'path': str(file_path.relative_to(self.kb_path)),
            'size': file_path.stat().st_size,
            'word_count': len(content.split()),
            'char_count': len(content)
        }

    def load_document_content(self, document_path: str) -> Optional[str]:
        full = self.kb_path / document_path
        if full.exists():
            with open(full, 'r', encoding='utf-8') as f:
                return f.read()
        return None

    def search_documents(self, query: str, category: Optional[str] = None) -> List[Dict]:
        results = []
        ql = query.lower()
        for cat, info in self.documents.items():
            if category and cat != category:
                continue
            for doc in info['documents']:
                content = self.load_document_content(doc['path'])
                if not content:
                    continue
                if ql in content.lower() or ql in doc['title'].lower():
                    score = content.lower().count(ql)
                    results.append({**doc, 'relevance_score': score})
        results.sort(key=lambda x: x['relevance_score'], reverse=True)
        return results