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

جواب «Step 1: Learn the Word Weights» چالش 1 از جلسه‌ی 36

سؤال

قدم ۱. TRAIN و TEST آماده‌اند. دیکشنری weights را از TRAIN یاد بگیر: برای هر (text, label) و هر کلمه‌ی text.split()، اگر کلمه در weights نبود با 0 اضافه شود؛ بعد +1 اگر label == "kind"، وگرنه -1. برنامه وزن پنج کلمه را چاپ می‌کند. خط اول:
love 3

راهنما

دقیقاً مثل سؤال آخر جلسه‌ی ۳۳، فقط روی TRAIN.

یک جواب درست

TRAIN = [
    ("great game i love it", "kind"), ("so fun and so cool", "kind"), ("nice colors", "kind"),
    ("amazing work", "kind"), ("i love the music", "kind"), ("this is so cool", "kind"),
    ("great idea", "kind"), ("fun level and nice music", "kind"), ("thanks for sharing", "kind"),
    ("cool game well done", "kind"), ("the art is beautiful", "kind"), ("so funny i love it", "kind"),
    ("well done great job", "kind"), ("nice and fun", "kind"), ("the best game ever", "kind"),
    ("i like the colors", "kind"), ("beautiful work thanks", "kind"), ("very cool idea", "kind"),
    ("fun fun fun", "kind"), ("good job", "kind"),
    ("this is boring", "unkind"), ("so stupid", "unkind"), ("worst game ever", "unkind"),
    ("ugly colors", "unkind"), ("i hate it", "unkind"), ("boring and slow", "unkind"),
    ("lame idea", "unkind"), ("this is dumb", "unkind"), ("you are a loser", "unkind"),
    ("the music is awful", "unkind"), ("so slow and boring", "unkind"), ("terrible game", "unkind"),
    ("ugly and stupid", "unkind"), ("i hate this game", "unkind"), ("worst idea", "unkind"),
    ("dumb level", "unkind"), ("boring music", "unkind"), ("awful art", "unkind"),
    ("this game is lame", "unkind"), ("so bad", "unkind"),
]

TEST = [
    ("i love the art", "kind"), ("boring level", "unkind"), ("cool music", "kind"),
    ("this is the worst", "unkind"), ("so nice", "kind"), ("ugly game", "unkind"),
    ("not bad at all", "kind"), ("you are not funny", "unkind"), ("great work", "kind"),
    ("slow and lame", "unkind"),
]

weights = {}
for text, label in TRAIN:
    for word in text.split():
        if word not in weights:
            weights[word] = 0
        if label == "kind":
            weights[word] += 1
        else:
            weights[word] -= 1

for word in ["love", "boring", "game", "the", "fun"]:
    print(word, weights.get(word, 0))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن