init commit
This commit is contained in:
64
lab4/generate_spam_data.py
Normal file
64
lab4/generate_spam_data.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import csv
|
||||
import random
|
||||
|
||||
random.seed(42)
|
||||
|
||||
SPAM_TEMPLATES = [
|
||||
"CONGRATULATIONS! You've WON a {prize}! Click {link} now to claim!!!",
|
||||
"URGENT: Your account will be suspended. Verify at {link} immediately",
|
||||
"Make ${amount} a week working from home, no experience needed! {link}",
|
||||
"FREE {prize} for the first 100 people! Limited time offer {link}",
|
||||
"Dear winner, you have been selected to receive {amount} dollars. Reply now",
|
||||
"Lowest prices on {product}! Buy now and save 90%! {link}",
|
||||
"Your {product} order could not be delivered, click {link} to reschedule",
|
||||
"Hot singles in your area want to meet you! {link}",
|
||||
"Act now! This {product} offer expires in 24 hours {link}",
|
||||
"You have 1 unread message regarding your {amount} refund, claim at {link}",
|
||||
]
|
||||
|
||||
HAM_TEMPLATES = [
|
||||
"Hey, are we still on for lunch tomorrow at noon?",
|
||||
"Please find attached the {product} report you requested.",
|
||||
"Can you review the pull request I sent over this morning?",
|
||||
"Reminder: team meeting moved to 3pm today.",
|
||||
"Thanks for your help with the {product} demo yesterday.",
|
||||
"The invoice for {amount} has been processed, receipt attached.",
|
||||
"Let's catch up sometime this week, been a while.",
|
||||
"I'll be out of office next Monday, please contact the team lead.",
|
||||
"Great presentation earlier, learned a lot about {product}.",
|
||||
"Attached is the agenda for tomorrow's {product} planning session.",
|
||||
]
|
||||
|
||||
PRIZES = ["iPhone", "vacation package", "gift card", "laptop", "cash prize"]
|
||||
PRODUCTS = ["software", "quarterly", "marketing", "onboarding", "budget"]
|
||||
LINKS = ["http://bit.ly/xyz123", "http://claim-now.co/win", "http://free-gift.biz"]
|
||||
AMOUNTS = ["500", "1000", "2500", "10000"]
|
||||
|
||||
|
||||
def fill(template):
|
||||
return template.format(
|
||||
prize=random.choice(PRIZES),
|
||||
link=random.choice(LINKS),
|
||||
amount=random.choice(AMOUNTS),
|
||||
product=random.choice(PRODUCTS),
|
||||
)
|
||||
|
||||
|
||||
def generate(n_per_class=300):
|
||||
rows = []
|
||||
for _ in range(n_per_class):
|
||||
rows.append((fill(random.choice(SPAM_TEMPLATES)), 1,
|
||||
round(min(1.0, max(0.0, random.gauss(0.85, 0.1))), 3)))
|
||||
rows.append((fill(random.choice(HAM_TEMPLATES)), 0,
|
||||
round(min(1.0, max(0.0, random.gauss(0.08, 0.07))), 3)))
|
||||
random.shuffle(rows)
|
||||
return rows
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
rows = generate(n_per_class=300)
|
||||
with open("data.csv", "w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(["text", "label", "spam_score"])
|
||||
writer.writerows(rows)
|
||||
print(f"Wrote {len(rows)} rows to data.csv")
|
||||
Reference in New Issue
Block a user