How to do Syntax Analysis with Google’s Natural Language API in Google Sheets (Apps Script)

syntax analysis google sheets tutorial mlforseo

Are you looking to understand the structural elements of language in large volumes of text like web content, technical documents, or anchor text? Syntax analysis with Google’s Natural Language API is one way to better understand sentence structures.

This tutorial will help you master the Syntax Analysis module of Google’s Natural Language API, all without needing to write a single line of code! This tutorial is suitable for complete beginners, as we will implement this machine learning API in Google Sheets via Apps Script code. You don’t need prior coding knowledge, I’ll lead you through the entire process, enabling you to dissect the grammatical structure of your text data.

About the method: How does syntax analysis work?

Syntax analysis, the second phase of natural language processing, involves examining the arrangement of words in a sentence to understand their grammatical structure. This process identifies how words function together to form coherent sentences, breaking down phrases into elements like nouns, verbs, adjectives, etc., and analyzing their relationships to one another.

Simply put, syntactic analysis breaks up a given text into a series of sentences and tokens (generally, words) and provides linguistic information about those tokens.

For example, in the sentence “The quick brown fox jumps over the lazy dog,” syntax analysis would identify “fox” as the subject, “jumps” as the verb, and “over the lazy dog” as the prepositional phrase indicating where the fox jumps. This analysis helps in understanding the sentence’s structure, which is crucial for further linguistic, semantic analysis, or computational processes, such as translating text or extracting information (like entities and sentiment).

About the model: Google Cloud’s Natural Language API

The Natural Language API is a versatile API that draws from a vast library filled with knowledge about language structure, grammar structure, sentiment, and real world entities. It’s trained on massive amounts of text data, allowing it to:

  • Understand context and entities: It goes beyond individual words, considering the surrounding text and even real-world knowledge to grasp the meaning of text via entity analysis.
  • Detect sentiment: It can sense emotions like joy, anger, or sadness expressed in the text, not only at a document level (the entire text) but also at the entity level (sentiment associated with a specific entity mentioned in the text).
  • Classify text: It has pre-training applied to identify whether the text you analyze aligns with either of more than 1,300+ categories
  • Moderate text: It can analyse documents and texts against a list of safety attributes, which include “harmful categories” and topics that may be considered sensitive.

How syntax analysis works

The analyzeSyntax module of Google’s Natural Language API is designed to parse the text you provide, breaking it down into its syntactic components to reveal the grammatical structure and relationships between different parts of the sentence.

The API will take text that you provide and return a response that provides the following information:

  • Sentence Content: This field stores the full content of each sentence parsed by the API. It is essential for contextual analysis and helps in understanding the syntax within the broader textual content.
  • Token Content: The specific word or punctuation mark analyzed is stored here. This field directly correlates to the individual elements of the sentence as parsed by the API.
  • Part of Speech: This comprehensive JSON object contains detailed part of speech information provided by the API, which includes not just basic categories like nouns and verbs, but also intricate details like aspect, case, and tense.
  • Dependency Label: Understanding syntactic relationships within sentences is simplified with this data, which labels how words relate to one another, such as ‘NSUBJ’ for nominal subjects or ‘DOBJ’ for direct objects.
  • Head Token Index: This index points to the syntactic head of each token, offering insights into the structural dependencies and hierarchies within sentences, crucial for parsing complex sentence structures.
  • Lemma: The lemma or the canonical form of each word is crucial for linguistic processing and is especially useful in applications that involve text normalization or keyword extraction.
  • Offset: The offset marks the position of each token within the original text, which is particularly useful for applications that need to highlight or manipulate specific parts of the text.

The language would be automatically detected from the text analysed, but you can also specify document language as well, as long as it’s within the list of supported languages.

Additional resources on syntax analysis

Check out the additional resources by Google Cloud to practice working with this API, and the syntax analysis module specifically:

Step-by-step guide on using the Natural Language API Syntax Analysis Module in Google Sheets via Apps Script


Get your API key

Having selected your Google Cloud project, navigate to the APIs and Services menu > Credentials.

Screenshot 2024 02 25 at 11.51.54

Then, click on the Create Credentials button from the navigation next to the page title, then select API Key from the drop-down menu.

image 3

This is the easiest to use, but least secure method of authentication – you might consider alternatives for more complex projects.

What is the difference between API, OAuth client ID and Service account authentication?

In short, API key authentication is like a Public key for basic access (like a library card), OAuth client ID allows for more user-specific access requiring authorization (like a bank card with PIN), while Service account authentication is the most secure access for applications without users (like a company credit card).

Once you click on the Create API key button, there will be a pop-up menu that will indicate that the API key is being created, after which it will appear on the screen for you to copy.

image 4

You can always navigate back to this section of your project, and reveal the API key at a later stage, using the Show Key button. If you ever need to edit or delete the API key, you can do so from the drop-down menu.

image 5

Extract and organise the text content you want to analyse syntax for

The next step is to decide on and organise the content you want to analyse syntax for into Google Sheets.

What content can you do syntax analysis for with the Natural Language API?

The Google Cloud Natural Language API can analyse syntax in both short snippets of text and longer documents. The API will split the text into sentences anyway, so there wouldn’t be any loss of quality of the analysis for longer or shorter lengths. You can do syntax analysis in organic search marketing in texts like your users’ social comments, page titles, or competitor content.

For the purposes of the demo today, I’ve scraped content from a news website using Screaming Frog.

Once you have your content organised into a spreadsheet-suitable format, you can move on to the next step.

Make a copy of the Google Sheets Template and paste your content and API key

To prepare the data for analysis, we need to do two things – organize the content for analysis, and paste the API key in the script.

Paste your API key

In Google Sheets, open the Extensions menu, and click on Apps Script.

Open the script attached, and select the text that says enterAPIkey. Replace it with your Google Cloud API project key. Then click on the disk icon to Save, and return to the Google Sheet file.

image 10

Replace the content in the Working Sheet with your own

Paste your content for analysis in the Working Sheet. Ensure that you keep the top-level navigation.

You need an ID for each item (like a URL, or customer identifier, depending on the content you are analysing), and content to analyse. Once you are done pasting your text, also clear the contents of column C: Syntax Structure (keep only the name of the column, as specified earlier).

image 11

Run the analysis to analyse the syntax of the provided text

To run the analysis, navigate to the Syntax Tools menu and click on the button, titled Mark Syntax Structure.

image 12

You might need to enable the permissions for the Apps Script to run before it runs (click on “Go to Syntax Analysis Google Natural language API (unsafe)“, then click “Allow”, and if that is the case, then navigate back to the menu above and click the same button again, once you’ve authorised the script.

image 13

An indication that the script has started executing is that there will be a pop-up in the bottom-right part of the screen notifying you that the syntax analysis has started, and for each of the individual texts, there will be a label of ‘complete’ in column C of the Working sheet.

Screenshot 2024 04 23 at 21.46.32

Now, you can navigate to the Syntax Data sheet. Your data, organised per your identifier, will be split per sentence (indicated in column B), and the API response will be organised in the other columns.

image 14

Visualize the Syntax Analysis Data in Looker Studio (Optional)

While analyzing the syntax structure data in a spreadsheet can be informative, visualizing these details in Looker Studio can significantly enhance your understanding and presentation of the linguistic features of your text.

Set up a Syntax Analysis Dashboard in Looker Studio, so you can visualize the details of syntax parsing effectively:

  • Flexible Data Input: Start by uploading your syntax analysis data directly or connect to various data sources where your textual data is stored.
  • Customizable Content Dimensions: You can define custom fields that are relevant to your analysis needs, such as text source, author, or document type
  • Token and Sentence Visualization: Utilize bar charts or column charts to display the frequency and distribution of specific parts of speech across your data set. This can highlight dominant linguistic features in different texts.
  • Dependency Structure Tree: Create visual diagrams or tree maps that display the dependency relationships between tokens within quickly understand sentence structures and the function of each word within.
  • Part of Speech Breakdown: See the overall distribution of parts of speech (nouns, verbs, adjectives, etc.) through charts, providing a quick overview of the textual style and complexity.
  • Lemmatization Analysis: Examine the base forms of words (lemmas) using a word cloud colored by part of speech or dependency label, offering insights into common themes or focuses within the text.
  • Interactive Filters: Employ advanced filters based on different dimensions like sentence length, text origin, or specific syntactic features to allow for granular, focused analyses on segments of your data that are of particular interest.

By leveraging these visualization techniques, you can uncover deeper linguistic patterns and improve the effectiveness of your text-based projects or research.

Why use Google’s Natural Language API syntax analysis in Google Sheets

Syntax analysis offers multiple advantages for improving SEO strategies by deeply understanding and optimizing the textual structure of web content. Using syntax analysis in Google Sheets can be a valuable addition to an SEO toolkit for several reasons, though it might not be necessary for every project:

  1. Improve Readability: Syntax analysis helps identify complex sentence structures that could be simplified, making content more accessible and enjoyable for a broader audience. This can lead to better user engagement and potentially higher search engine rankings.
  2. Help to create a better Keyword Strategy: It provides insights into the contextual use of keywords within content, allowing for more effective placement and usage that aligns with natural language processing trends in search algorithms.
  3. Optimize for Voice Search: As voice searches increase, syntax analysis is crucial for adapting content to match the conversational tone used in spoken queries, improving the likelihood of being featured in voice search results.
  4. Improve content quality: Syntax analysis can guide content creators to produce syntactically diverse and rich content, which can engage readers better and keep them on the page longer.
  5. Reduce spelling or grammar errors: By systematically identifying and correcting grammatical errors, syntax analysis helps maintain high-quality, professional web content, enhancing the site’s credibility and trustworthiness with both users and search engines.

While not every SEO project will require syntax analysis, it can provide deep insights into content structure when tackling specific challenges, such as enhancing readability or shifting the brand tone of voice.

Learn how to implement syntax analysis in your Organic Search strategy

Syntax analysis, while less commonly used than other SEO tools, offers opportunities to refine and enhance organic search strategies. This linguistic approach can dissect and interpret the structure of text in ways that can be beneficial for SEO and digital marketing. Here are some practical applications of syntax analysis that can help improve your organic search efforts:

  • Content Audit: By analyzing sentence structures and parts of speech within your website content, syntax analysis can identify complex sentences that may hinder readability. Simplifying these sentences can improve user engagement and content clarity, which are critical factors for SEO rankings.
  • Keyword Research: Syntax analysis can help identify the grammatical roles of words within searches or content, offering insights into how different parts of speech relate to keyword effectiveness. Understanding how nouns, verbs, and adjectives are used around your main keywords can guide you in creating richer, more contextually relevant content that aligns better with user intent.
  • Competitor Research: Use syntax analysis to break down the sentence structure and grammar used by competitors who rank well. This can reveal patterns and strategies in their writing that could be adapted to enhance your own content’s performance in search engine results pages (SERPs).
  • User Reviews Analysis: By examining the syntax of customer reviews or user-generated content, you can adjust your content’s tone, complexity, and style to better match your target audience’s preferences, enhancing user experience and engagement.
  • Detecting and Correcting Grammar Issues: Regularly scanning your website’s content for syntactic errors and inconsistencies can help maintain high-quality content standards, which is essential for both user experience and SEO rankings.

By integrating syntax analysis into your SEO processes, you can gain deeper insights into how the structure of text affects user interaction and search engine performance.

See what else you can do with to this API

As mentioned earlier, the Natural Language API has several additional capabilities that include text classification, entity analysis (which also includes entity sentiment analysis), and syntax analysis. Explore other step-by-step guides for this API by visiting the resources, linked below:

Meet the Author

Leave a Reply

Your email address will not be published. Required fields are marked *