NLP examples
Now lets build a python program that leverages the Google Cloud Natural Language API in order to determine sentiment of a financial news article, The API will return both the sentiment and the magnitude of the article.
First we need to enable the API for it to be used for a project.
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ gcloud services enable language.googleapis.com
Operation "operations/acat.p2-517129368909-e986f696-8764-40a0-afd0-84034512f9c7" finished successfully.
The json key for the service account for the project needs to be accessible via a linux environmental variable.
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ export GOOGLE_APPLICATION_CREDENTIALS="/home/john_iacovacci1/cloud-project-examples-316c375c6892.json"
Install the Python Library needed for NLP
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ pip install google-cloud-language
Sent_exam.py
========================================================
from google.cloud import language_v1
client = language_v1.LanguageServiceClient()
def analyze_article_sentiment(text_content):
type_ = language_v1.Document.Type.PLAIN_TEXT
document = {"content": text_content, "type_": type_}
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_sentiment(request={'document': document, 'encoding_type': encoding_type})
overall_sentiment = response.document_sentiment
print(f"Article Sentiment Score: {overall_sentiment.score}")
print(f"Article Sentiment Magnitude: {overall_sentiment.magnitude}")
for sentence in response.sentences:
print(f"\nSentence: {sentence.text.content}")
print(f"Sentence Score: {sentence.sentiment.score}")
return overall_sentiment
article_text = """
Medical Properties Trust reported better than expected quarterly earnings.
However, rising interest rates remain a concern for future debt maturity.
"""
analyze_article_sentiment(article_text)
=========================================================
Results:
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ python3 sent_exam.py
Article Sentiment Score: 0.0
Article Sentiment Magnitude: 1.2000000476837158
Sentence: Medical Properties Trust reported better than expected quarterly earnings.
Sentence Score: 0.6000000238418579
Sentence: However, rising interest rates remain a concern for future debt maturity.
Sentence Score: -0.6000000238418579
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$
Breakdown of code
from google.cloud import language_v1
client = language_v1.LanguageServiceClient()
Language_v1 imports Google's library for NLP.
type_ = language_v1.Document.Type.PLAIN_TEXT
document = {"content": text_content, "type_": type_}
encoding_type = language_v1.EncodingType.UTF8
The type_ Tells the API that you are sending plain text.
The document points to the text you want analyzed. A dictionary containing your actual text (the article) and the type defined above.
Encoding_type identifies character sets used.
response = client.analyze_sentiment(request={'document': document, 'encoding_type': encoding_type})
This line executes the API passing parameters to the function and returns results into the response field.
overall_sentiment = response.document_sentiment
print(f"Article Sentiment Score: {overall_sentiment.score}")
print(f"Article Sentiment Magnitude: {overall_sentiment.magnitude}")
Web based articles scoring
Lets use internet based articles as a source for scoring. In this example I’ll use an earnings press release from a NYSE listed company as input in order to determine the sentiment of that release.
We will use the python library, Newspaper3k to extract web based articles.
Also, we will modify the sent_exam.py to allow for direct input of the url we want analyzed.
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ pip install newspaper3k
pip install newspaper3k
sent_url_link.py
======================================================
from google.cloud import language_v1
from newspaper import Article
client = language_v1.LanguageServiceClient()
def analyze_url_sentiment(url):
try:
article = Article(url)
article.download()
article.parse()
text_content = article.text
if not text_content:
print("\n[!] Error: Could not extract meaningful text from this URL.")
return
print(f"\n--- Analyzing: {article.title} ---")
document = {
"content": text_content,
"type_": language_v1.Document.Type.PLAIN_TEXT
}
response = client.analyze_sentiment(
request={'document': document, 'encoding_type': language_v1.EncodingType.UTF8}
)
overall = response.document_sentiment
print(f"Final Score: {overall.score} (Range -1.0 to 1.0)")
print(f"Magnitude: {overall.magnitude} (Strength of emotion)")
if overall.score > 0.2:
print("Verdict: Overall Positive")
elif overall.score < -0.2:
print("Verdict: Overall Negative")
else:
print("Verdict: Neutral or Mixed")
except Exception as e:
print(f"\n[!] Error processing URL: {e}")
if __name__ == "__main__":
user_url = input("Please paste the article URL here: ").strip()
if user_url.startswith("http"):
analyze_url_sentiment(user_url)
else:
print("[!] Invalid URL. Please make sure it starts with http:// or https://")
=========================================================
Results:
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ python3 sent_url_link.py
Please paste the article URL here: https://finance.yahoo.com/news/medical-properties-trust-mpt-6-001906438.html
--- Analyzing: Medical Properties Trust (MPT) Is Up 6.2% After Returning To Quarterly Profitability And Affirming Dividend – Has The Bull Case Changed? ---
Final Score: 0.10000000149011612 (Range -1.0 to 1.0)
Magnitude: 4.800000190734863 (Strength of emotion)
Verdict: Neutral or Mixed
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$
Let’s add Named Entity Recognition (NER) functionality to the program. The analyze_entities method will be used to identify entities that are important to the article. Also, add salience score to the NER process.
I’m looking for a link for the last press release for NVIDIA so I’ll ask gemini to find a link for me
link for last earnings press release from nvidia
Link is in blue and underlined so I will right click and copy the url
sent_ent_sal.py
=========================================================
from google.cloud import language_v1
from newspaper import Article
client = language_v1.LanguageServiceClient()
def analyze_url_full(url):
try:
article = Article(url)
article.download()
article.parse()
text_content = article.text
if not text_content:
print("\n[!] Error: Could not extract meaningful text from this URL.")
return
print(f"\n--- Analyzing: {article.title} ---")
document = {
"content": text_content,
"type_": language_v1.Document.Type.PLAIN_TEXT
}
encoding_type = language_v1.EncodingType.UTF8
sentiment_response = client.analyze_sentiment(
request={'document': document, 'encoding_type': encoding_type}
)
overall = sentiment_response.document_sentiment
print(f"\n[ SENTIMENT RESULTS ]")
print(f"Final Score: {overall.score} (-1.0 to 1.0)")
print(f"Magnitude: {overall.magnitude} (Total emotional weight)")
entity_response = client.analyze_entities(
request={'document': document, 'encoding_type': encoding_type}
)
print(f"\n[ TOP ENTITIES BY SALIENCE ]")
for entity in entity_response.entities[:10]:
entity_type = language_v1.Entity.Type(entity.type_).name
salience_pct = entity.salience * 100
print(f"- {entity.name}")
print(f" Type: {entity_type} | Salience: {salience_pct:.2f}%")
if 'wikipedia_url' in entity.metadata:
print(f" Wiki: {entity.metadata['wikipedia_url']}")
except Exception as e:
print(f"\n[!] Error processing URL: {e}")
if __name__ == "__main__":
user_url = input("Please paste the article URL here: ").strip()
if user_url.startswith("http"):
analyze_url_full(user_url)
else:
print("[!] Invalid URL. Please make sure it starts with http:// or https://")
=========================================================Results:
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$ python3 sent_ent_sal.py
Please paste the article URL here: https://nvidianews.nvidia.com/news/nvidia-announces-financial-results-for-fourth-quarter-and-fiscal-2026
--- Analyzing: NVIDIA Announces Financial Results for Fourth Quarter and Fiscal 2026 ---
[ SENTIMENT RESULTS ]
Final Score: 0.10000000149011612 (-1.0 to 1.0)
Magnitude: 18.299999237060547 (Total emotional weight)
[ TOP ENTITIES BY SALIENCE ]
- NVIDIA
Type: ORGANIZATION | Salience: 11.20%
Wiki: https://en.wikipedia.org/wiki/Nvidia
- Fireworks AI
Type: OTHER | Salience: 7.06%
- share
Type: OTHER | Salience: 3.85%
- revenue
Type: OTHER | Salience: 2.95%
- Context Memory Storage Platform
Type: OTHER | Salience: 2.04%
- earnings
Type: OTHER | Salience: 1.75%
- record revenue
Type: OTHER | Salience: 1.67%
- compensation expense
Type: OTHER | Salience: 1.37%
- company
Type: ORGANIZATION | Salience: 1.32%
- NVIDIA AI
Type: OTHER | Salience: 1.11%
john_iacovacci1@cloudshell:~/nlp (cloud-project-examples)$
NER identifies NVIDIA as an ORGANIZATION.
link for last earnings press release from nvidia
No comments:
Post a Comment