let's design a program with compound Boolean Expressions we're working on an automated content moderation system for our site we want our system to automatically flag posts that seem questionable so our team can investigate further and decide which ones to take down we also want to automatically promote any posts that we think are particularly useful so that they appear at the top of the page let's think about what our algorithm for flagging posts might look like our moderators have told us that they're especially wey of new accounts it's not often that users who have been on
our site for a while all of a sudden start posting a ton of spam they're of course legitimate new users and we don't want to discourage them from using our site by flagging their posts all the time so we think the best thing to do might be to check for a combination of conditions we have available the overall sentiment of the post whether it's positive neutral or negative here we're most worried about negative posts we definitely don't want users bullying each other or promoting violence on our site all right now what a better algorithm for
promoting useful posts we want to make sure not to promote posts that are NE but we want to be fair and not just remote posts that talk about how great our site is so we'll consider both positive and neutral posts then we decided we also want to Value consistent users people who have been on our site for a while and represent trusted voices we recognize this isn't a perfect algorithm but we think it might be good enough for what we're trying to do here next step let's translate our content moderation algorithms into code we start
with our two pieces of data the sentiment of the post and the user's account age in days our algorithm for flagging was if the sentiment is negative and the account is new let's say new is less than a week old that means our condition is the sentiment equals equals negative and the account age and days is less than seven it's an and because we only want to flag if both conditions are true we surround our compound condition with an if statement and then inside the if statement we want to print our content moderation decision that
lets our moderators know to take a closer look at this post okay now let's test this on a couple different posts a negative post created by a new user should get this flag a negative post created by a longtime user shouldn't and a positive post created by a new user shouldn't either let's work on our post promotion algorithm next this algorithm was neutral or positive posts from trusted accounts because there's only three possible sentiments positive neutral and negative this is equivalent to saying sentiment is not equal to negative for trusted users let's go with an
account age that's greater than or equal to 30 days that's not the perfect equivalent but we think it'll provide a good approximation so we add our if statement and then inside the if statement we just want to signal that the post has been featured so we indent a print function call inside the statement then let's run through a few test cases to make sure this is working as intended a negative post by a trusted user a positive post by a new user and a neutral post by a trusted user we have it working but we
want to refine our algorithm a bit we are noticing that some not super useful posts are getting featured like that post we had at the beginning that just said hi we think we can make a good generalization here that posts that are super short or super long are probably not the most useful so let's add another condition to our feature case to do this we need a new piece of information about each post we need to know how many words it has our team says it should be easy to get this data so we'll add
a new variable word count this is about to make our conditions super long so I'm going to break it up into multiple pieces to make it easier to read post length that we don't like are less than or equal to 3 words or greater than 200 words we use an or here because it's suspicious if either condition is true we store that intermediate result in a variable and then we add it on to our feature condition we use an and here because we want all three of these conditions to be true in order to feature
the post however we don't want to feature it if it's a suspicious length we want to feature it if it's not a suspicious length so we use the not operator here now let's check our condition with that post that said hi it had a word count of one a neutral sentiment and a pretty old account great now that post is no longer being featured however I see I'm getting a lint error now where my line is too long to fix this I'm going to break my condition up into multiple variables let's say a useful post
is not negative and not a suspicious length and then let's store the result of the account age check in a variable called is trusted user then our condition just becomes if is useful post and is trusted user which is a lot easier to understand at a glance in fact it's so readable that it's self-documenting such that we don't even really need this comment anymore because it just says the same thing as the variable names we'll go on and test with a few more cases to make sure everything works and then we'll make sure to monitor
how our algorithm performs on our site so we can keep making adjustments as needed