#!/usr/bin/env python3 """简历打分脚本 v2 - 按 rubric 规则逐份打分,支持淘汰项 用法: python3 score_resumes.py --rubric data/scoring_rubric_v2.json --db data/resumes.db --output data/scoring_result_v2.jsonl python3 score_resumes.py --rubric data/scoring_rubric_v2.json --db data/resumes.db --only-unscored """ import sqlite3, json, argparse, os, sys from datetime import datetime def load_rubric(path): with open(path, 'r', encoding='utf-8') as f: return json.load(f) def match_score(value, rules): """根据 rules 字典匹配分数,5>4>3>2>1""" if not value: return 1, "信息不足" value_lower = value.lower().strip() for score in ['5', '4', '3', '2', '1']: keywords = rules.get(score, []) for kw in keywords: if kw.lower() in value_lower: return int(score), f"匹配: {kw}" # 默认 return 2, "未明确匹配" def compute_grad_year(experiences, resume): """估算毕业年份。优先级:workAge 明确工作年限≥value 时用工作年限推算(避免成人教育/第二学历 endDate 误杀); 否则用教育经历 endDate 最大年份;再 fallback 应届生标记。返回 None 表示无法判断(不淘汰,宁漏勿杀)。""" import re now_year = datetime.now().year wa = str(resume.get('workAge', '')) # workAge 明确数字年限(如 "3年"、"8年";"1年以内"/"应届生" 不算) m = re.match(r'^(\d+)年$', wa) work_years = int(m.group(1)) if m else None m4 = re.search(r'(20\d{2})年应届生', wa) # 教育经历 endDate 最大年份 edu = [e for e in experiences if e.get('type') == 2] years = [] for e in edu: ed = str(e.get('endDate', '') or '') mm = re.search(r'(20\d{2})', ed) if mm and '至今' not in ed: years.append(int(mm.group(1))) grad_edu = max(years) if years else None # 规则:workAge ≥2 年 → 信任工作年限(毕业至少 work_years 年前) if work_years is not None and work_years >= 2: return now_year - work_years # 应届生带年份("25年应届生")→ 直接用 if m4: return int(m4.group(1)) # 无年份应届生 → 视为去年毕业 if '应届生' in wa: return now_year - 1 # 其他情况用教育 endDate return grad_edu _JOB_PHRASE_RE = None def clean_chat_text(chat_text): """移除候选人复述岗位名/打招呼产生的噪声词("看到贵司招聘AI短剧制作岗位"这类)。 保留第一人称经验信号(我做过/独立制作/作品集/链接/AI工具名)。""" global _JOB_PHRASE_RE import re if _JOB_PHRASE_RE is None: words = ["AI短剧制作", "漫剧制作", "短剧制作", "AI短剧", "短剧", "漫剧", "全流程制作", "全流程", "招聘", "岗位", "职位", "贵司", "贵公司", "贵方", "应聘", "看到", "期待回复", "进一步沟通", "可以聊聊"] _JOB_PHRASE_RE = re.compile('|'.join(re.escape(w) for w in words)) return _JOB_PHRASE_RE.sub(' ', chat_text) def check_elimination(resume, rubric): """检查淘汰规则,返回 (eliminated: bool, reason: str)""" elim_rules = rubric.get('elimination_rules', []) if not elim_rules: return False, None # 准备各字段值 gender = resume.get('gender', '') experiences = json.loads(resume['experiences']) if isinstance(resume.get('experiences', ''), str) else resume.get('experiences', []) edu_exps = [e for e in experiences if e.get('type') == 2] edu_occups = [e.get('occupation', '') for e in edu_exps if e.get('occupation')] edu_occup_str = ' '.join(edu_occups) second_text = resume.get('secondText', '') # 专业字段 = 教育经历occupation + secondText major_text = edu_occup_str + ' ' + second_text for rule in elim_rules: name = rule.get('name', '') field = rule.get('field', '') rule_type = rule.get('rule', '') value = rule.get('value', '') keywords = rule.get('keywords', []) reason = rule.get('reason', '') field_value = '' if field == 'gender': field_value = gender or '' elif field == 'experiences_education_occupation': field_value = major_text or '' elif field == 'graduation': # 毕业年限淘汰: rule='within_years', value=N → 毕业不足 N 年淘汰 grad_year = compute_grad_year(experiences, resume) if grad_year is None: continue # 无法判断 → 不淘汰 try: limit = datetime.now().year - int(value) except (TypeError, ValueError): continue if grad_year > limit: return True, reason continue else: # 通用字段读取 field_value = str(resume.get(field, '')) if rule_type == 'equals': if field_value == value: return True, reason elif rule_type == 'not_contains_any': # field_value 里不包含任何一个 keyword → 淘汰 field_lower = field_value.lower() found = False for kw in keywords: if kw.lower() in field_lower: found = True break if not found: return True, reason elif rule_type == 'contains_any': field_lower = field_value.lower() for kw in keywords: if kw.lower() in field_lower: return True, reason return False, None def score_resume(resume, rubric): """对一份简历打分""" experiences = json.loads(resume['experiences']) if isinstance(resume['experiences'], str) else resume['experiences'] # 教育经历 edu_exps = [e for e in experiences if e.get('type') == 2] work_exps = [e for e in experiences if e.get('type') == 1] # 专业:取教育经历中的 occupation edu_occups = [e.get('occupation', '') for e in edu_exps if e.get('occupation')] edu_occup_str = ' '.join(edu_occups) # 学校:取教育经历中的 organization edu_orgs = [e.get('organization', '') for e in edu_exps if e.get('organization')] edu_org_str = ' '.join(edu_orgs) # 工作经历 occupation work_occups = [e.get('occupation', '') for e in work_exps if e.get('occupation')] work_ocup_str = ' '.join(work_occups) # 也看 secondText 里的专业和学校信息 second_text = resume.get('secondText', '') # 聊天文本(候选人主动发的作品链接/作品描述等,BOSS 简历 BLOB 里没有) chat_text = clean_chat_text(resume.get('chat_text', '') or '') dimension_scores = {} total_weighted = 0 total_weight = 0 for criterion in rubric.get('criteria', []): name = criterion['name'] weight = criterion['weight'] rules = criterion.get('rules', {}) if name == '专业匹配度': score, reason = match_score(edu_occup_str + ' ' + second_text, rules) elif name == '学校层次': score, reason = match_score(edu_org_str + ' ' + second_text, rules) elif name == '写作创作经验': # 工作 occupation + 聊天文本(聊天里常有"独立制作/导演/剪辑"等自述) score, reason = match_score(work_ocup_str + ' ' + chat_text, rules) if (work_ocup_str or chat_text) else (1, "无工作经历") elif name == '到岗状态': apply_status = resume.get('applyStatus', '') score, reason = match_score(apply_status, rules) elif name == 'AI相关经验': ai_text = work_ocup_str + ' ' + edu_occup_str + ' ' + chat_text score, reason = match_score(ai_text, rules) elif name == '作品与市场验证': # 聊天文本 + 工作 occupation:作品链接/独立制作/全流程/市场验证 src = chat_text + ' ' + work_ocup_str score, reason = match_score(src, rules) if src.strip() else (1, "无作品信息") elif name == '美术功底': # 专业 + 聊天文本:美术/绘画/手绘/插画/设计等 src = edu_occup_str + ' ' + second_text + ' ' + chat_text score, reason = match_score(src, rules) elif name == '毕业年限': # 加分项:毕业≥2年5分,1-2年3分,应届1分(数值逻辑,不走关键词) gy = compute_grad_year(experiences, resume) if gy is None: score, reason = 3, "毕业年限无法判断" else: yrs = datetime.now().year - gy if yrs >= 2: score, reason = 5, f"毕业{yrs}年" elif yrs >= 1: score, reason = 3, f"毕业{yrs}年" else: score, reason = 1, f"毕业{yrs}年(应届)" else: score, reason = 1, "未知维度" dimension_scores[name] = {"score": score, "reason": reason, "weight": weight} total_weighted += score * weight total_weight += weight raw_score = total_weighted / total_weight if total_weight > 0 else 1 # 1-5 映射到 0-100: (score - 1) / 4 * 100 total_score = round((raw_score - 1) / 4 * 100) # 分档 thresholds = rubric.get('tier_thresholds', {"recommend": 75, "backup": 55}) if total_score >= thresholds['recommend']: tier = "推荐面试" elif total_score >= thresholds['backup']: tier = "可备选" else: tier = "暂不考虑" # 生成摘要 edu_summary = second_text.replace('毕业于 ', '') if second_text else '未知' work_summary = work_occups[0] if work_occups else '无工作经历' result = { "friendId": str(resume['friendId']), "name": resume['name'], "total_score": total_score, "tier": tier, "eliminated": 0, "eliminated_reason": None, "dimension_scores": dimension_scores, "education": resume.get('education', ''), "gender": resume.get('gender', ''), "applyStatus": resume.get('applyStatus', ''), "city": resume.get('city', ''), "age": resume.get('age', ''), "workAge": resume.get('workAge', ''), "secondText": second_text, "work_summary": work_summary, "edu_summary": edu_summary, "rubric_version": rubric.get('version', 1), "scored_at": datetime.now().strftime('%Y-%m-%d %H:%M:%S') } return result def score_resume_with_elimination(resume, rubric): """先检查淘汰,再打分""" eliminated, reason = check_elimination(resume, rubric) if eliminated: # 淘汰:0分,直接标记 return { "friendId": str(resume['friendId']), "name": resume['name'], "total_score": 0, "tier": "已淘汰", "eliminated": 1, "eliminated_reason": reason, "dimension_scores": {}, "education": resume.get('education', ''), "gender": resume.get('gender', ''), "applyStatus": resume.get('applyStatus', ''), "city": resume.get('city', ''), "age": resume.get('age', ''), "workAge": resume.get('workAge', ''), "secondText": resume.get('secondText', ''), "work_summary": "", "edu_summary": resume.get('secondText', '').replace('毕业于 ', '') if resume.get('secondText') else '未知', "rubric_version": rubric.get('version', 1), "scored_at": datetime.now().strftime('%Y-%m-%d %H:%M:%S') } return score_resume(resume, rubric) def save_to_db(db, result, resume_id): """把打分结果写入 scores 表""" dim_json = json.dumps(result.get('dimension_scores', {}), ensure_ascii=False) version = result.get('rubric_version', 1) # 先删除旧版本记录(如果存在) db.execute("DELETE FROM scores WHERE resume_id=? AND rubric_version=?", (resume_id, version)) sql = "INSERT INTO scores (resume_id, friendId, rubric_version, total_score, tier, dimension_scores, scored_at, eliminated, eliminated_reason) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" vals = [ resume_id, result['friendId'], version, result['total_score'], result['tier'], dim_json, result['scored_at'], result.get('eliminated', 0), result.get('eliminated_reason'), ] assert sql.count('?') == len(vals), f"placeholder mismatch: {sql.count('?')} vs {len(vals)}" db.execute(sql, vals) def main(): parser = argparse.ArgumentParser(description='简历打分 v2') parser.add_argument('--rubric', required=True, help='rubric JSON 文件路径') parser.add_argument('--db', default='/sdcard/bosszhipin/data/resumes.db', help='简历数据库路径') parser.add_argument('--output', default=None, help='输出 JSONL 路径') parser.add_argument('--only-unscored', action='store_true', help='只打分未打分的简历') parser.add_argument('--job-id', default=None, help='只打分指定 jobId 的岗位简历(不传则全库)') args = parser.parse_args() rubric = load_rubric(args.rubric) # 默认输出路径 if not args.output: version = rubric.get('version', 1) args.output = f'/sdcard/bosszhipin/data/scoring_result_v{version}.jsonl' db = sqlite3.connect(args.db) db.row_factory = sqlite3.Row # 取简历:only-unscored 模式下只取未打分的 if args.only_unscored: rubric_version = rubric.get('version', 1) if args.job_id: rows = db.execute(''' SELECT r.* FROM resumes r LEFT JOIN scores s ON r.id = s.resume_id AND s.rubric_version = ? WHERE s.resume_id IS NULL AND r.jobId = ? ''', (rubric_version, args.job_id)).fetchall() else: rows = db.execute(''' SELECT r.* FROM resumes r LEFT JOIN scores s ON r.id = s.resume_id AND s.rubric_version = ? WHERE s.resume_id IS NULL ''', (rubric_version,)).fetchall() print(f'--only-unscored: {len(rows)} 份新简历待打分(rubric v{rubric_version}' + (f',jobId={args.job_id}' if args.job_id else ',全库') + ')') else: if args.job_id: rows = db.execute('SELECT * FROM resumes WHERE jobId = ?', (args.job_id,)).fetchall() else: rows = db.execute('SELECT * FROM resumes').fetchall() if not rows: print('没有待打分的简历,跳过') db.close() return results = [] count = 0 eliminated_count = 0 for row in rows: resume = dict(row) result = score_resume_with_elimination(resume, rubric) if result['eliminated']: eliminated_count += 1 results.append(result) count += 1 # 写入 scores 表 save_to_db(db, result, row['id']) db.commit() # 排序:非淘汰在前(按分数降序),淘汰在后 results.sort(key=lambda x: (x['eliminated'], -x['total_score'])) # 写入 JSONL with open(args.output, 'w', encoding='utf-8') as f: for r in results: f.write(json.dumps(r, ensure_ascii=False) + '\n') # 统计 print(f'打分完成: {count} 份 (其中淘汰 {eliminated_count} 份)') print(f'输出: {args.output}') print() tiers = {} for r in results: tier = r['tier'] tiers[tier] = tiers.get(tier, 0) + 1 print('=== 分档统计 ===') for tier in ['已淘汰', '推荐面试', '可备选', '暂不考虑']: print(f' {tier}: {tiers.get(tier, 0)}人') # 淘汰原因分布 if eliminated_count > 0: print() print('=== 淘汰原因分布 ===') reasons = {} for r in results: if r['eliminated']: reason = r['eliminated_reason'] or '未知' reasons[reason] = reasons.get(reason, 0) + 1 for reason, cnt in sorted(reasons.items(), key=lambda x: -x[1]): print(f' {reason}: {cnt}人') print() print('=== Top 10 (非淘汰) ===') non_elim = [r for r in results if not r['eliminated']] for i, r in enumerate(non_elim[:10]): print(f' #{i+1} | {r["total_score"]}分 | {r["name"]} | {r["gender"]} | {r["tier"]} | {r["edu_summary"]} | {r["work_summary"]}') print() print('=== 淘汰样本 (前5) ===') elim = [r for r in results if r['eliminated']] for r in elim[:5]: print(f' {r["total_score"]}分 | {r["name"]} | {r["gender"]} | 淘汰原因: {r["eliminated_reason"]} | {r["edu_summary"]}') db.close() if __name__ == '__main__': main()