This content originally appeared on Level Up Coding - Medium and was authored by Rafał Rybnik
Add a tl;dr button to Google Docs
Whether you’re a writer, data scientist or just skimming through sources to get a job done, reading longer texts to pluck out crumbs of information can be quite exhausting. Automating such elements of your work allows you to focus on the creative side of things.
Text summarization
Text summarization is the technique of extracting key informational elements of a voluminous text. Manual text summarization is a difficult and time-expensive task, so NLP and machine learning algorithms became popular to automate it.
There are ready-made solutions on the market, whether in the form of libraries or ready-made tools for end-users.
In this article, we will prepare our own tailored solution, which at the same time does not require advanced knowledge of data science.
NLP Cloud
NLP Cloud is a provider of multiple APIs for text processing using machine learning models. One of these is the text summarizer, which looks promising in terms of simple implementation.
The summarizer provided by NLP Cloud is extractive, which means that no new sentences are generated, but parts with low information-noise ratio are removed.
Let’s see an example:
We pass a block of text, and the model returns a summary. But operating in a console is not very convenient. So, let’s make Google Docs summarize the selected text fragment this way.
Extending Google Docs
Our goal is to create a convenient menu so that text summarization happens automatically from within Google Docs.
This is how our project is structured. Using Apps Script, we will extend the GUI with a button that will trigger functions that communicate with the NLP Cloud API and then insert the result below.
We will start by preparing an add-on for the menu.
Making menu
Using Google Apps Scripts to adding custom functionalities to Google Docs is fairly easy. You can customize the user interface with new menus, dialog boxes and sidebars. To create a script, after opening Google Docs, select Tools -> Script editor.
Note: you can learn more about creating Apps Script project from my other articles:
How to create online survey for free with SurveyJS and Google Sheets
All the interface elements should be added in the onOpen function. It is run after opening the document and allows us to add the menu.
After saving script and refreshing Docs, you should find new menu element “Text Summarizer”.
Before we implement summarizeSelection function, we must get selected text.
Get selection
The tricky part is getting currently selected text and passing it to functions. It is possible thanks to getSelection function:
However, this function returns not only highlighted part of the documents, but a whole paragraph in which selections resides. That’s why we create more complex getSelectedText function:
The function simply takes the entire paragraph and trims it down to just the selected fragment. It also returns the index of the paragraph so you know where to insert the summary.
Now, let’s send the text to the API and parse the result.
External APIs
We’ll use the `UrlFetch service to make API requests directly. Text Summarization API request requires authorization through token. To get it, sign up at nlpcloud.io (the free plan will do just fine).
The API being requested returns a raw JSON response for a request.
Remember that all text processed by this script are sent to external API.
To test the functionality, just select a text fragment in Docs and choose Text Summarization -> Summarize selection from the Docs menu.
After a moment of processing, you should see a pop-up with the result, which you can even copy.
Writing response to document
Finally, we can insert an API-generated summary directly into the document. For ease of distinction, bold the text of the summary. That’s why the getSelectedText function also returns the paragraph index of the selected text.
This way we know exactly where to tell Apps Script to insert the new text.
Let’s test final version:
Takeaways
Building an application that extends Google Docs functionality based on external APIs is an interesting alternative to building large systems to automate inconvenient work steps. Moreover, individual components can be replaced, depending on needs and specific skills. For example, our solution can be extended with emotion detection or text classification (also available in NLP Cloud). You can also prepare your own API or simple functions directly in Apps Script.
Thank you for reading. I hope you enjoyed reading as much as I enjoyed writing this for you.
If you enjoyed reading this, you’ll probably enjoy my other articles too:
References
https://jeffreyeverhart.com/2018/10/05/translate-text-google-doc-using-google-apps-script/
https://developers.google.com/google-ads/scripts/docs/features/third-party-apis
https://netlabe.com/real-time-context-targeting-using-nlp-baceb4324fc4
How to add text summarizer to Google Docs using Text Summarization API was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Rafał Rybnik
Rafał Rybnik | Sciencx (2021-05-25T14:52:18+00:00) How to add text summarizer to Google Docs using Text Summarization API. Retrieved from https://www.scien.cx/2021/05/25/how-to-add-text-summarizer-to-google-docs-using-text-summarization-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.