14.7.3. Demo: Hide Some Comments#

Now we will use our code from before, but we will skip displaying some comments, and we can make up whatever rule we want to do this.

First let’s make our fake conversation data:

comment_about_exam = {
    'text': 'That last exam in sure was hard!',
    'replies':[{
        'text': 'It sure was hard, what score did you get? ',
        'replies': [{
            'text': 'I got a 67% :(',
            'replies': []
        },{
            'text': 'I got a 73%',
            'replies': []
        }]
    }, {
        'text': 'I didn\'t think it was that bad',
        'replies': [{
            'text': 'how was that not a super hard exam?',
            'replies': []
        }, {
            'text': 'of course you didn\'t',
            'replies': [{
                'text': 'what\'s that supposed to mean?',
                'replies': [{
                    'text': 'you\'re an overacheiver',
                    'replies': [{
                        'text': 'and that\'s bad how?',
                        'replies': []
                    }]
                }]
            }]
        }]
    }]
}

We’ll copy that function to help us display the tweets nicely again too

from IPython.display import HTML, Image, display
import html
def display_indented(text, left_margin=0):
    display(
        HTML(
            "<pre style='border:solid 1px;padding:3px;margin-left:"+ str(left_margin) + "px'>" + 
            html.escape(text) + 
            "</pre>"
        )
    )

Display everything#

If we want to display everything, we can use the recursive function from the last section:

def print_comment_and_replies(comment, num_indents=0):
    # print indented tweet
    display_indented(comment['text'], left_margin=num_indents*20)
    
    #print replies (and the replies of those, etc.)
    for reply in comment['replies']:
        print_comment_and_replies(reply, num_indents = num_indents + 1)

And when we test this out, we can see the result

print_comment_and_replies(comment_about_exam)
That last exam in sure was hard!
It sure was hard, what score did you get? 
I got a 67% :(
I got a 73%
I didn't think it was that bad
how was that not a super hard exam?
of course you didn't
what's that supposed to mean?
you're an overacheiver
and that's bad how?

Display only some#

If we want to make a rule for what to display, we will first make a new function called should_display which will look at a comment/reply and return True if it should be displayed, or False if it should be hidden.

For our first rule, let’s say we will display all messages that are more than 16 characters long. If a comment/reply is shorter than that, we won’t display it or any of the replies to it.

def should_display(comment):
    # only display if the length of the comment text is more than 20 characters long 
    if(len(comment["text"]) > 20):
        return True
    else:
        return False

Now we will make a new version of our recursive print_tweet_and_replies with an added if statement that checks whether the should_display function says if we should display that comment and its replies:

def print_comment_and_replies(comment, num_indents=0):
    if(should_display(comment)):
        # print indented tweet
        display_indented(comment['text'], left_margin=num_indents*20)

        #print replies (and the replies of those, etc.)
        for reply in comment['replies']:
            print_comment_and_replies(reply, num_indents = num_indents + 1)

Now let’s test it out and see that fewer of the messages were printed out (only the long ones)

print_comment_and_replies(comment_about_exam)
That last exam in sure was hard!
It sure was hard, what score did you get? 
I didn't think it was that bad
how was that not a super hard exam?

Making up new rules#

We can make up whatever rules we want for what to display. For example, we might search for offensive words and hide those, or we could hide ones with negative sentiment.

As one more simple example here, we will make a new rule that only displays a message if it got replies (we will assume that if no one bothered to reply, than it isn’t worth displaying).

To make this change we will redefine our should_display function with the new rule, and then re-run print_tweet_and_replies

def should_display(comment):
    # only display if there are more than 0 replies
    if(len(comment["replies"]) > 0):
        return True
    else:
        return False
print_comment_and_replies(comment_about_exam)
That last exam in sure was hard!
It sure was hard, what score did you get? 
I didn't think it was that bad
of course you didn't
what's that supposed to mean?
you're an overacheiver