-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_functions.py
More file actions
189 lines (151 loc) · 5.99 KB
/
demo_functions.py
File metadata and controls
189 lines (151 loc) · 5.99 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
"""
Demo script to test individual utility functions without full dependencies.
Run this to see the Lambda's utility functions in action.
"""
import re
import hashlib
def extract_email_address(from_header: str) -> str:
"""Extract clean email address from 'From' header."""
email_match = re.search(r"<([^>]+)>|([^\s<>]+@[^\s<>]+)", from_header)
if email_match:
return email_match.group(1) or email_match.group(2)
return from_header.strip()
def normalize_subject_for_thread(subject: str) -> str:
"""Normalize email subject for thread_id generation."""
if not subject:
return ""
normalized = re.sub(r"^(re:|fwd:|fw:)\s*", "", subject, flags=re.IGNORECASE).strip()
normalized = " ".join(normalized.split())
return normalized
def generate_thread_id(sender_email: str, recipient_email: str, subject: str) -> str:
"""Generate a consistent thread_id."""
sender_clean = sender_email.lower().strip()
recipient_clean = recipient_email.lower().strip()
subject_normalized = normalize_subject_for_thread(subject)
emails_sorted = sorted([sender_clean, recipient_clean])
thread_components = emails_sorted + [subject_normalized]
thread_string = "|".join(thread_components)
thread_hash = hashlib.sha256(thread_string.encode("utf-8")).hexdigest()[:16]
subject_prefix = "".join(c for c in subject_normalized[:10] if c.isalnum()).lower()
if subject_prefix:
return f"{thread_hash}-{subject_prefix}"
else:
return thread_hash
def extract_budget_value(budget_str: str) -> float:
"""Extract numeric budget value from budget string."""
if not budget_str:
return 0.0
try:
clean_budget = budget_str.replace("$", "").replace(",", "").replace("USD", "").strip()
budget_numbers = re.findall(r"\d+(?:\.\d{2})?", clean_budget)
if budget_numbers:
return float(budget_numbers[0])
else:
range_match = re.search(r"(\d+(?:\.\d{2})?)\s*[-–]\s*(\d+(?:\.\d{2})?)", clean_budget)
if range_match:
return float(range_match.group(1))
except (ValueError, AttributeError):
pass
return 0.0
def determine_deal_type_from_budget(budget_str: str) -> str:
"""Determine deal type based on budget description."""
if not budget_str:
return "FLAT_RATE"
budget_lower = budget_str.lower()
if any(term in budget_lower for term in ["affiliate", "commission", "%"]):
return "AFFILIATE"
elif "ugc" in budget_lower:
return "UGC"
elif "sponsored" in budget_lower:
return "SPONSORED_POST"
elif "partnership" in budget_lower:
return "BRAND_PARTNERSHIP"
elif any(term in budget_lower for term in ["revenue", "share"]):
return "REVENUE_SHARE"
elif any(term in budget_lower for term in ["performance", "hybrid"]):
return "PERFORMANCE_HYBRID"
else:
return "FLAT_RATE"
# ============================================================================
# DEMO: Run this file to see the functions in action
# ============================================================================
if __name__ == "__main__":
print("=" * 70)
print("REPFLOW LAMBDA - UTILITY FUNCTIONS DEMO")
print("=" * 70)
# Demo 1: Email Extraction
print("\n[EMAIL] 1. EMAIL EXTRACTION")
print("-" * 70)
test_emails = [
"John Doe <[email protected]>",
'"Brand Rep" <[email protected]>',
]
for email in test_emails:
result = extract_email_address(email)
print(f"Input: {email!r}")
print(f"Output: {result!r}\n")
# Demo 2: Subject Normalization
print("\n[SUBJECT] 2. SUBJECT NORMALIZATION")
print("-" * 70)
test_subjects = [
"Re: Sponsorship Opportunity",
"Fwd: Partnership Inquiry",
"RE: Important Deal",
"Brand Partnership Q4"
]
for subject in test_subjects:
result = normalize_subject_for_thread(subject)
print(f"Input: {subject!r}")
print(f"Output: {result!r}\n")
# Demo 3: Thread ID Generation
print("\n[THREAD] 3. THREAD ID GENERATION")
print("-" * 70)
thread_tests = [
("[email protected]", "[email protected]", "Sponsorship Opportunity"),
("[email protected]", "[email protected]", "Re: Sponsorship Opportunity"), # Same thread!
("[email protected]", "[email protected]", "Sponsorship Opportunity"), # Different
]
for sender, recipient, subject in thread_tests:
result = generate_thread_id(sender, recipient, subject)
print(f"From: {sender}")
print(f"To: {recipient}")
print(f"Subject: {subject}")
print(f"Thread: {result}\n")
# Demo 4: Budget Extraction
print("\n[BUDGET] 4. BUDGET EXTRACTION")
print("-" * 70)
test_budgets = [
"$1,000",
"$1,000 - $5,000",
"Budget is $2500 USD",
"5000 dollars",
"Negotiable"
]
for budget in test_budgets:
result = extract_budget_value(budget)
print(f"Input: {budget!r}")
print(f"Output: ${result:,.2f}\n")
# Demo 5: Deal Type Detection
print("\n[DEALS] 5. DEAL TYPE DETECTION")
print("-" * 70)
test_deal_types = [
"$5000 flat rate",
"10% affiliate commission",
"$500 for UGC content",
"$2000 sponsored post",
"Long-term partnership",
"$1000 + performance bonus"
]
for deal_desc in test_deal_types:
result = determine_deal_type_from_budget(deal_desc)
print(f"Input: {deal_desc!r}")
print(f"Type: {result}\n")
print("=" * 70)
print("[SUCCESS] DEMO COMPLETE!")
print("=" * 70)
print("\nThese are the core utility functions from the Lambda.")
print("The full Lambda uses 4 AI agents + AWS services.")
print("\nTo see the full analysis, read: SOLUTION.md")
print("To run all 80 tests: pytest tests/ -v")