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

جواب «Step 4: A Person Decides» چالش 4 از جلسه‌ی 36

سؤال

قدم ۴. سه نظر تازه در new_comments می‌آید. اگر check گفت «شاید نامهربان»، دلیلش را چاپ کن و با input() از آدم بپرس: اگر جواب y بود چاپ کن published (a person decided)، وگرنه hidden. نظرهای خوب مستقیم -> published می‌شوند. به نظر سوم نگاه کن: کمی نیش دارد ولی ابزار ردش کرد! برای همین آدم باید همه‌چیز را ببیند. آخر کارت مدل را چاپ کن:
MODEL CARD
data: 40 comments from our class, in English
test accuracy: 90 %
limits: does not understand 'not' or jokes

راهنما

for text in new_comments: با score, bad_words = check(text) و if score < 0: که داخلش answer = input() و if answer == "y": است. درصد کارت: right * 10 (چون آزمون ۱۰ نظر است).

یک جواب درست

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

def check(text):
    score = 0
    bad_words = []
    for word in text.split():
        w = weights.get(word, 0)
        score += w
        if w < 0:
            bad_words.append(word)
    return score, bad_words

right = 0
mistakes = []
for text, label in TEST:
    score, bad_words = check(text)
    guess = "kind" if score >= 0 else "unkind"
    if guess == label:
        right += 1
    else:
        mistakes.append(text)
print("right:", right, "of", len(TEST))
for text in mistakes:
    print("mistake:", text)

new_comments = ["your game is boring", "nice jump sound", "the music is so slow"]
for text in new_comments:
    score, bad_words = check(text)
    if score < 0:
        print(text, "-> maybe unkind because of:", ", ".join(bad_words))
        answer = input()
        if answer == "y":
            print("published (a person decided)")
        else:
            print("hidden")
    else:
        print(text, "-> published")

print("MODEL CARD")
print("data: 40 comments from our class, in English")
print("test accuracy:", right * 10, "%")
print("limits: does not understand 'not' or jokes")

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