Text Summarization API for Python


Installing

To use our Text Summarization API in Python, you need install the Unirest for Python client library first. The simplest way install it by pip:


pip install unirest

After installing the pip package, you can test it by imporint unirest:


import unirest

Now you are probaly wondering how using Unirest to creat requests for the Text Summarization API in Python. Let's start with the previous json rquest body example:


{
    "url": "http://en.wikipedia.org/wiki/Automatic_summarization",
    "text": "",
    "sentnum": 8
}

Creating POST Request

The request created with Unirest Python library like this:


response = unirest.post("https://textanalysis-text-summarization.p.mashape.com/text-summarizer",
    headers={
        "X-Mashape-Authorization": "Mashape API Key",
        "Content-Type": "application/json"
    },
    params="{\"url\":\"http:\/\/en.wikipedia.org\/wiki\/Automatic_summarization\",\"text\":\"\",\"sentnum\":8}"
);

Here the Mashape API Key you can find in your Mashape account dashboard, copy it and replace it, and than excute it on the python interpreter, finally, you can get the response like this:


>>> import unirest
>>> response = unirest.post("https://textanalysis-text-summarization.p.mashape.com/text-summarizer",
...     headers={
...         "X-Mashape-Authorization": "Your Mashape API Key",
...         "Content-Type": "application/json"
...     },
...     params="{\"url\":\"http:\/\/en.wikipedia.org\/wiki\/Automatic_summarization\",\"text\":\"\",\"sentnum\":8}"
... );
>>> dir(response)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_body', '_code', '_headers', '_raw_body', 'body', 'code', 'headers', 'raw_body']
>>> response.code
200
>>> response.headers

>>> response.body
{u'sentences': [u'Document summarization is another.', u'Generally, there are two approaches to automatic summarization: extraction and abstraction.', u'Furthermore, evaluation of extracted summaries can be automated, since it is essentially a classification task.', u'Even though automating abstractive summarization is the goal of summarization research, most practical systems are based on some form of extractive summarization.', u'Extractive methods work by selecting a subset of existing words, phrases, or sentences in the original text to form the summary.', u'The state-of-the-art abstractive methods are still quite weak, so most research has focused on extractive methods.', u'These systems are known as multi-document summarization systems.', u'People are subjective, and different authors would choose different sentences.']}
>>> response.raw_body
'{\n  "sentences": [\n    "Document summarization is another.",\n    "Generally, there are two approaches to automatic summarization: extraction and abstraction.",\n    "Furthermore, evaluation of extracted summaries can be automated, since it is essentially a classification task.",\n    "Even though automating abstractive summarization is the goal of summarization research, most practical systems are based on some form of extractive summarization.",\n    "Extractive methods work by selecting a subset of existing words, phrases, or sentences in the original text to form the summary.",\n    "The state-of-the-art abstractive methods are still quite weak, so most research has focused on extractive methods.",\n    "These systems are known as multi-document summarization systems.",\n    "People are subjective, and different authors would choose different sentences."\n  ]\n}'
>>>

If you test it successuly in your python interpreter, now it's time to enjoy our Text Summarization API for your Python Projects.

About Unirest

Unirest is a set of lightweight HTTP libraries available in multiple languages, ideal for most applications:

  • Make GET, POST, PUT, PATCH, DELETE requests
  • Both syncronous and asynchronous (non-blocking) requests
  • It supports form parameters, file uploads and custom body entities
  • Supports gzip
  • Supports Basic Authentication natively
  • Customizable timeout
  • Customizable default headers for every request (DRY)
  • Automatic JSON parsing into a native object for JSON responses

Reference

For other languages support by Text Summarization API, you can find the document link below: