File size: 8,585 Bytes
d18c374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
"""
Example usage script for SHL Assessment Recommender System

This script demonstrates how to use the system programmatically.
"""

import sys
import os


def example_direct_usage():
    """Example: Using the recommender directly (without API)"""
    print("\n" + "="*60)
    print("EXAMPLE 1: Direct Usage (Python)")
    print("="*60)
    
    from src.recommender import AssessmentRecommender
    from src.reranker import AssessmentReranker
    
    # Initialize recommender
    print("\nLoading recommender system...")
    recommender = AssessmentRecommender()
    
    # Load index
    if not recommender.load_index():
        print("Error: Please run 'python setup.py' first to build the index")
        return
    
    # Initialize reranker
    reranker = AssessmentReranker()
    
    # Example query
    query = "Looking for a Java developer who can lead a small team"
    print(f"\nQuery: {query}")
    
    # Get initial candidates
    print("\nGetting initial candidates...")
    candidates = recommender.recommend(query, k=15, method='faiss')
    
    # Rerank and balance
    print("Applying reranking and balancing...")
    results = reranker.rerank_and_balance(
        query=query,
        candidates=candidates,
        top_k=10,
        min_k=1,
        min_p=1
    )
    
    # Display results
    print(f"\n{'='*60}")
    print(f"Top {len(results)} Recommendations:")
    print('='*60)
    
    for assessment in results:
        print(f"\n{assessment['rank']}. {assessment['assessment_name']}")
        print(f"   Type: {assessment['test_type']}")
        print(f"   Category: {assessment['category']}")
        print(f"   Score: {assessment.get('score', 0):.4f}")
        print(f"   URL: {assessment['assessment_url']}")


def example_api_client():
    """Example: Using the API client"""
    print("\n" + "="*60)
    print("EXAMPLE 2: API Client Usage")
    print("="*60)
    
    import requests
    import json
    
    # API URL (assumes API is running)
    api_url = "http://localhost:8000"
    
    # Check health
    print("\n1. Checking API health...")
    try:
        response = requests.get(f"{api_url}/health", timeout=5)
        if response.status_code == 200:
            print(f"   ✓ API is running: {response.json()}")
        else:
            print(f"   ✗ API returned status {response.status_code}")
            print("   Please start the API: python api/main.py")
            return
    except requests.exceptions.RequestException as e:
        print(f"   ✗ Cannot connect to API: {e}")
        print("   Please start the API: python api/main.py")
        return
    
    # Get recommendations
    print("\n2. Getting recommendations...")
    
    query = "Need a data analyst with SQL and Python skills"
    print(f"   Query: {query}")
    
    payload = {
        "query": query,
        "num_results": 5,
        "use_reranking": True,
        "min_k": 1,
        "min_p": 1
    }
    
    response = requests.post(
        f"{api_url}/recommend",
        json=payload,
        timeout=30
    )
    
    if response.status_code == 200:
        result = response.json()
        
        print(f"\n{'='*60}")
        print(f"Recommendations for: {result['query']}")
        print('='*60)
        
        for rec in result['recommendations']:
            print(f"\n{rec['rank']}. {rec['assessment_name']}")
            print(f"   Type: {rec['test_type']}")
            print(f"   Category: {rec['category']}")
            print(f"   Score: {rec['score']:.2%}")
    else:
        print(f"   ✗ Error: {response.status_code}")
        print(f"   {response.text}")


def example_batch_processing():
    """Example: Batch processing multiple queries"""
    print("\n" + "="*60)
    print("EXAMPLE 3: Batch Processing")
    print("="*60)
    
    from src.recommender import AssessmentRecommender
    
    # Initialize recommender
    print("\nLoading recommender system...")
    recommender = AssessmentRecommender()
    
    if not recommender.load_index():
        print("Error: Please run 'python setup.py' first")
        return
    
    # Multiple queries
    queries = [
        "Java developer with team leadership",
        "Python data scientist",
        "Customer service representative",
        "Software engineer with problem-solving skills"
    ]
    
    print(f"\nProcessing {len(queries)} queries...")
    
    # Get recommendations for all queries
    all_recommendations = recommender.recommend_batch(queries, k=5)
    
    # Display results
    for query, recommendations in zip(queries, all_recommendations):
        print(f"\n{'='*60}")
        print(f"Query: {query}")
        print('-'*60)
        
        for i, rec in enumerate(recommendations[:3], 1):  # Show top 3
            print(f"{i}. {rec['assessment_name']} ({rec['test_type']}) - {rec['score']:.4f}")


def example_custom_filtering():
    """Example: Custom filtering and post-processing"""
    print("\n" + "="*60)
    print("EXAMPLE 4: Custom Filtering")
    print("="*60)
    
    from src.recommender import AssessmentRecommender
    
    recommender = AssessmentRecommender()
    
    if not recommender.load_index():
        print("Error: Please run 'python setup.py' first")
        return
    
    query = "Software developer position"
    print(f"\nQuery: {query}")
    
    # Get recommendations
    recommendations = recommender.recommend(query, k=20)
    
    # Filter for only technical assessments
    technical = [r for r in recommendations if r['category'] == 'Technical']
    
    print(f"\nAll recommendations: {len(recommendations)}")
    print(f"Technical only: {len(technical)}")
    
    print("\nTechnical Assessments:")
    for i, rec in enumerate(technical[:5], 1):
        print(f"{i}. {rec['assessment_name']} - Score: {rec['score']:.4f}")
    
    # Filter for only K-type assessments
    k_type = [r for r in recommendations if r['test_type'] == 'K']
    
    print(f"\nKnowledge/Skill Assessments: {len(k_type)}")
    for i, rec in enumerate(k_type[:5], 1):
        print(f"{i}. {rec['assessment_name']} - {rec['category']}")


def example_evaluation():
    """Example: Running evaluation"""
    print("\n" + "="*60)
    print("EXAMPLE 5: System Evaluation")
    print("="*60)
    
    from src.evaluator import RecommenderEvaluator
    from src.recommender import AssessmentRecommender
    from src.preprocess import DataPreprocessor
    
    # Load data
    print("\nLoading training data...")
    preprocessor = DataPreprocessor()
    data = preprocessor.preprocess()
    train_mapping = data['train_mapping']
    
    if not train_mapping:
        print("No training data available")
        return
    
    print(f"Found {len(train_mapping)} training queries")
    
    # Load recommender
    print("\nLoading recommender...")
    recommender = AssessmentRecommender()
    if not recommender.load_index():
        print("Error: Please run 'python setup.py' first")
        return
    
    # Run evaluation
    print("\nRunning evaluation (this may take a moment)...")
    evaluator = RecommenderEvaluator()
    results = evaluator.evaluate(recommender, train_mapping, k=10)
    
    # Print report
    evaluator.print_report()


def main():
    """Main function - run all examples"""
    examples = [
        ("Direct Usage", example_direct_usage),
        ("API Client", example_api_client),
        ("Batch Processing", example_batch_processing),
        ("Custom Filtering", example_custom_filtering),
        ("Evaluation", example_evaluation)
    ]
    
    print("="*60)
    print("SHL ASSESSMENT RECOMMENDER - USAGE EXAMPLES")
    print("="*60)
    print("\nAvailable examples:")
    for i, (name, _) in enumerate(examples, 1):
        print(f"{i}. {name}")
    
    print("\nSelect an example (1-5) or 'all' to run all:")
    print("(Press Enter to run Example 1)")
    
    choice = input("> ").strip().lower()
    
    if not choice:
        choice = "1"
    
    if choice == "all":
        for name, func in examples:
            try:
                func()
            except Exception as e:
                print(f"\n✗ Error in {name}: {e}")
    elif choice.isdigit() and 1 <= int(choice) <= len(examples):
        idx = int(choice) - 1
        try:
            examples[idx][1]()
        except Exception as e:
            print(f"\n✗ Error: {e}")
    else:
        print("Invalid choice")
        return 1
    
    print("\n" + "="*60)
    print("For more information, see README.md")
    print("="*60)
    
    return 0


if __name__ == "__main__":
    sys.exit(main())