Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue lies in how you're splitting the input strings for `positive` and `negative`. You're splitting on `"##"`, but the input strings don't contain this substring. They appear to be comma-separated lists of author names and titles, which you're trying to use to match against the `titles` list. One change that would fix this is to split the input strings on a different separator, such as commas or newline characters, depending on the actual format of the input strings. For example, if the titles are just the first part of the string before the newline character, you could split on newline characters like so:
1 2 | positive = request.args.get('positive', '').split('\n')[0].split(':')[1].strip() negative = request.args.get('negative', '').split('\n')[0].split(':')[1].strip() |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue lies in how you're splitting the input strings for `positive` and `negative`. You're splitting on `"##"`, but the input strings don't contain this substring. They appear to be comma-separated lists of author names and titles, which you're trying to use to match against the `titles` list. One change that would fix this is to split the input strings on a different separator, such as commas or newline characters, depending on the actual format of the input strings. For example, if the titles are just the first part of the string before the newline character, you could split on newline characters like so:
1 2 | positive = request.args.get('positive', '').split('\n')[0].split(':')[1].strip() negative = request.args.get('negative', '').split('\n')[0].split(':')[1].strip() |
The student fails.
Testing if the previous output contains the string student passes
: False