i have a sentiment analysis python script, but I want to make it runs in html and get the input form from html page, and the shows the result back in html page. I already use a django framework to run html page. But i don't know how to connect it with python script I have.
this is my python script
query = input("query? \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
)
plt.axis('equal')
plt.title("Sentiment of {} Tweets about {}".format(number, query))
plt.show()