-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze_transactional.py
More file actions
executable file
·236 lines (194 loc) · 8.01 KB
/
analyze_transactional.py
File metadata and controls
executable file
·236 lines (194 loc) · 8.01 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
#!/usr/bin/env python3
#
# Jan 2026
#
# Analyse van preken aan de hand van Transactionele Analyse (Eric Berne)
# Focus op Ego-posities, Transacties, Games en de Dramadriehoek
#
#####################################################
import os
import json
import datetime
import argparse
import google.generativeai as genai
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def count_words(text):
"""
Count the actual number of words in the sermon text.
Returns the word count.
"""
words = text.split()
return len(words)
# Configuration
API_KEY = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
DEFAULT_INPUT_FILE = "input/preek_01.txt"
PROMPT_FILE = "prompts/analyze_transactional.md"
OUTPUT_DIR = "outputs"
def validate_input(text):
"""
Validates if the sermon text is substantial enough for analysis.
"""
lines = text.split('\n')
non_empty_lines = [line for line in lines if line.strip()]
if len(non_empty_lines) < 50:
raise ValueError(
f"Preektekst te kort voor analyse. "
f"Gevonden: {len(non_empty_lines)} regels. Minimaal vereist: 50 regels."
)
return True
def analyze_sermon_transactional(text, prompt_template, word_count):
"""
Calls Gemini API to analyze the sermon using Transactional Analysis framework.
"""
if not API_KEY:
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
print(f"DEBUG: API Key found. Length: {len(API_KEY)}")
print(f"DEBUG: Key starts with: {API_KEY[:4]}...")
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel('gemini-3.1-pro-preview')
# Calculate estimated duration (100 words per minute)
estimated_duration = round(word_count / 100)
full_prompt = f"""{prompt_template}
--- BELANGRIJKE METADATA ---
Het exacte aantal woorden in deze preek is: {word_count}
Geschatte duur bij 100 woorden/minuut: {estimated_duration} minuten
Gebruik deze exacte waarden in je metadata sectie:
- "geschatte_woordlengte": {word_count}
- "geschatte_tijdsduur_minuten": {estimated_duration}
--- BEGIN PREEK ---
{text}
--- EINDE PREEK ---"""
print("📡 Versturen naar Gemini API voor Transactionele Analyse...")
print("⚙️ Dit kan enkele seconden duren...")
response = model.generate_content(
full_prompt,
generation_config={
"response_mime_type": "application/json",
"temperature": 0.4,
}
)
return response.text
def save_output(input_filename, json_content, output_dir=OUTPUT_DIR):
"""
Saves the JSON content to the specified output directory.
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
base_name = os.path.splitext(os.path.basename(input_filename))[0]
output_filename = f"{output_dir}/{base_name}_transactional.json"
try:
parsed_json = json.loads(json_content)
with open(output_filename, 'w', encoding='utf-8') as f:
json.dump(parsed_json, f, indent=2, ensure_ascii=False)
print(f"\n✅ Analyse succesvol opgeslagen: {output_filename}")
# Print simple score if available
if "conclusie_en_aanbeveling" in parsed_json:
score = parsed_json["conclusie_en_aanbeveling"].get("psychologische_gezondheid_score", 0)
print(f"🧠 Psychologische Gezondheid Score: {score}/10")
return output_filename
except json.JSONDecodeError as e:
print(f"❌ Error: content returned was not valid JSON: {e}")
raw_filename = f"{output_filename}.raw"
with open(raw_filename, 'w', encoding='utf-8') as f:
f.write(json_content)
print(f"💾 Raw content saved for debugging: {raw_filename}")
return None
def print_summary(output_file):
"""
Prints a brief summary of the TA results.
"""
try:
with open(output_file, 'r', encoding='utf-8') as f:
data = json.load(f)
print("\n" + "="*70)
print("🧠 SAMENVATTING TRANSACTIONELE ANALYSE")
print("="*70)
if "ego_posities_scan" in data:
scan = data["ego_posities_scan"]
print("\n📊 Ego-posities (10 = Gezond/Vrij):")
if "dominante_ego_positie" in scan:
print(f" 🎯 Dominant: {scan['dominante_ego_positie']}")
# Print simple bar charts for key positions if scores exist
# Note: Scores are now "Positive" (10 = Good/Absent of negative trait)
positions = {
"Vrijheid v. Kritische Ouder": scan.get("ouder_parent", {}).get("vrijheid_van_kritische_ouder_CP", {}).get("score", 0),
"Gezonde Zorg (NP)": scan.get("ouder_parent", {}).get("gezonde_zorg_NP", {}).get("score", 0),
"Volwassene (A)": scan.get("volwassene_adult", {}).get("score", 0),
"Vrijheid v. Aangepast Kind": scan.get("kind_child", {}).get("vrijheid_van_aangepast_kind_AC", {}).get("score", 0),
"Vrij Kind (FC)": scan.get("kind_child", {}).get("vrij_kind_FC", {}).get("score", 0)
}
for name, score in positions.items():
if score is not None:
# Visual representation: 10 is good, 0 is bad
try:
score_int = int(score)
bar = "█" * score_int + "░" * (10 - score_int)
print(f" {name:30s} {bar} {score}/10")
except ValueError:
pass
if "spel_analyse_games" in data:
games = data["spel_analyse_games"].get("gedetecteerde_spelen", [])
if games:
print("\n🎭 Gedetecteerde Spelen:")
for game in games:
name = game.get("naam", "Onbekend")
prob = game.get("waarschijnlijkheid", "?")
print(f" - {name} ({prob})")
else:
print("\n🎭 Geen destructieve spelen gedetecteerd.")
if "conclusie_en_aanbeveling" in data:
conc = data["conclusie_en_aanbeveling"]
print(f"\n🧠 Gezondheidsscore: {conc.get('psychologische_gezondheid_score', '?')}/10")
print(f"💡 Advies: {conc.get('advies_voor_game_vrije_communicatie', '')[:100]}...")
print("="*70 + "\n")
except Exception as e:
print(f"⚠️ Kon geen samenvatting genereren: {e}")
def main():
parser = argparse.ArgumentParser(
description="Analyseer een preek aan de hand van Transactionele Analyse (Eric Berne).",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-i", "--input",
type=str,
default=DEFAULT_INPUT_FILE,
help="Pad naar het inputbestand (.txt)"
)
parser.add_argument(
"--output-dir",
type=str,
default=OUTPUT_DIR,
help=f"Output directory voor JSON bestanden (default: {OUTPUT_DIR})"
)
parser.add_argument(
"--no-summary",
action="store_true",
help="Onderdruk de samenvatting in de console"
)
args = parser.parse_args()
input_file = args.input
output_dir = args.output_dir
print("="*70)
print("🧠 TRANSACTIONELE ANALYSE VOOR HOMILETIEK")
print("="*70)
print(f"📖 Input bestand: {input_file}\n")
try:
with open(input_file, 'r', encoding='utf-8') as f:
sermon_text = f.read()
word_count = validate_input(sermon_text)
with open(PROMPT_FILE, 'r', encoding='utf-8') as f:
prompt_template = f.read()
json_response = analyze_sermon_transactional(sermon_text, prompt_template, word_count)
output_file = save_output(input_file, json_response, output_dir)
if output_file and not args.no_summary:
print_summary(output_file)
except FileNotFoundError as e:
print(f"❌ Bestand niet gevonden: {e}")
except ValueError as e:
print(f"❌ Validatie fout: {e}")
except Exception as e:
print(f"❌ Fout: {e}")
if __name__ == "__main__":
main()