بتل کد Battle Codeدارد باز می‌شود…

Step 2: Build X and y چالش 2 از جلسه‌ی 24، متوسط

قدم دوم: از لیست messages (جفت‌های متن و برچسب) دو لیست X و y بساز: X ویژگی‌های هر پیام و y برچسب‌ها. برنامه X[0]، y[0] و len(X) را چاپ می‌کند: [4, 5, 1]، spam، 6.

SPAM_WORDS = ["free", "win", "now", "click", "prize"]


def features(text):
    words = text.lower().split()
    spam_count = 0
    for w in words:
        if w.strip("!") in SPAM_WORDS:
            spam_count += 1
    return [spam_count, len(words), text.count("!")]


messages = [
    ("Win a FREE prize now!", "spam"),
    ("free money win now click", "spam"),
    ("you won click now!!", "spam"),
    ("hi are you coming to class", "ham"),
    ("see you at football practice", "ham"),
    ("mum says dinner is ready", "ham"),
]
X = []
y = []
# fill X and y from messages
print(X[0])
print(y[0])
print(len(X))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 24: پروژه: تشخیص هرز · چالش بعدی: Step 3: Classify New Messages

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال