Spaces:
Running
on
Zero
Running
on
Zero
import pandas as pd | |
import re | |
from sqlalchemy import create_engine, inspect, Table, MetaData, Column, String, text | |
import psycopg2 | |
# チャット履歴ファイルを読み込む関数(最初の3行をスキップ) | |
def load_chat_history(file_path): | |
return pd.read_csv(file_path, skiprows=3) | |
# 個人情報をマスクする関数 | |
def mask_personal_info(text): | |
# 電話番号のマスク | |
text = re.sub(r'\b\d{2,4}-\d{2,4}-\d{4}\b', '[電話番号]', text) | |
# メールアドレスのマスク | |
text = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', '[メールアドレス]', text) | |
# 名前のマスク(例として"えみいわし"をマスク) | |
names = ['えみいわし'] | |
for name in names: | |
text = re.sub(r'\b' + name + r'\b', '[名前]', text) | |
return text | |
# データフレームの特定の列の個人情報をマスクする関数 | |
def mask_specific_columns(df, columns): | |
for column in columns: | |
df[column] = df[column].astype(str).apply(mask_personal_info) | |
return df | |
# データベースに接続する関数 | |
def connect_to_db(): | |
conn = psycopg2.connect( | |
dbname="neondb", | |
user="miyataken999", | |
password="yz1wPf4KrWTm", | |
host="ep-odd-mode-93794521.us-east-2.aws.neon.tech", | |
port=5432, | |
sslmode="require" | |
) | |
return conn | |
# ファイルパスの設定 | |
input_file_path = '/home/user/app/polls/databases/test.csv' # エクスポートされたチャット履歴ファイルのパス | |
# チャット履歴を読み込み、特定の列の個人情報をマスク | |
chat_history_df = load_chat_history(input_file_path) | |
columns_to_mask = ['送信者タイプ', '送信者名', '送信日', '送信時刻', '内容'] | |
masked_chat_history_df = mask_specific_columns(chat_history_df, columns_to_mask) | |
# データベースの接続設定 | |
engine = create_engine('postgresql+psycopg2://miyataken999:yz1wPf4KrWTm@ep-odd-mode-93794521.us-east-2.aws.neon.tech:5432/neondb') | |
# テーブルが存在しない場合に作成 | |
if not inspect(engine).has_table("fasis_chat_history"): | |
metadata = MetaData() | |
Table('fasis_chat_history', metadata, | |
*(Column(name, String) for name in masked_chat_history_df.columns)) | |
metadata.create_all(engine) | |
# マスクされたデータをPostgreSQLにインポート | |
masked_chat_history_df.to_sql('fasis_chat_history', engine, if_exists='replace', index=False) | |
print("データのインポートが完了しました。") | |
# データベースに接続してクエリを実行する関数 | |
def execute_query(query, engine): | |
with engine.connect() as connection: | |
result = connection.execute(text(query)) | |
return result.fetchall() | |
# クエリの例: マスクされたデータを取得 | |
query = "SELECT * FROM fasis_chat_history LIMIT 10;" | |
results = execute_query(query, engine) | |
# クエリ結果を表示 | |
for row in results: | |
print(row) | |