-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNACEConverter.py
More file actions
437 lines (344 loc) · 15.1 KB
/
Copy pathNACEConverter.py
File metadata and controls
437 lines (344 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""
NACE Code Converter
====================
A Python package for converting NACE codes to their plaintext descriptions
and searching for codes by keywords.
NACE (Nomenclature of Economic Activities) is the European statistical
classification of economic activities.
"""
import csv
import os
from pathlib import Path
from typing import List, Dict, Optional, Union
import re
class NACEConverter:
"""
A converter class for NACE codes.
This class provides functionality to:
- Convert NACE codes to their plaintext descriptions
- Search for NACE codes by keywords
- Access hierarchical information about NACE codes
The NACE codes data is loaded from the nacecodes.csv file.
Attributes:
codes_dict (dict): Dictionary mapping codes to their full information
normalized_codes (dict): Maps normalized codes to original codes
search_index (dict): Inverted index for efficient text searching
Examples:
>>> converter = NACEConverter()
>>> converter.get_description("01.1")
'Growing of non-perennial crops'
>>> converter.search_codes("farming")
[{'code': '01.5', 'description': 'Mixed farming', 'level': 3}, ...]
"""
def __init__(self, csv_path: Optional[str] = None):
"""
Initialize the NACE converter.
Args:
csv_path: Optional path to the nacecodes.csv file.
If not provided, looks for the file in the same directory.
Raises:
ValueError: If the CSV data has an unexpected format
FileNotFoundError: If the CSV file cannot be found
"""
self.codes_dict = {}
self.normalized_codes = {} # Maps normalized codes to original codes
self.search_index = {}
self._load_data(csv_path)
self._build_search_index()
def _find_csv_path(self) -> str:
"""
Find the path to the nacecodes.csv file.
Returns:
str: Path to the CSV file
Raises:
FileNotFoundError: If the file cannot be found
"""
# Get the directory where this module is located
module_dir = Path(__file__).parent
# Try multiple possible locations
possible_paths = [
module_dir / 'nacecodes.csv', # Same directory as this file
Path('nacecodes.csv'), # Current working directory
module_dir / 'data' / 'nacecodes.csv', # data subdirectory
]
for path in possible_paths:
if path.exists():
return str(path)
raise FileNotFoundError(
f"nacecodes.csv not found. Searched in: {[str(p) for p in possible_paths]}"
)
def _clean_value(self, value):
"""
Clean a value from the CSV by handling None and stripping quotes.
Args:
value: The value to clean (may be None or string)
Returns:
Cleaned string value
"""
if value is None:
return ''
return str(value).strip('"')
def _load_data(self, csv_path: Optional[str] = None):
"""
Load NACE codes from CSV file.
Args:
csv_path: Path to the CSV file
Raises:
FileNotFoundError: If the file doesn't exist
ValueError: If the CSV format is unexpected
"""
if csv_path is None:
csv_path = self._find_csv_path()
if not Path(csv_path).exists():
raise FileNotFoundError(f"CSV file not found at: {csv_path}")
try:
with open(csv_path, 'r', encoding='utf-8') as f:
# Handle semicolon-separated CSV
reader = csv.DictReader(f, delimiter=';')
for row in reader:
# Clean the data - handle None values and remove quotes
code = self._clean_value(row.get('code'))
if not code: # Skip empty codes
continue
# Parse level, handling None and empty strings
level_str = self._clean_value(row.get('level'))
try:
level = int(level_str) if level_str else 0
except ValueError:
level = 0
self.codes_dict[code] = {
'code': code,
'parentCode': self._clean_value(row.get('parentCode')),
'level': level,
'name': self._clean_value(row.get('name')),
'shortName': self._clean_value(row.get('shortName')),
'notes': self._clean_value(row.get('notes')),
'validFrom': self._clean_value(row.get('validFrom')),
'validTo': self._clean_value(row.get('validTo'))
}
# Store normalized version for flexible lookup
normalized = self._normalize_code(code)
self.normalized_codes[normalized] = code
except Exception as e:
raise ValueError(f"Error reading CSV file: {e}")
if not self.codes_dict:
raise ValueError("No NACE codes were loaded from the CSV file")
def _normalize_code(self, code: str) -> str:
"""
Normalize a NACE code by removing dots and converting to uppercase.
Args:
code: The code to normalize
Returns:
Normalized code without dots
Examples:
>>> _normalize_code("01.30")
"0130"
>>> _normalize_code("A")
"A"
"""
return code.replace('.', '').replace('-', '').upper().strip()
def _build_search_index(self):
"""
Build an inverted index for efficient text searching.
Creates a mapping from words to the codes that contain them.
"""
for code, info in self.codes_dict.items():
# Combine searchable text fields
searchable_text = ' '.join([
info.get('name', ''),
info.get('shortName', ''),
info.get('notes', '')
]).lower()
# Extract words (alphanumeric sequences)
words = re.findall(r'\b\w+\b', searchable_text)
# Add to index
for word in words:
if word not in self.search_index:
self.search_index[word] = set()
self.search_index[word].add(code)
def get_description(self, code: str) -> Optional[str]:
"""
Get the plaintext description for a NACE code.
Handles codes with or without dots (e.g., "01.30" and "0130" return same result).
Args:
code: The NACE code to look up
Returns:
The description (name) of the code, or None if not found
Examples:
>>> converter.get_description("01")
'Crop and animal production, hunting and related service activities'
>>> converter.get_description("01.1")
'Growing of non-perennial crops'
>>> converter.get_description("011") # Same as "01.1"
'Growing of non-perennial crops'
"""
# Try direct lookup first
code_clean = code.strip().strip('"')
if code_clean in self.codes_dict:
return self.codes_dict[code_clean]['name']
# Try normalized lookup
normalized = self._normalize_code(code)
if normalized in self.normalized_codes:
original_code = self.normalized_codes[normalized]
return self.codes_dict[original_code]['name']
return None
def get_full_info(self, code: str) -> Optional[Dict]:
"""
Get complete information for a NACE code.
Handles codes with or without dots (e.g., "01.30" and "0130" return same result).
Args:
code: The NACE code to look up
Returns:
Dictionary with all information about the code, or None if not found
Examples:
>>> info = converter.get_full_info("01.1")
>>> print(info['name'])
'Growing of non-perennial crops'
>>> info2 = converter.get_full_info("011") # Same result
>>> print(info2['name'])
'Growing of non-perennial crops'
"""
# Try direct lookup first
code_clean = code.strip().strip('"')
if code_clean in self.codes_dict:
return self.codes_dict[code_clean].copy()
# Try normalized lookup
normalized = self._normalize_code(code)
if normalized in self.normalized_codes:
original_code = self.normalized_codes[normalized]
return self.codes_dict[original_code].copy()
return None
def search_codes(self, keyword: str, max_results: Optional[int] = None) -> List[Dict]:
"""
Search for NACE codes containing a keyword.
Performs case-insensitive search across code names, short names, and notes.
Args:
keyword: The search term (case-insensitive)
max_results: Maximum number of results to return (None for all)
Returns:
List of dictionaries with matching codes, sorted by relevance.
Each dictionary contains 'code', 'description', and 'level'.
Examples:
>>> converter.search_codes("painting")
[{'code': '43.34', 'description': 'Painting and glazing', 'level': 4}, ...]
>>> converter.search_codes("agriculture", max_results=5)
# Returns up to 5 agriculture-related codes
"""
keyword = keyword.lower().strip()
results = []
scores = {} # Track relevance scores
# Direct word match from index
matching_codes = set()
words = re.findall(r'\b\w+\b', keyword)
# Find codes containing any of the search words
for word in words:
if word in self.search_index:
matching_codes.update(self.search_index[word])
# Also check for partial matches and multi-word phrases
for code, info in self.codes_dict.items():
searchable_text = ' '.join([
info.get('name', ''),
info.get('shortName', ''),
info.get('notes', '')
]).lower()
# Calculate relevance score
score = 0
# Exact phrase match (highest priority)
if keyword in searchable_text:
score += 100
matching_codes.add(code)
# Individual word matches
for word in words:
if word in searchable_text:
score += 10
# Bonus for word at start of description
if info.get('name', '').lower().startswith(word):
score += 5
# Partial word matches (lower priority)
if score == 0:
for word in words:
if len(word) > 3 and word in searchable_text:
score += 1
matching_codes.add(code)
if code in matching_codes:
scores[code] = score
# Build results list
for code in matching_codes:
info = self.codes_dict[code]
result = {
'code': code,
'description': info['name'],
'level': info['level']
}
# Add parent code if available
if info.get('parentCode'):
result['parentCode'] = info['parentCode']
# Add short name if different from name
if info.get('shortName') and info['shortName'] != info['name']:
result['shortName'] = info['shortName']
results.append((scores.get(code, 0), result))
# Sort by relevance score (descending) and then by code
results.sort(key=lambda x: (-x[0], x[1]['code']))
# Extract just the result dictionaries
results = [r[1] for r in results]
# Apply max_results limit if specified
if max_results is not None and max_results > 0:
results = results[:max_results]
return results
def get_all_codes(self) -> List[str]:
"""
Get a list of all available NACE codes.
Returns:
List of all NACE codes in the dataset
Examples:
>>> all_codes = converter.get_all_codes()
>>> print(len(all_codes))
# Number of codes in the dataset
"""
return sorted(list(self.codes_dict.keys()))
def get_codes_by_level(self, level: int) -> List[Dict]:
"""
Get all NACE codes at a specific hierarchical level.
Args:
level: The hierarchy level (1-4 typically)
Returns:
List of dictionaries with codes at the specified level
Examples:
>>> level_2_codes = converter.get_codes_by_level(2)
# Returns all 2-digit codes
"""
results = []
for code, info in self.codes_dict.items():
if info['level'] == level:
results.append({
'code': code,
'description': info['name'],
'parentCode': info.get('parentCode', '')
})
return sorted(results, key=lambda x: x['code'])
def get_children(self, parent_code: str) -> List[Dict]:
"""
Get all direct children of a NACE code.
Args:
parent_code: The parent NACE code
Returns:
List of dictionaries with child codes
Examples:
>>> children = converter.get_children("01")
# Returns codes like "01.1", "01.2", etc.
"""
# Normalize the parent code for comparison
parent_code_clean = parent_code.strip().strip('"')
# Also check normalized version
normalized_parent = self._normalize_code(parent_code)
if normalized_parent in self.normalized_codes:
parent_code_clean = self.normalized_codes[normalized_parent]
results = []
for code, info in self.codes_dict.items():
if info.get('parentCode') == parent_code_clean:
results.append({
'code': code,
'description': info['name'],
'level': info['level']
})
return sorted(results, key=lambda x: x['code'])