جواب «Learn the Weights» چالش 6 از جلسهی 33
سؤال
بهجای اینکه وزنها را خودمان بنویسیم، از DATA یاد بگیریم: برای هر (text, label)، هر کلمهی text اگر در weights نبود با 0 اضافه شود، بعد اگر label برابر "kind" بود +1 و وگرنه -1 بگیرد. خروجی:fun 2great 2boring -2slow -1the 0game 0
چرا the و game صفر شدند؟
راهنما
for text, label in DATA: for word in text.split():، بعد if word not in weights: weights[word] = 0 و if label == "kind": weights[word] += 1 و else: weights[word] -= 1.
یک جواب درست
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 text, label in DATA:
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 ["fun", "great", "boring", "slow", "the", "game"]:
print(word, weights.get(word, 0))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن