I have custom python script for twitter sentiment analysis, let's call it sentiment.py
how to execute that file in html web page.
-- it has two inputs, topic query and number of tweets.
this is the full code
import sys
import csv
import tweepy
import matplotlib.pyplot as plt
from collections import Counter
from aylienapiclient import textapi
if sys.version_info[0] < 3:
input = raw_input()
## Twitter credentials
consumer_key = "xxx"
consumer_secret = "xxx"
access_token = "xxx"
access_token_secret = "xxx"
## AYLIEN credentials
application_id = "xxx"
application_key = "xxx"
## set up an instance of Tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
## set up an instance of the AYLIEN Text API
client = textapi.Client(application_id, application_key)
## search Twitter for something that interests you
query = input("topic? \n")
number = input("number of tweets? \n")
results = api.search(
lang="en",
q=query + " -rt",
count=number,
result_type="recent"
)
print("--- Gathered Tweets \n")
## open a csv file to store the Tweets and their sentiment
file_name = 'Sentiment_Analysis_of_{}_Tweets_About_{}.csv'.format(number, query)
with open(file_name, 'w', newline='') as csvfile:
csv_writer = csv.DictWriter(
f=csvfile,
fieldnames=["Tweet", "Sentiment"]
)
csv_writer.writeheader()
print("--- Opened a CSV file to store the results of your sentiment analysis... \n")
## tidy up the Tweets and send each to the AYLIEN Text API
for c, result in enumerate(results, start=1):
tweet = result.text
tidy_tweet = tweet.strip().encode('ascii', 'ignore')
if len(tweet) == 0:
print('Empty Tweet')
continue
response = client.Sentiment({'text': tidy_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']
negative = counter['negative']
neutral = counter['neutral']
## declare the variables for the pie chart, using the Counter variables for "sizes"
colors = ['limegreen', 'dodgerblue', 'darkorchid']
sizes = [positive, negative, neutral]
labels = 'Positif', 'Negatif', 'Netral'
explode = (0.1, 0, 0)
## use matplotlib to plot the chart
plt.pie(
x=sizes,
shadow=False,
colors=colors,
labels=labels,
startangle=90,
explode=explode,
autopct='%1.0f%%'
)
plt.axis('equal')
plt.title("Sentiment of {} Tweets about {}".format(number, query))
plt.show()
how to execute that file in html web page.
let's say in the web page, there are two input texts, topic and number, and when I press the submit button, it executes the python script.
I'm new to this Django so please explain it with detail,
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/85d79e02-e517-45ea-a639-29856f1f5c65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment