Corin1998 commited on
Commit
5e16504
·
verified ·
1 Parent(s): 7b71759

Upload 4 files

Browse files
Files changed (4) hide show
  1. es_processor_main.py +162 -0
  2. model_initializer.py +57 -0
  3. star_checker.py +52 -0
  4. tone_analyzer.py +29 -0
es_processor_main.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ # 外部モジュールのインポートをシミュレート (実際には適切な方法でインポートが必要)
3
+ # from model_initializer import initialize_tone_classifier, initialize_star_classifier
4
+ # from tone_analyzer import analyze_tone
5
+ # from star_checker import check_star_suitability
6
+
7
+ # NOTE: 実行環境の制約上、ここでは前のファイルの内容を直接関数として再定義し、
8
+ # 外部モジュールとして動作していることをシミュレーションします。
9
+
10
+ # --- 1. model_initializer.py からの関数 (ここでは再定義) ---
11
+ import torch
12
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
13
+
14
+ def initialize_tone_classifier():
15
+ TONE_MODEL_NAME = "cl-tohoku/bert-base-japanese-whole-word-masking"
16
+ try:
17
+ tone_classifier = pipeline(
18
+ "sentiment-analysis",
19
+ model="cl-tohoku/bert-base-japanese-whole-word-masking",
20
+ tokenizer="cl-tohoku/bert-base-japanese-whole-word-masking"
21
+ )
22
+ return tone_classifier
23
+ except Exception:
24
+ classifier = pipeline(
25
+ "text-classification",
26
+ model=AutoModelForSequenceClassification.from_pretrained(TONE_MODEL_NAME),
27
+ tokenizer=AutoTokenizer.from_pretrained(TONE_MODEL_NAME),
28
+ id2label={0: "Negative", 1: "Positive"}
29
+ )
30
+ return classifier
31
+
32
+ def initialize_star_classifier():
33
+ try:
34
+ star_classifier = pipeline(
35
+ "zero-shot-classification",
36
+ model="izumi-lab/bert-base-japanese-v2",
37
+ tokenizer="izumi-lab/bert-base-japanese-v2",
38
+ device=0 if torch.cuda.is_available() else -1
39
+ )
40
+ return star_classifier
41
+ except Exception:
42
+ star_classifier = pipeline(
43
+ "zero-shot-classification",
44
+ model="cl-tohoku/bert-base-japanese-whole-word-masking",
45
+ tokenizer="cl-tohoku/bert-base-japanese-whole-word-masking",
46
+ device=0 if torch.cuda.is_available() else -1
47
+ )
48
+ return star_classifier
49
+
50
+ # --- 2. tone_analyzer.py からの関数 (ここでは再定義) ---
51
+ def analyze_tone(text, tone_classifier):
52
+ results = tone_classifier(text)
53
+ result = results[0]
54
+ sentiment_label = result['label']
55
+ sentiment_score = result['score']
56
+ enthusiasm_score = round(sentiment_score * 100, 2)
57
+ return {
58
+ "label": sentiment_label,
59
+ "raw_score": sentiment_score,
60
+ "enthusiasm_score": enthusiasm_score
61
+ }
62
+
63
+ # --- 3. star_checker.py からの関数 (ここでは再定義) ---
64
+ def check_star_suitability(self_pr_text, star_classifier):
65
+ candidate_labels = ["状況 (Situation)", "課題 (Task)", "行動 (Action)", "結果 (Result)"]
66
+ star_results = star_classifier(
67
+ self_pr_text,
68
+ candidate_labels,
69
+ multi_label=True
70
+ )
71
+ star_scores = {label: round(score * 100, 2) for label, score in zip(star_results['labels'], star_results['scores'])}
72
+
73
+ insufficiency_threshold = 70.0
74
+ missing_elements = []
75
+ total_score = 0
76
+ for label, score in star_scores.items():
77
+ total_score += score
78
+ if score < insufficiency_threshold:
79
+ missing_elements.append(label)
80
+
81
+ suitability_score = round(total_score / len(candidate_labels), 2)
82
+
83
+ analysis_reason = {
84
+ "suitability_score": suitability_score,
85
+ "is_logical": suitability_score >= 75.0,
86
+ "reason_code": "LOGICAL_STRUCTURE_GOOD" if not missing_elements else "ELEMENTS_MISSING",
87
+ "missing_elements": missing_elements,
88
+ "detail_scores": star_scores
89
+ }
90
+
91
+ return {
92
+ "suitability_score": suitability_score,
93
+ "analysis_json": analysis_reason
94
+ }
95
+ # --- ここまでシミュレーションのための再定義 ---
96
+
97
+
98
+ def process_es_data(es_text, self_pr_text, tone_classifier, star_classifier):
99
+ """
100
+ ES全体のデータを受け取り、感情・トーン分析とSTAR適合度チェックを実行するメイン関数。
101
+ """
102
+ print("=========================================")
103
+ print("🚀 新卒ES 自動評価パイプライン開始")
104
+ print("=========================================")
105
+
106
+ # 1. 感情・トーン分析
107
+ print("\n--- 1. 感情・トーン分析の実行 ---")
108
+ tone_analysis = analyze_tone(es_text, tone_classifier)
109
+ print(f"熱意・トーン分析結果 (ポジティブ度): {tone_analysis['enthusiasm_score']} %")
110
+ print(f"判定ラベル: {tone_analysis['label']}")
111
+
112
+ # 2. STAR法適合度チェック
113
+ print("\n--- 2. STAR法適合度チェックの実行 ---")
114
+ star_analysis = check_star_suitability(self_pr_text, star_classifier)
115
+ print(f"STAR法 総合適合度スコア: {star_analysis['suitability_score']} %")
116
+ print(f"論理構造分析JSON (不足要素など):")
117
+ print(json.dumps(star_analysis['analysis_json'], indent=4, ensure_ascii=False))
118
+
119
+ # 最終スコアの統合(例: 感情スコア 50%, STARスコア 50% の重み付け)
120
+ final_potential_score = (tone_analysis['enthusiasm_score'] * 0.5) + (star_analysis['suitability_score'] * 0.5)
121
+
122
+ print("\n=========================================")
123
+ print(f"✨ 最終ポテンシャル評価スコア (統合): {round(final_potential_score, 2)} / 100")
124
+ print("=========================================")
125
+
126
+ return {
127
+ "final_score": round(final_potential_score, 2),
128
+ "tone_analysis": tone_analysis,
129
+ "star_analysis": star_analysis
130
+ }
131
+
132
+
133
+ if __name__ == '__main__':
134
+ # 1. モデルの初期化
135
+ print("Hugging Face パイプラインの初期化を開始します...")
136
+ tc = initialize_tone_classifier()
137
+ sc = initialize_star_classifier()
138
+
139
+ # 2. 実行例
140
+ # 実際のESテキスト(全体)
141
+ es_full_text = """
142
+ 貴社が推進する「グローバル×ローカル」のデジタル変革戦略に深く共感し、志望いたしました。私は大学時代、学園祭の実行委員長として、従来の集客方法に課題を感じ、SNSを活用した新しいプロモーション戦略を立案・実行しました。これにより、来場者数を前年比150%に増加させるという明確な結果を出しました。この経験から学んだ、現状を打破するための課題設定力と、関係者を巻き込む実行力を貴社で活かし、世界中の顧客に感動を届けるシステムを構築したいと強く願っています。
143
+ """
144
+
145
+ # STAR法チェックの対象となる自己PRセクション
146
+ self_pr_text_star_focus = """
147
+ 私は大学時代、学園祭の実行委員長として、従来の集客方法に課題を感じ、SNSを活用した新しいプロモーション戦略を立案・実行しました。これにより、来場者数を前年比150%に増加させるという明確な結果を出しました。課題はSNSチームのメンバーのモチベーション管理でしたが、個別の面談を通じて主体的な関与を引き出し、最終的に全員が目標達成に貢献しました。
148
+ """
149
+
150
+ # ESデータの処理を実行
151
+ results = process_es_data(es_full_text, self_pr_text_star_focus, tc, sc)
152
+
153
+ # 例: 不足要素がある場合のシミュレーション (結果が弱いES)
154
+ print("\n\n--- 不足要素がある場合のシミュレーション (結果が弱いES) ---")
155
+ weak_result_es = """
156
+ 私はチームプロジェクトでリーダーシップを発揮しました(Action)。当初、目標達成が難しい状況(Situation)でしたが、諦めずにメンバーと協力し、最後までやり遂げました(Task)。結果、多くのことを学び、成長できました。
157
+ """
158
+
159
+ weak_results = check_star_suitability(weak_result_es, sc)
160
+ print("STAR法 総合適合度スコア:", weak_results['suitability_score'], "%")
161
+ print("論理構造分析JSON (結果が弱いES):")
162
+ print(json.dumps(weak_results['analysis_json'], indent=4, ensure_ascii=False))
model_initializer.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
+
4
+ def initialize_tone_classifier():
5
+ """
6
+ 感情・トーン分析用のHugging Faceパイプラインを初期化する。
7
+ """
8
+ TONE_MODEL_NAME = "cl-tone/bert-base-japanese-whole-word-masking"
9
+ try:
10
+ # 感情・トーン分析パイプライン(二値分類をシミュレート)
11
+ tone_classifier = pipeline(
12
+ "sentiment-analysis",
13
+ model = "cl-tohoku/bert-base-japanese-whole-word-masking", # 適切な感情分析モデルに置き換える
14
+ tokenizer = "cl-tohoku/bert-base-japanese-whole-word-masking"
15
+ )
16
+ print("感情・トーン分析パイプラインをロードしました。")
17
+ return tone_classifier
18
+ except Exception as e:
19
+ print(f"感情分析モデルのロードに失敗しました。デフォルトのテキスト分類器を使用します:{e}")
20
+ # 代替として、凡庸的な分類モデルを使用
21
+ classifier = pipeline(
22
+ "text-classification",
23
+ model=AutoModelForSequenceClassification.from_pretrained(TONE_MODEL_NAME),
24
+ tokenizer=AutoTokenizer.from_pretrained(TONE_MODEL_NAME),
25
+ id2label={0: "NEGATIVE", 1: "POSITIVE"}
26
+ )
27
+ print("汎用テキスト分類器を代替して使用します。")
28
+ return classifier
29
+
30
+ def initialize_star_classifier():
31
+ """
32
+ STAR法適用度チェック用のZero-Shot Classificationパイプラインを初期化する。
33
+ """
34
+ try:
35
+ star_classifier = pipeline(
36
+ "zero-shot-classification",
37
+ model="izumi-lab/bert-base-japanese-v2", # Zero-Shot Classificationにより適した日本語モデル
38
+ tokenizer= "izumi-lab/bert-base-japanese-v2",
39
+ device=0 if torch.cuda.is_available() else -1 # GPUがあれば使用
40
+ )
41
+ print("STAR法適用度チェックパイプライン(Zero-Shot)をロードしました。")
42
+ return star_classifier
43
+ except Exception as e:
44
+ print(f"Zero-Shot Classigicationモデルのロードに失敗しました。汎用モデルを使用します:{e}")
45
+ star_classifier = pipeline(
46
+ "zero-shot-classification",
47
+ model="cl-tohoku/bert^base^japanese-whole-word-masking",
48
+ tokenizer="cl-tohoku/bert-base-japanese-whole-word-masking",
49
+ device=0 if torch.cuda.is_available() else -1
50
+ )
51
+ print("汎用Zero-Shot Classificationモデルを代替して使用します。")
52
+ return star_classifier
53
+
54
+ __name__ = "__main__":
55
+ # モジュールの動作確認
56
+ initialize_tone_classifier()
57
+ initialize_star_classifier()
star_checker.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ def check_star_sitabillity(self_pr_text, str_classifier):
4
+ """
5
+ 自己PRテキストがSTAR法(Situation, Task, Action, Result)に適合しているかをチェックする。
6
+ Args:
7
+ self_pr_text (str): 自己PRセクションのテキスト。
8
+ str_classifier (function): Hugging faceのZero-shot Classificationパイプライン。
9
+ Returns:
10
+ dict: STAR適合度チェックの結果(適合度スコアとJSPM形式の理由)。
11
+ """
12
+ # STAR法の各要素に対応する Zero-Shot Classification の候補ラベル
13
+ candidate_labels = ["状況(Situation)", "課題(Task)", "行動(Action)", "結果(Result)"]
14
+
15
+ # Zero-Shot Classification の実行
16
+ star_results = str_classifier(
17
+ self_pr_text,
18
+ candidate_labels,
19
+ multi_label=True # 複数の要素が同時に含まれる可能性があるため
20
+ )
21
+
22
+ #結果の処理
23
+ star_scores = {label: round(score*100, 2) for label, score in zip(star_results['labels'], star_results['scores'])}
24
+
25
+ # 適合度と算出の理由の特定
26
+ # 基準として、スコアが70%未満の要素を「不足」とみなす
27
+ insufficiency_threshold = 70.0
28
+ missing_elements = []
29
+
30
+ total_score = 0
31
+ for label, score in star_scores.items():
32
+ total_score += score
33
+ if score < insufficiency_threshold:
34
+ missing_elements.append(label)
35
+
36
+ #総合適合度スコア(4要素の平均スコア)
37
+ suitability_score = round(total_score / len(candidate_labels), 2)
38
+
39
+ #論理的でない場合の理由をJSONライクな形で出力
40
+ analysis_reason = {
41
+ "suitabilitiy_score": suitability_score,
42
+ "is_logical":suitability_score >= 75.0, # 例: 平均75点以上で論理的と判断
43
+ "reason_code":"LOGICAL_STRUCTURE_GOOD" if not missing_elements else "ELEMENTS_MISSING",
44
+ "missing_elements": missing_elements,
45
+ "detail_scores": star_scores
46
+ }
47
+
48
+ # 結果を返す
49
+ return {
50
+ "suitability_score": suitability_score,
51
+ "analysis_json": analysis_reason
52
+ }
tone_analyzer.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def analyze_tone(text, tone_classifier):
2
+ """
3
+ ESテキストの感情・トーンを分析し、ポジティンブ度と熱意スコアを算出する。
4
+
5
+ Args:
6
+ text (str): 志望動機や自己PRのテキスト。
7
+ tone_classifier (callable): トーン分類器。テキストを入力として受け取り、トーンスコアを返す関数。
8
+
9
+ Returns:
10
+ dict: トーン分析結果(スコアとラベル)。
11
+ """
12
+ # 感情分析を実行
13
+ # ラベルが'positive'のスコアを熱意スコアとして抽出
14
+ results = tone_classifier(text)
15
+
16
+ # 結果の整形
17
+ result = results[0]
18
+ sentiment_label = result['label']
19
+ sentiment_score = result['score']
20
+
21
+ # ポジティブ度/熱意スコア(0.0~100.0に換算)
22
+ enthusiasm_score = round(sentiment_score *100,2)
23
+
24
+ # 結果を返す
25
+ return {
26
+ "label": sentiment_label,
27
+ "raw_score": sentiment_score,
28
+ "enthusiasm_score": enthusiasm_score
29
+ }