Commit
·
d57d7e1
1
Parent(s):
fd1befe
Update app.py
Browse files
app.py
CHANGED
@@ -3,63 +3,11 @@ import streamlit as st
|
|
3 |
from google.oauth2 import service_account
|
4 |
from google.cloud import language_v1
|
5 |
|
6 |
-
#
|
7 |
-
st.sidebar.title("About This Tool")
|
8 |
-
|
9 |
-
st.sidebar.markdown("### Descriptive Introduction")
|
10 |
-
st.sidebar.markdown("""
|
11 |
-
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.
|
12 |
-
|
13 |
-
The application is built with Streamlit and interfaces with Google's Cloud NLP API to conduct the entity analysis. It allows users to enter text and an optional query, performs the analysis, and then displays the results in an easy-to-read format. The output includes the type of the entity, its relevance score (salience), and any metadata associated with it.
|
14 |
-
""")
|
15 |
-
|
16 |
-
st.sidebar.markdown("### Step-by-Step Guide")
|
17 |
-
st.sidebar.markdown("""
|
18 |
-
1. **Open the Tool**: Navigate to the URL where the tool is hosted.
|
19 |
-
2. **User Input**:
|
20 |
-
- **Text to Analyze**: In the text area labeled "Enter text to analyze", paste or type the text you want to analyze.
|
21 |
-
- **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.
|
22 |
-
- **Analyze**: Click the button labeled "Analyze". The tool will then process the text and perform entity analysis on it.
|
23 |
-
3. **View Results**:
|
24 |
-
- 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).
|
25 |
-
- Below this, you'll find a line-by-line breakdown of each entity identified in the text.
|
26 |
-
4. **Interpreting the Output**: Each entity will be displayed with the following information:
|
27 |
-
- **Relevance Score**: A score indicating how relevant the entity is within the text.
|
28 |
-
- **Name**: The name of the entity.
|
29 |
-
- **Type**: The type of entity (e.g., PERSON, ORGANIZATION).
|
30 |
-
- **Salience Score**: A score indicating the entity's importance or centrality in the text.
|
31 |
-
- **Metadata**: Additional data associated with the entity.
|
32 |
-
- **Mentions**: Instances where the entity is mentioned in the text.
|
33 |
-
- **Knowledge Graph Information**: For each entity, additional information from the Google Knowledge Graph will be displayed, providing more context about the entity.
|
34 |
-
5. **Language of the Text**: At the bottom, the tool will also display the language in which the text is written.
|
35 |
-
|
36 |
-
And that's it! You can now easily analyze text for entities and get insights into how Google understands the content.
|
37 |
-
""")
|
38 |
-
|
39 |
-
# Header and intro
|
40 |
-
st.title("Google Cloud NLP Entity Analyzer")
|
41 |
-
st.write("## Introduction to the Knowledge Graph API")
|
42 |
-
st.write("---")
|
43 |
-
st.write("""
|
44 |
-
The Google Knowledge Graph API reveals entity information related to a keyword, that Google knows about.
|
45 |
-
This information can be very useful for SEO – discovering related topics and what Google believes is relevant.
|
46 |
-
It can also help when trying to claim/win a Knowledge Graph box on search results.
|
47 |
-
The API requires a high level of technical understanding, so this tool creates a simple public interface, with the ability to export data into spreadsheets.
|
48 |
-
""")
|
49 |
|
50 |
def sample_analyze_entities(text_content, your_query=""):
|
51 |
-
|
52 |
-
|
53 |
-
service_account_info, scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
54 |
-
)
|
55 |
-
client = language_v1.LanguageServiceClient(credentials=credentials)
|
56 |
-
type_ = language_v1.Document.Type.PLAIN_TEXT
|
57 |
-
language = "en"
|
58 |
-
document = {"content": text_content, "type_": type_, "language": language}
|
59 |
-
encoding_type = language_v1.EncodingType.UTF8
|
60 |
-
|
61 |
-
response = client.analyze_entities(request={"document": document, "encoding_type": encoding_type})
|
62 |
-
|
63 |
entities_list = []
|
64 |
for entity in response.entities:
|
65 |
entity_details = {
|
@@ -77,25 +25,26 @@ def sample_analyze_entities(text_content, your_query=""):
|
|
77 |
st.write("### We found results for your query")
|
78 |
|
79 |
st.write("----")
|
80 |
-
for i, entity in enumerate(entities_list):
|
81 |
-
st.write(f"Entity {i+1} of {len(entities_list)}")
|
82 |
-
st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
|
83 |
-
st.write(f"Name: {entity.get('Name', 'N/A')}")
|
84 |
-
st.write(f"Type: {entity.get('Type', 'N/A')}")
|
85 |
-
st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
st.write("
|
90 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
mentions = entity.get('Mentions', [])
|
93 |
-
if mentions:
|
94 |
-
st.write("Mentions:")
|
95 |
-
st.write(', '.join(mentions))
|
96 |
-
|
97 |
-
st.write("----")
|
98 |
-
|
99 |
st.write(f"### Language of the text: {response.language}")
|
100 |
|
101 |
# User input for text analysis
|
|
|
3 |
from google.oauth2 import service_account
|
4 |
from google.cloud import language_v1
|
5 |
|
6 |
+
# ... (sidebar and headers are the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def sample_analyze_entities(text_content, your_query=""):
|
9 |
+
# ... (NLP setup is the same)
|
10 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
entities_list = []
|
12 |
for entity in response.entities:
|
13 |
entity_details = {
|
|
|
25 |
st.write("### We found results for your query")
|
26 |
|
27 |
st.write("----")
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
for i, entity in enumerate(entities_list):
|
30 |
+
st.write(f"Entity {i+1} of {len(entities_list)}")
|
31 |
+
st.write(f"Relevance Score: {round(entity.get('Salience Score', 0) * 100)}%")
|
32 |
+
st.write(f"Name: {entity.get('Name', 'N/A')}")
|
33 |
+
st.write(f"Type: {entity.get('Type', 'N/A')}")
|
34 |
+
st.write(f"Salience Score: {entity.get('Salience Score', 'N/A')}")
|
35 |
+
|
36 |
+
metadata = entity.get('Metadata', {})
|
37 |
+
if metadata:
|
38 |
+
st.write("Metadata:")
|
39 |
+
st.write(metadata)
|
40 |
+
|
41 |
+
mentions = entity.get('Mentions', [])
|
42 |
+
if mentions:
|
43 |
+
st.write("Mentions:")
|
44 |
+
st.write(', '.join(mentions))
|
45 |
+
|
46 |
+
st.write("----")
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
st.write(f"### Language of the text: {response.language}")
|
49 |
|
50 |
# User input for text analysis
|