Skip to content

tirtho/cognitive-text-python

Repository files navigation

cognitive-text-python

Python code using Azure Cognitive Text Analytics on Twitter Feed, Stock Feed etc... to extract key phrases, named entities, sentiment etc.

Setup

Twitter Developer API Keys

Login to your Twitter account and go to the Twitter Developer Page and get the Twitter API Key, API Key Secret, Access Token and Access Token Secret. You will need it to get Twitter Feed.

Install Tweepy

Install the tweepy python package in your python environment.

Marketstack API Keys

Get a Marketstack API Key to make API calls from python.

Install stocknews python package

Install the stocknews python package in your python environment.

Setup an Azure Cognitive Service Instance

Create an Azure subscription if do not have one already. You may want to ask the Azure administrator in your organization first.

Alternatively, you can create your own free Azure subscription to test out.

Once you have your Azure Subscription, create an Azure Cognitive Services resource or check with your Azure adminstrator to create one for you. Once this created, get the key and endpoint from the Azure Portal under the "Keys and Endpoint" Menu under the Azur eCognitive Service resource.

Azure Cognitive Service Page in Azure Portal

Install Azure AI Text Analytics Python Packages

Install the Azure AI Text Analytics python package in your python environment.

    > pip install azure-ai-textanalytics==5.1.0b7

Run

The python code here has been tested with version 3.9.1 in Windows.

Before running any of the python code, from command line, set the following environment variables with the values of keys etc. you obtained from above.

set STOCK_NEWS_KEY=<YOUR MARKETSTACK API KEY>
set TWITTER_CONSUMER_KEY=<YOUR TWITTER CONSUMER KEY>
set TWITTER_CONSUMER_SECRET=<YOUR TWITTER CONSUMER SECRET>
set TWITTER_ACCESS_TOKEN_KEY=<YOUR TWITTER ACCESS TOKEN>
set TWITTER_ACCESS_TOKEN_SECRET=<YOUR TWITTER ACCESS TOKEN SECRET>
set COGNITIVE_SERVICE_KEY=<YOUR AZURE COGNITIVE SERVICE KEY>
set COGNITIVE_SERVICE_ENDPOINT=https://<YOUR AZURE COGNITIVE SERVICE NAME>.cognitiveservices.azure.com/

In order to get end of day stock market information of a ticker symbol and also to get twitter feed on a search string and then feed it to Azure Text Analytics APIs to extract key phrases, named entities, sentiment etc.. run this below -

# Run text analytics on twitter feed
> python ComputeCompanyScore.py -s TSLA -q "electric+car+chip" 

The output

Date: 2021-05-28T00:00:00+0000, Name: Tesla Inc [TSLA], Open: 628.500000, High: 635.590000, Low: 622.380000, Closing: 625.220000, Volume: 22640034.000000

Search Twitter messages for electric+car+chip since 2020-05-13
        Tweet -> RT @NetDec: Find out more about the latest AI-driven Navy Submarine powered by an Nvidia chip and electric car battery. Is this the beginni…
        Tweet -> Find out more about the latest AI-driven Navy Submarine powered by an Nvidia chip and electric car battery. Is this… https://t.co/9WxSirayfb
        Tweet -> Chip shortage will hit electric car production https://t.co/ujU0NBAXFv
        Tweet -> @jimcramer Nah. Chip shortage is going to crush their delivery for the next year. Their share price is going to get… https://t.co/P4VjtvtjLB
        Tweet -> @Jchaps03 @The_Left_Prince @ElonLovesUs @Lunnaris01 @SenSanders Yeah Elon having control would be such a bad thing!… https://t.co/66aOnOGXAE

Key Phrases:
         ['Nvidia chip', 'latest AI-driven Navy Submarine', 'electric car battery', 'RT', 'NetDec', 'beginni']

Named Entities:
        Text:    Nvidia
        Category:        Organization   SubCategory:     None   Confidence Score:        0.54

Document Sentiment: neutral

Overall scores: positive=0.06; neutral=0.90; negative=0.04

About

Python code using Azure Cognitive Text Analytics, Twitter Feed, Stock Feed etc...

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages