Spaces:
Sleeping
Sleeping
| import language_tool_python | |
| import re | |
| class ResumeReviewer: | |
| def __init__(self): | |
| self.tool = language_tool_python.LanguageTool('en-US') | |
| def grammar_check(self, parsed_resume): | |
| if not parsed_resume: | |
| return {}, 3 # β Return empty dictionary and high grammar score | |
| section_wise_issues = {} | |
| total_grammar_errors = 0 | |
| total_words = 0 | |
| grammar_score = 0 | |
| ignore_keywords = {"imarticus", "github", "linkedin", "microsoft office", "google drive", "amazon web services"} | |
| ignore_rule_ids = {"WHITESPACE_RULE", "EN_QUOTES", "CONSECUTIVE_SPACES", "UPPERCASE_SENTENCE_START"} # β Ignore false uppercase errors | |
| ignore_patterns = [ | |
| r'\d+', # Ignore standalone numbers | |
| r'https?://\S+', # Ignore URLs | |
| r'\S+@\S+', # Ignore emails | |
| r'^\s*[-β’]+\s*', # β Ignore bullet points | |
| ] | |
| name_to_exclude = parsed_resume.get("name", "").strip().lower() | |
| for section, text in parsed_resume.items(): | |
| if isinstance(text, str) and text.strip(): | |
| words_in_section = len(text.split()) | |
| total_words += words_in_section | |
| matches = self.tool.check(text) | |
| grammar_issues = [] | |
| spelling_errors = [] | |
| for match in matches: | |
| context_text = match.context.lower() | |
| # β Ignore rule-based exclusions | |
| if match.ruleId in ignore_rule_ids or any(keyword in context_text for keyword in ignore_keywords): | |
| continue | |
| if name_to_exclude and name_to_exclude in context_text: | |
| continue | |
| if any(re.search(pattern, match.context) for pattern in ignore_patterns): | |
| continue | |
| # β Ignore false uppercase errors caused by bullet points | |
| if "sentence does not start with an uppercase letter" in match.message.lower() and re.search(r'^\s*[-β’]+\s*', match.context): | |
| continue | |
| issue = { | |
| "context": match.context, | |
| "error": match.message, | |
| "rule_id": match.ruleId, | |
| "suggested_correction": match.replacements | |
| } | |
| # β Separate grammar and spelling errors | |
| if "SPELL" in issue.get("rule_id", ""): | |
| spelling_errors.append(issue) | |
| else: | |
| grammar_issues.append(issue) | |
| total_grammar_errors += 1 | |
| if grammar_issues or spelling_errors: | |
| section_wise_issues[section] = { | |
| "grammar_issues": grammar_issues, | |
| "spelling_errors": spelling_errors | |
| } | |
| # β Calculate mistake percentage | |
| mistake_percentage = (total_grammar_errors / total_words) * 100 if total_words > 0 else 0 | |
| # β Apply scoring logic from Excel | |
| if total_grammar_errors == 0: | |
| grammar_score = 3 | |
| elif mistake_percentage < 50: | |
| grammar_score = 2 | |
| else: | |
| grammar_score = 1 | |
| return section_wise_issues, grammar_score # β Always returns 2 values | |