133 lines
No EOL
4 KiB
Python
133 lines
No EOL
4 KiB
Python
"""
|
|
Test-Anwendung für den Daten-Neutralisierer
|
|
Demonstriert die Funktionalität mit verschiedenen Testdaten
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
# Add the parent directory to the Python path
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
|
|
# Import directly from the package
|
|
from test_neutralizer import DataAnonymizer, ChatDataProcessor, TEST_DATA
|
|
|
|
def test_text_anonymization():
|
|
"""Testet die Text-Anonymisierung für verschiedene Sprachen"""
|
|
anonymizer = DataAnonymizer()
|
|
|
|
for language in ["de", "ch", "fr", "it"]:
|
|
print(f"\n{'='*20} Test für Sprache: {language} {'='*20}")
|
|
|
|
# Text anonymisieren
|
|
anonymized_text, mapping = anonymizer.anonymize_text(TEST_DATA[language], language)
|
|
|
|
print("\nOriginaler Text:")
|
|
print(TEST_DATA[language])
|
|
|
|
print("\nAnonymisierter Text:")
|
|
print(anonymized_text)
|
|
|
|
print("\nMapping:")
|
|
for placeholder, original in mapping.items():
|
|
print(f"{placeholder} -> {original}")
|
|
|
|
# Test der Deanonymisierung
|
|
ai_response = f"Basierend auf dem Text können wir sehen, dass NAME_1 an EMAIL_1 geschrieben hat."
|
|
restored_response = anonymizer.deanonymize_text(ai_response, mapping)
|
|
|
|
print("\nKI-Antwort (anonymisiert):")
|
|
print(ai_response)
|
|
|
|
print("\nKI-Antwort (deanonymisiert):")
|
|
print(restored_response)
|
|
|
|
def test_file_processing():
|
|
"""Testet die Verarbeitung verschiedener Dateiformate"""
|
|
anonymizer = DataAnonymizer()
|
|
|
|
# Temporäre Testdateien erstellen
|
|
test_dir = Path("test_files")
|
|
test_dir.mkdir(exist_ok=True)
|
|
|
|
# JSON-Datei erstellen
|
|
json_path = test_dir / "test.json"
|
|
with open(json_path, "w", encoding="utf-8") as f:
|
|
json.dump(TEST_DATA["json"], f, indent=2)
|
|
|
|
# CSV-Datei erstellen
|
|
csv_path = test_dir / "test.csv"
|
|
with open(csv_path, "w", encoding="utf-8") as f:
|
|
f.write(TEST_DATA["csv"])
|
|
|
|
# Dateien verarbeiten
|
|
print("\n" + "="*20 + " JSON-Verarbeitung " + "="*20)
|
|
json_result = anonymizer.process_file(str(json_path), "de")
|
|
print("\nAnonymisierte JSON-Daten:")
|
|
print(json.dumps(json_result["anonymized_content"], indent=2))
|
|
|
|
print("\n" + "="*20 + " CSV-Verarbeitung " + "="*20)
|
|
csv_result = anonymizer.process_file(str(csv_path), "de")
|
|
print("\nAnonymisierte CSV-Daten:")
|
|
for row in csv_result["anonymized_content"]:
|
|
print(row)
|
|
|
|
# Aufräumen
|
|
json_path.unlink()
|
|
csv_path.unlink()
|
|
test_dir.rmdir()
|
|
|
|
def test_chat_processor():
|
|
"""Testet die Chat-Integration"""
|
|
processor = ChatDataProcessor()
|
|
|
|
# Temporäre Testdatei erstellen
|
|
test_dir = Path("test_files")
|
|
test_dir.mkdir(exist_ok=True)
|
|
|
|
txt_path = test_dir / "test.txt"
|
|
with open(txt_path, "w", encoding="utf-8") as f:
|
|
f.write(TEST_DATA["de"])
|
|
|
|
# Chat-Nachricht verarbeiten
|
|
print("\n" + "="*20 + " Chat-Verarbeitung " + "="*20)
|
|
result = processor.process_chat_message("user123", str(txt_path), "de")
|
|
|
|
print("\nSession-ID:", result["session_id"])
|
|
print("\nAnonymisierte Daten:")
|
|
print(result["content"])
|
|
|
|
# KI-Antwort verarbeiten
|
|
ai_response = "Basierend auf den Daten können wir sehen, dass NAME_1 an EMAIL_1 geschrieben hat."
|
|
restored = processor.restore_ai_response(result["session_id"], ai_response)
|
|
|
|
print("\nKI-Antwort (anonymisiert):")
|
|
print(ai_response)
|
|
|
|
print("\nKI-Antwort (deanonymisiert):")
|
|
print(restored["restored_response"])
|
|
|
|
# Aufräumen
|
|
txt_path.unlink()
|
|
test_dir.rmdir()
|
|
processor.cleanup_session(result["session_id"])
|
|
|
|
def main():
|
|
"""Hauptfunktion für die Test-Ausführung"""
|
|
print("Starte Tests für den Daten-Neutralisierer...")
|
|
|
|
# Text-Anonymisierung testen
|
|
test_text_anonymization()
|
|
|
|
# Dateiverarbeitung testen
|
|
test_file_processing()
|
|
|
|
# Chat-Integration testen
|
|
test_chat_processor()
|
|
|
|
print("\nAlle Tests abgeschlossen!")
|
|
|
|
if __name__ == "__main__":
|
|
main() |