コード例 #1
0
def problem4():
    if check_doctest('analyze_tweet_sentiment', trends):
        return True

    # Change the representation of sentiments to validate abstraction barrier.
    original_make_sentiment = trends.make_sentiment
    original_sentiment_value = trends.sentiment_value
    original_has_sentiment = trends.has_sentiment
    trends.make_sentiment = lambda s: lambda : s
    trends.sentiment_value = lambda s: s()
    trends.has_sentiment = lambda s: s() != None

    sentiment_tests = (
        ((trends.make_tweet('Help, I\'m trapped in an autograder factory and I can\'t get out!'.lower(), None, 0, 0),), -0.416666667),
        ((trends.make_tweet('The thing that I love about hating things that I love is that I hate loving that I hate doing it.'.lower(), None, 0, 0),), 0.075),
    )
    no_sentiment_tests = (
        ((trends.make_tweet('Peter Piper picked a peck of pickled peppers'.lower(), None, 0, 0),), None),
    )

    def analyze(tweet):
        return trends.sentiment_value(trends.analyze_tweet_sentiment(tweet))

    if check_func(analyze, sentiment_tests, comp=comp_float):
        return True
    if check_func(analyze, no_sentiment_tests):
        return True

    trends.make_sentiment = original_make_sentiment
    trends.sentiment_value = original_sentiment_value
    trends.has_sentiment = original_has_sentiment
コード例 #2
0
def problem4():
    if check_doctest('analyze_tweet_sentiment', trends):
        return True

    # Change the representation of sentiments to validate abstraction barrier.
    original_make_sentiment = trends.make_sentiment
    original_sentiment_value = trends.sentiment_value
    original_has_sentiment = trends.has_sentiment
    trends.make_sentiment = lambda s: lambda: s
    trends.sentiment_value = lambda s: s()
    trends.has_sentiment = lambda s: s() != None

    sentiment_tests = (
        ((trends.make_tweet(
            'Help, I\'m trapped in an autograder factory and I can\'t get out!'
            .lower(), None, 0, 0), ), -0.416666667),
        ((trends.make_tweet(
            'The thing that I love about hating things that I love is that I hate loving that I hate doing it.'
            .lower(), None, 0, 0), ), 0.075),
    )
    no_sentiment_tests = (((trends.make_tweet(
        'Peter Piper picked a peck of pickled peppers'.lower(), None, 0,
        0), ), None), )

    def analyze(tweet):
        return trends.sentiment_value(trends.analyze_tweet_sentiment(tweet))

    if check_func(analyze, sentiment_tests, comp=comp_float):
        return True
    if check_func(analyze, no_sentiment_tests):
        return True

    trends.make_sentiment = original_make_sentiment
    trends.sentiment_value = original_sentiment_value
    trends.has_sentiment = original_has_sentiment
コード例 #3
0
 def test_average():
     tweets = pirate_tweets(trends.make_tweet) + (
       trends.make_tweet('This tweet is without a sentiment', None, None, None),
       trends.make_tweet('This tweet is also without a sentiment', None, None, None),
     )
     tweets_by_state = {
         'MT': [ tweets[1], tweets[5] ],
         'MI': [ tweets[0], tweets[4] ],
         'FL': [ tweets[3], tweets[7] ],
         'ND': [ tweets[2], tweets[6] ],
         'AA': [ tweets[8], tweets[9] ],
     }
     expected = {
         'MT': -0.08333333333333333,
         'MI': 0.325,
         'FL': 0.5,
         'ND': 0.020833333333333332
     }
     tests = ( ((tweets_by_state,),expected) ,)
     if check_func(trends.average_sentiments, tests, comp=comp_dict):
         return True
コード例 #4
0
 def test_average():
     tweets = pirate_tweets(trends.make_tweet) + (
         trends.make_tweet('This tweet is without a sentiment', None, None,
                           None),
         trends.make_tweet('This tweet is also without a sentiment', None,
                           None, None),
     )
     tweets_by_state = {
         'MT': [tweets[1], tweets[5]],
         'MI': [tweets[0], tweets[4]],
         'FL': [tweets[3], tweets[7]],
         'ND': [tweets[2], tweets[6]],
         'AA': [tweets[8], tweets[9]],
     }
     expected = {
         'MT': -0.08333333333333333,
         'MI': 0.325,
         'FL': 0.5,
         'ND': 0.020833333333333332
     }
     tests = (((tweets_by_state, ), expected), )
     if check_func(trends.average_sentiments, tests, comp=comp_dict):
         return True