Exemplo n.º 1
0
def generate_response(text):
    # blob = TextBlob(message, classifier=CLASSIFICATION)
    # text_type = blob.classify(message)
    blob = TextBlob(text)
    text_type = TEXT_STATEMENT
    nouns_str = get_key_nouns_str(blob)
    sentiment = blob.sentiment(text).polarity
    response = dbutil.find_chat_response(text, text_type, nouns_str, sentiment)
    return response
    )

    ## tidy up the Tweets and send each to the AYLIEN Text API
    for c, result in enumerate(results, start=1):
        tweet = result.text
        client = TextBlob(tweet)
        #tidy_tweet = tweet.strip().encode('ascii', 'ignore')

        if len(tweet) == 0:
            print('Empty Tweet')
            continue

        #response = client.sentiment

        #response = client.sentiment({'text': tidy_tweet})
        response = client.sentiment({'text': tweet})

        csv_writer.writerow({
            'Tweet': response['text'],
            'Sentiment': response['polarity']
        })

        print("Analyzed Tweet {}".format(c))

## count the data in the Sentiment column of the CSV file
with open(file_name, 'r') as data:
    counter = Counter()
    for row in csv.DictReader(data):
        counter[row['Sentiment']] += 1

    positive = counter['positive']