-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_readable_version.py
More file actions
65 lines (49 loc) · 2.43 KB
/
Copy pathcreate_readable_version.py
File metadata and controls
65 lines (49 loc) · 2.43 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
#!/usr/bin/env python3
"""
Create a less aggressively redacted version for better sarcasm analysis
"""
import pandas as pd
import re
def light_redaction(text):
"""Apply lighter redaction - keep common words, mask only potential personal info"""
if pd.isna(text):
return text
# Keep common words but mask potential names/places
text = re.sub(r'\b[A-Z][a-z]{2,}\b', '[NAME]', text) # Proper nouns
text = re.sub(r'\b\d{3,}\b', '[NUMBER]', text) # Long numbers
text = re.sub(r'\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b', '[EMAIL]', text) # Emails
text = re.sub(r'\b\+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\b', '[PHONE]', text) # Phone numbers
return text
def main():
print("🔧 Creating lighter redaction version...")
# Load the existing CSV and convert timestamp column properly
df = pd.read_csv('pi_sarcasm_hybrid_review.csv')
df["sent_at_utc"] = pd.to_datetime(df["sent_at_utc"])
# Load original data to get unredacted text
import json
with open("Copy of pi-user-history.json", "r") as f:
data = json.load(f)
messages = pd.DataFrame(data['user_data']['messages'])
ai_messages = messages[messages["sender"] == "AI"].copy()
# Apply lighter redaction to original text
ai_messages["text_light_redacted"] = ai_messages["text"].apply(light_redaction)
# Convert timestamps for matching
ai_messages["sent_at_dt"] = pd.to_datetime(ai_messages["sent_at"], format='mixed')
# Create a mapping from timestamp to lightly redacted text
timestamp_to_text = dict(zip(ai_messages["sent_at_dt"], ai_messages["text_light_redacted"]))
# Add lighter redaction to our analysis by matching timestamps
df["text_readable"] = df["sent_at_utc"].apply(lambda x: timestamp_to_text.get(x, ""))
# Reorder columns to put readable text first
cols = ["text_readable"] + [col for col in df.columns if col != "text_readable"]
df_readable = df[cols]
# Save the more readable version
df_readable.to_csv("pi_sarcasm_readable_review.csv", index=False)
print("✅ Created pi_sarcasm_readable_review.csv with lighter redaction")
print("\nSample of readable text:")
for i, text in enumerate(df_readable['text_readable'].head(3)):
if text:
print(f"{i+1}. {text[:150]}...")
else:
print(f"{i+1}. (No matching text found)")
if __name__ == "__main__":
main()