File size: 9,233 Bytes
2f33d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Skill extraction service using spaCy NLP.



This module extracts technical and soft skills from resume text

and job descriptions using spaCy for NER (Named Entity Recognition)

combined with a predefined skills database.



Why spaCy?

- Fast and efficient for production use

- Can be extended with custom entity patterns

- Supports rule-based and ML-based extraction

- Memory efficient compared to other NLP libraries



Extraction Strategy:

1. Load predefined skills list (technical + soft skills)

2. Normalize text (lowercase, remove special characters)

3. Use fuzzy matching for typo tolerance

4. Return matched skills in original case

"""

import logging
import json
import os
from typing import List, Set, Optional
import spacy
from difflib import SequenceMatcher

logger = logging.getLogger(__name__)


class SkillExtractor:
    """

    Extract skills from text using spaCy and predefined skill list.

    

    This class:

    - Loads a predefined skills database

    - Normalizes text for matching

    - Performs fuzzy matching for typo tolerance

    - Returns unique, deduplicated skills

    """
    
    def __init__(self, skills_json_path: Optional[str] = None):
        """

        Initialize the skill extractor.

        

        Args:

            skills_json_path: Path to skills.json file.

                             If None, uses default in same directory.

        """
        try:
            # Load spaCy model
            self.nlp = spacy.load("en_core_web_sm")
            logger.info("Loaded spaCy model: en_core_web_sm")
        except OSError:
            logger.error("spaCy model not found. Run: python -m spacy download en_core_web_sm")
            raise
        
        # Load skills database
        if skills_json_path is None:
            skills_json_path = os.path.join(
                os.path.dirname(__file__),
                '..',
                'skills.json'
            )
        
        self.skills_list = self._load_skills(skills_json_path)
        logger.info(f"Loaded {len(self.skills_list)} skills from database")
    
    @staticmethod
    def _load_skills(json_path: str) -> List[str]:
        """

        Load skills from JSON file.

        

        Args:

            json_path: Path to skills.json

            

        Returns:

            List of skills

        """
        try:
            with open(json_path, 'r') as f:
                data = json.load(f)
                # Handle different JSON structures
                if isinstance(data, dict) and 'skills' in data:
                    return data['skills']
                elif isinstance(data, list):
                    return data
                else:
                    logger.warning("Unexpected skills.json structure, using defaults")
                    return SkillExtractor._get_default_skills()
        except FileNotFoundError:
            logger.warning(f"skills.json not found at {json_path}, using default skills")
            return SkillExtractor._get_default_skills()
    
    @staticmethod
    def _get_default_skills() -> List[str]:
        """

        Return default skills list if JSON is not available.

        

        Returns:

            Default list of common technical and soft skills

        """
        return [
            # Programming Languages
            "Python", "Java", "JavaScript", "TypeScript", "C++", "C#", "Go",
            "Rust", "PHP", "Ruby", "Swift", "Kotlin", "R", "MATLAB",
            
            # Web Frameworks
            "FastAPI", "Django", "Flask", "Express", "React", "Vue", "Angular",
            "Next.js", "Spring", "ASP.NET", "Rails", "Laravel",
            
            # Databases
            "PostgreSQL", "MySQL", "MongoDB", "Redis", "Cassandra",
            "DynamoDB", "Elasticsearch", "Oracle", "SQLite",
            
            # Cloud & DevOps
            "AWS", "Azure", "GCP", "Docker", "Kubernetes", "Terraform",
            "Jenkins", "GitLab CI", "GitHub Actions", "Ansible",
            
            # Tools & Technologies
            "Git", "Linux", "Windows", "macOS", "REST API", "GraphQL",
            "Microservices", "CI/CD", "Docker Compose", "Nginx",
            
            # Data & ML
            "Machine Learning", "Deep Learning", "TensorFlow", "PyTorch",
            "Pandas", "NumPy", "Scikit-learn", "SQL", "Data Analysis",
            
            # Soft Skills
            "Leadership", "Communication", "Problem Solving", "Team Collaboration",
            "Project Management", "Agile", "Scrum", "Critical Thinking"
        ]
    
    def _normalize_text(self, text: str) -> str:
        """

        Normalize text for skill matching.

        

        Operations:

        - Convert to lowercase

        - Remove special characters (keep alphanumeric and spaces)

        

        Args:

            text: Raw text

            

        Returns:

            Normalized text

        """
        import re
        text = text.lower()
        # Remove special characters but keep spaces
        text = re.sub(r'[^a-z0-9\s+#\.]', ' ', text)
        # Remove extra spaces
        text = re.sub(r'\s+', ' ', text)
        return text.strip()
    
    def _fuzzy_match(self, text: str, skill: str, threshold: float = 0.85) -> bool:
        """

        Perform fuzzy matching between text and skill.

        

        Uses SequenceMatcher for typo tolerance.

        Threshold of 0.85 means 85% character match is required.

        

        Args:

            text: Text to search in

            skill: Skill to find

            threshold: Minimum match ratio (0-1)

            

        Returns:

            True if skill is found (with fuzzy match)

        """
        normalized_skill = skill.lower()
        
        # Check for exact match first
        if normalized_skill in text:
            return True
        
        # Fuzzy matching for typos/variations
        ratio = SequenceMatcher(None, text, normalized_skill).ratio()
        return ratio >= threshold
    
    def extract_potential_skills(self, text: str) -> Set[str]:
        """

        Extract potential skills from text using regex and heuristics.

        

        This looks for:

        - Common tech patterns (Node.js, C++, C#, .NET)

        - PascalCase/camelCase words that might be frameworks

        - Uppercase acronyms (AWS, GCP, SQL)

        - Intersection with the global skills list

        

        Args:

            text: Raw text to extract from

            

        Returns:

            Set of potential skill strings

        """
        import re
        
        # 1. Start with matches from the global list (reliable)
        found_skills = set()
        normalized_text = self._normalize_text(text)
        
        for skill in self.skills_list:
            if self._fuzzy_match(normalized_text, skill):
                found_skills.add(skill)
        
        # 2. Add regex-based tech patterns (less reliable, but catches new terms)
        # Patterns like Node.js, React.js, C++, C#, .NET, Web3, Vue3
        tech_patterns = [
            r'\b[A-Z][a-z]+(?:\.[a-z]+|[A-Z][a-z]+)+\b', # PascalCase/camelCase
            r'\b[A-Za-z]+\.js\b',                        # *.js
            r'\b[A-Z]{2,}\b',                            # Acronyms (AWS, GCP)
            r'\b[A-Za-z]+\+\+\b',                        # C++
            r'\b[A-Za-z]+#\b',                           # C#
            r'\.\b[A-Za-z]+\b',                          # .NET
        ]
        
        for pattern in tech_patterns:
            matches = re.findall(pattern, text)
            for match in matches:
                # Basic validation: length and not common words
                if len(match) > 1 and match.lower() not in ['the', 'and', 'for', 'with', 'from']:
                    found_skills.add(match)
        
        return found_skills

    def classify_skill(self, skill: str) -> str:
        """

        Classify a skill as 'tech' or 'secondary'.

        

        Args:

            skill: Skill name

            

        Returns:

            'tech' or 'secondary'

        """
        soft_skill_keywords = [
            'communication', 'leadership', 'problem solving', 'team', 
            'management', 'agile', 'scrum', 'critical thinking', 
            'adaptability', 'creativity', 'time management', 'collaboration'
        ]
        
        skill_lower = skill.lower()
        for keyword in soft_skill_keywords:
            if keyword in skill_lower:
                return 'secondary'
                
        return 'tech'

    def extract_skills(self, text: str) -> List[str]:
        """

        Extract skills from text.

        """
        if not text:
            return []
            
        potential_skills = self.extract_potential_skills(text)
        return sorted(list(potential_skills))

    def extract_skills_with_context(self, text: str) -> List[str]:
        """

        Fallback to basic extraction for now.

        """
        return self.extract_skills(text)