-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsst_filter.py
More file actions
317 lines (289 loc) · 15.1 KB
/
Copy pathsst_filter.py
File metadata and controls
317 lines (289 loc) · 15.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
SST persona filtering core module.
Contains the LinkedInFilter class and all filtering logic, so main.py can be a thin CLI entrypoint.
"""
import csv
import re
import json
import argparse # kept for potential future reuse in module-level utilities
import yaml
import collections
import unicodedata
import io
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Any
import logging
# Configure logging for this module
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class LinkedInFilter:
"""Main class for filtering LinkedIn connections based on persona configuration."""
def __init__(self, config_path: str = "config.yaml"):
self.config = self._load_config(config_path)
self.active_persona = self.config['active_persona']
self.persona_config = self.config['personas'][self.active_persona]
# Default mode comes from config; CLI may override in main.py
self.mode = self.config.get('matching', {}).get('default_mode', 'sst_only')
if self.config['debug']['enabled']:
level = getattr(logging, self.config['debug']['log_level'])
logger.setLevel(level)
self.enterprise_list = self.build_enterprise_list()
logger.info(f"Initialized with persona: {self.active_persona}")
logger.info(f"Persona: {self.persona_config['name']}")
def _load_config(self, config_path: str) -> Dict[str, Any]:
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
logger.info(f"Configuration loaded from {config_path}")
return config
except FileNotFoundError:
logger.error(f"Configuration file not found: {config_path}")
raise
except yaml.YAMLError as e:
logger.error(f"Error parsing YAML configuration: {e}")
raise
def normalize_text(self, text: str) -> str:
if not text:
return ""
if self.config['language_support']['remove_accents']:
text = unicodedata.normalize('NFD', text)
text = ''.join(char for char in text if unicodedata.category(char) != 'Mn')
if self.config['language_support']['lowercase']:
text = text.lower()
if self.config['language_support']['normalize_whitespace']:
text = re.sub(r'\s+', ' ', text.strip())
return text
def extract_company_tokens(self, company_name: str) -> set:
normalized = self.normalize_text(company_name)
suffixes = r'\b(inc|llc|ltd|limited|plc|ag|sa|nv|se|co|corp|corporation|company|ltda|s\.?a\.?)\b'
normalized = re.sub(suffixes, '', normalized)
normalized = re.sub(r'[^\w\s]', ' ', normalized)
tokens = [t for t in normalized.split() if len(t) > 2 and t not in {'and', 'the', 'group', 'global'}]
return set(tokens)
def is_innovation_role(self, position: str, company_tokens: set) -> Tuple[bool, Optional[str], List[str], List[str]]:
"""General persona matching: (is_match, primary_category, matched_keywords, matched_categories)."""
if not position:
return False, None, [], []
pos_normalized = self.normalize_text(position)
filtered_position = ' '.join(word for word in pos_normalized.split() if word not in company_tokens)
# Exclusions
exclude_patterns = self.config['validation']['exclude_patterns']
for pattern in exclude_patterns:
if pattern.lower() in filtered_position:
logger.debug(f"Position excluded due to pattern '{pattern}': {position}")
return False, None, [], []
# Category matches
keywords_config = self.persona_config['keywords']
category_hits: Dict[str, List[str]] = {}
total_hits = 0
for category, patterns in keywords_config.items():
matches_for_cat: List[str] = []
for pattern in patterns:
pat = pattern
if not pat.startswith('^') and '.*' not in pat:
pat = r'\b' + re.escape(pat) + r'\b'
if re.search(pat, filtered_position, re.IGNORECASE):
matches_for_cat.append(pattern)
if matches_for_cat:
category_hits[category] = matches_for_cat
total_hits += len(matches_for_cat)
if not category_hits:
return False, None, [], []
# Multi-signal rules
cfg = self.config.get('matching', {})
min_total = int(cfg.get('min_total_matches', cfg.get('min_keyword_match', 1)))
required_groups = set(cfg.get('required_groups', []))
low_signal_groups = set(cfg.get('low_signal_groups', []))
allow_low_signal_only = bool(cfg.get('allow_low_signal_only', False))
cats = set(category_hits.keys())
high_signal_hit = True if not required_groups else bool(cats & required_groups)
low_only = bool(cats) and cats.issubset(low_signal_groups) if low_signal_groups else False
if total_hits < min_total:
return False, None, [], []
if not high_signal_hit:
return False, None, [], []
if low_only and not allow_low_signal_only:
return False, None, [], []
# Primary category by weight
weights = self.config['matching'].get('category_weights', {})
def cat_score(cat: str) -> Tuple[float, int]:
return (float(weights.get(cat, 0.5)), len(category_hits.get(cat, [])))
primary_category = max(category_hits.keys(), key=lambda c: cat_score(c))
matched_categories = list(category_hits.keys())
matched_keywords: List[str] = []
for _, kws in category_hits.items():
matched_keywords.extend(kws)
return True, primary_category, matched_keywords, matched_categories
def build_enterprise_list(self) -> List[str]:
enterprises = []
enterprise_config = self.config['enterprise_companies']
for _, companies in enterprise_config.items():
enterprises.extend([self.normalize_text(company) for company in companies])
logger.info(f"Built enterprise list with {len(enterprises)} companies")
return enterprises
def is_enterprise_company(self, company: str) -> Tuple[bool, Optional[str]]:
if not company:
return False, None
company_normalized = self.normalize_text(company)
for enterprise in self.enterprise_list:
pattern = r'\b' + re.escape(enterprise) + r'\b'
if re.search(pattern, company_normalized):
logger.debug(f"Enterprise match found: {company} -> {enterprise}")
return True, enterprise
return False, None
def determine_seniority(self, position: str) -> str:
position_normalized = self.normalize_text(position)
seniority_config = self.config['matching']['seniority_levels']
for level, keywords in seniority_config.items():
for keyword in keywords:
if keyword.lower() in position_normalized:
return level
return 'specialist'
def calculate_match_confidence(self, primary_category: str, matched_keywords: List[str]) -> float:
if not primary_category:
return 0.0
category_weights = self.config['matching']['category_weights']
base_confidence = category_weights.get(primary_category, 0.5)
keyword_boost = min(len(matched_keywords) * 0.1, 0.3)
return round(min(base_confidence + keyword_boost, 1.0), 3)
def read_linkedin_csv(self, input_path: str) -> csv.DictReader:
try:
with open(input_path, 'r', encoding='utf-8-sig') as f:
lines = f.readlines()
except UnicodeDecodeError:
with open(input_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
header_line_idx = None
for i, line in enumerate(lines):
if line.startswith('First Name,'):
header_line_idx = i
break
if header_line_idx is None:
raise ValueError("Could not find CSV headers starting with 'First Name,'")
csv_data = ''.join(lines[header_line_idx:])
return csv.DictReader(io.StringIO(csv_data))
def process_connections(self, input_path: str) -> List[Dict[str, Any]]:
logger.info(f"Processing connections from {input_path}")
reader = self.read_linkedin_csv(input_path)
results: List[Dict[str, Any]] = []
stats = {
'total_connections': 0,
'persona_roles': 0,
'enterprise_companies': 0,
'final_matches': 0,
'categories': collections.defaultdict(int),
'seniority_levels': collections.defaultdict(int),
'companies': collections.defaultdict(int)
}
for row in reader:
stats['total_connections'] += 1
first_name = row.get('First Name', '').strip()
last_name = row.get('Last Name', '').strip()
company = row.get('Company', '').strip()
position = row.get('Position', '').strip()
if not all([first_name, last_name, company, position]):
logger.debug("Skipping row with missing required fields")
continue
company_tokens = self.extract_company_tokens(company)
is_match, primary_category, matched_keywords, matched_categories = self.is_innovation_role(position, company_tokens)
if is_match:
stats['persona_roles'] += 1
stats['categories'][primary_category] += 1
is_enterprise, enterprise_match = self.is_enterprise_company(company)
if is_enterprise:
stats['enterprise_companies'] += 1
passes_gate = is_match if self.mode == 'sst_only' else (is_match and is_enterprise)
if passes_gate:
seniority = self.determine_seniority(position)
confidence = self.calculate_match_confidence(primary_category, matched_keywords)
result = {
'First Name': first_name,
'Last Name': last_name,
'Company': company,
'Position': position,
'URL': row.get('URL', ''),
'Email Address': row.get('Email Address', ''),
'Connected On': row.get('Connected On', ''),
'match_primary_category': primary_category,
'matched_categories': '|'.join(matched_categories),
'matched_keywords': '|'.join(matched_keywords),
'enterprise_match': enterprise_match or '',
'match_confidence': confidence,
'seniority_level': seniority,
'persona': self.active_persona
}
results.append(result)
stats['final_matches'] += 1
stats['seniority_levels'][seniority] += 1
stats['companies'][company] += 1
logger.info(f"Processing complete: {stats['final_matches']} matches from {stats['total_connections']} connections")
return results, dict(stats)
def save_results(self, results: List[Dict[str, Any]], stats: Dict[str, Any], output_dir: Path) -> None:
output_dir.mkdir(exist_ok=True)
persona_name = self.active_persona
results_filename = f"{persona_name}_{self.config['output']['results_filename']}"
summary_filename = f"{persona_name}_{self.config['output']['summary_filename']}"
if results:
results_path = output_dir / results_filename
with open(results_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=results[0].keys())
writer.writeheader()
writer.writerows(results)
logger.info(f"Results saved to {results_path}")
summary = {
'persona': {
'name': self.persona_config['name'],
'description': self.persona_config['description'],
'active_persona': self.active_persona
},
'statistics': stats,
'match_rates': {
'persona_rate': f"{(stats['final_matches']/stats['total_connections']*100):.1f}%" if stats['total_connections'] > 0 else "0%",
'enterprise_rate': f"{(stats['enterprise_companies']/stats['total_connections']*100):.1f}%" if stats['total_connections'] > 0 else "0%"
},
'top_companies': dict(collections.Counter(stats['companies']).most_common(10)),
'configuration_used': {
'fuzzy_threshold': self.config['matching']['fuzzy_threshold'],
'min_keyword_match': self.config['matching']['min_keyword_match'],
'category_weights': self.config['matching']['category_weights'],
'mode': self.mode
}
}
summary_path = output_dir / summary_filename
with open(summary_path, 'w', encoding='utf-8') as f:
json.dump(summary, f, indent=2, ensure_ascii=False)
logger.info(f"Summary saved to {summary_path}")
def display_results(self, results: List[Dict[str, Any]], stats: Dict[str, Any]) -> None:
print(f"\n{'='*80}")
print(f"LINKEDIN CONNECTION FILTER RESULTS")
print(f"Persona: {self.persona_config['name']}")
print(f"{'='*80}")
print(f"\nSUMMARY STATISTICS:")
print(f"Total connections processed: {stats['total_connections']}")
print(f"SST-related matches: {stats['final_matches']} ({(stats['final_matches']/stats['total_connections']*100):.1f}%)")
print(f"Enterprise company connections: {stats['enterprise_companies']} ({(stats['enterprise_companies']/stats['total_connections']*100):.1f}%)")
if stats['categories']:
print(f"\nCATEGORIES:")
for category, count in stats['categories'].items():
print(f" {category}: {count}")
if stats['seniority_levels']:
print(f"\nSENIORITY LEVELS:")
for level, count in stats['seniority_levels'].items():
print(f" {level}: {count}")
if results:
print(f"\nTOP MATCHED CONNECTIONS:")
print("-" * 120)
for i, result in enumerate(results[:10]):
print(f"{i+1:2d}. {result['First Name']} {result['Last Name']}")
print(f" Position: {result['Position']}")
print(f" Company: {result['Company']}")
print(f" Category: {result['match_primary_category']} | Confidence: {result['match_confidence']} | Seniority: {result['seniority_level']}")
print(f" Categories: {result['matched_categories']}")
print(f" Keywords: {result['matched_keywords']}")
if result['Email Address']:
print(f" Email: {result['Email Address']}")
print(f" LinkedIn: {result['URL']}")
print("-" * 120)
if len(results) > 10:
print(f"... and {len(results) - 10} more matches (see CSV file for complete results)")