|
import json |
|
import streamlit as st |
|
from google.oauth2 import service_account |
|
from google.cloud import language_v1 |
|
|
|
|
|
st.sidebar.title("About This Tool") |
|
st.sidebar.markdown("### Descriptive Introduction") |
|
st.sidebar.markdown |
|
st.sidebar.markdown("### Step-by-Step Guide") |
|
st.sidebar.markdown(""" |
|
1. **Open the Tool**: Navigate to the URL where the tool is hosted. |
|
2. **User Input**: |
|
- **Text to Analyze**: In the text area labeled "Enter text to analyze", paste or type the text you want to analyze. |
|
- **Query**: Optionally, you can also enter a specific query in the text input field labeled "Enter your query (optional)". This is for your own reference. |
|
- **Analyze**: Click the button labeled "Analyze". The tool will then process the text and perform entity analysis on it. |
|
3. **View Results**: |
|
- After the analysis is complete, you'll see a section that says, "We found X results for your query of your_query" (or just "We found results for your query" if no query was entered). |
|
- Below this, you'll find a line-by-line breakdown of each entity identified in the text. |
|
""") |
|
|
|
|
|
st.title("Google Cloud NLP Entity Analyzer") |
|
st.write("""The "Google Cloud NLP Entity Analyzer" is a powerful tool designed to analyze text and identify various types of entities such as people, locations, organizations, and events. Leveraging Google's Natural Language Processing (NLP) technology, this tool provides insights into how Google understands text, which can be particularly useful for Search Engine Optimization (SEO) efforts. It also serves as an interface to the Google Knowledge Graph API, providing additional contextual information about the identified entities.""") |
|
|
|
|
|
|
|
def sample_analyze_entities(text_content, your_query=""): |
|
service_account_info = json.loads(st.secrets["google_nlp"]) |
|
credentials = service_account.Credentials.from_service_account_info( |
|
service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"] |
|
) |
|
client = language_v1.LanguageServiceClient(credentials=credentials) |
|
type_ = language_v1.Document.Type.PLAIN_TEXT |
|
language = "en" |
|
document = {"content": text_content, "type_": type_, "language": language} |
|
encoding_type = language_v1.EncodingType.UTF8 |
|
|
|
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type}) |
|
|
|
entities_list = [] |
|
for entity in response.entities: |
|
entity_details = { |
|
"Name": entity.name, |
|
"Type": language_v1.Entity.Type(entity.type_).name, |
|
"Salience Score": entity.salience, |
|
"Metadata": entity.metadata, |
|
"Mentions": [mention.text.content for mention in entity.mentions] |
|
} |
|
entities_list.append(entity_details) |
|
|
|
if your_query: |
|
st.write(f"### We found {len(entities_list)} results for your query of **{your_query}**") |
|
else: |
|
st.write("### We found results for your query") |
|
|
|
st.write("----") |
|
for i, entity in enumerate(entities_list): |
|
st.write(f"Entity {i+1} of {len(entities_list)}") |
|
st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%") |
|
st.write(f"Name: {entity.get('Name', 'N/A')}") |
|
st.write(f"Type: {entity.get('Type', 'N/A')}") |
|
st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}") |
|
|
|
metadata = entity.get('Metadata', {}) |
|
if metadata: |
|
st.write("Metadata:") |
|
st.write(metadata) |
|
|
|
mentions = entity.get('Mentions', []) |
|
if mentions: |
|
st.write("Mentions:") |
|
st.write(json.dumps(mentions)) |
|
|
|
st.write("----") |
|
|
|
st.write(f"### Language of the text: {response.language}") |
|
|
|
|
|
user_input = st.text_area("Enter text to analyze") |
|
|
|
|
|
if st.button("Analyze"): |
|
sample_analyze_entities(user_input) |
|
|