Spaces:
Sleeping
Sleeping
File size: 2,259 Bytes
e386167 |
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 |
import re
import random
from collections import Counter, defaultdict
def parse_chat(file_path):
pattern = r"(\d{1,2}/\d{1,2}/\d{2,4}), (\d{1,2}:\d{2}) - ([^:]+): (.*)"
messages = []
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
match = re.match(pattern, line)
if match:
date, time, sender, text = match.groups()
# Normalize names
if sender == "ak":
sender = "Aman"
elif sender == "Sarah con H":
sender = "Sarah"
messages.append({
"date": date,
"time": time,
"sender": sender,
"text": text.strip()
})
return messages
def extract_inside_jokes(messages):
funny_candidates = []
cute_candidates = []
memory_candidates = []
phrase_counter = Counter()
funny_keywords = ["lol", "😂", "🤣", "lmao", "funny", "haha", "hehe","hahaha"]
cute_keywords = ["miss", "thank", "sweet", "cute", "proud", "happy","aww","glad"]
for msg in messages:
text = msg["text"].lower()
# Funny moments
if any(k in text for k in funny_keywords):
funny_candidates.append(msg["text"])
# Cute/emotional moments
if any(k in text for k in cute_keywords):
cute_candidates.append(msg["text"])
# Memorable random moments
if len(msg["text"].split()) > 4: # skip too short
memory_candidates.append(msg["text"])
# Count repeated words
phrase_counter.update(text.split())
top_words = [w for w, c in phrase_counter.most_common(40)]
return {
"funny": funny_candidates,
"cute": cute_candidates,
"memories": memory_candidates,
"top_words": top_words
}
def random_memory(messages):
"""Returns a random meaningful moment."""
long_messages = [m["text"] for m in messages if len(m["text"]) > 10]
if not long_messages:
return "One of your old conversations ❤️"
return random.choice(long_messages)
|