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

Learn the Weights چالش 6 از جلسه‌ی 33، سخت

به‌جای اینکه وزن‌ها را خودمان بنویسیم، از DATA یاد بگیریم: برای هر (text, label)، هر کلمه‌ی text اگر در weights نبود با 0 اضافه شود، بعد اگر label برابر "kind" بود +1 و وگرنه -1 بگیرد. خروجی:
fun 2
great 2
boring -2
slow -1
the 0
game 0
چرا the و game صفر شدند؟

DATA = [
    ("what a fun game", "kind"),
    ("the music is great", "kind"),
    ("fun level great idea", "kind"),
    ("this game is boring", "unkind"),
    ("the level is so slow", "unkind"),
    ("boring music", "unkind"),
]
weights = {}
for word in ["fun", "great", "boring", "slow", "the", "game"]:
    print(word, weights.get(word, 0))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 33: خوب یا بد؟

بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال