Keyword extraction and extractive text summarization

更新时间:
复制 MD 格式

The keyword extraction and text summarization service uses the TextRank algorithm to extract keywords or summaries from documents.

Note

This service is provided by the NLP Self-Learning Platform. To use this service, call the API directly.

Service activation and resource plan purchase

Before you start, ensure that the service is activated. After you activate the service, you can purchase a resource plan.

Service invocation and debugging

For more information about how to call the model, see Model invocation.

For more information about SDK examples, see SDK examples.

Debug

You can run this API operation directly in the OpenAPI Developer Portal to avoid the need to calculate signatures. Upon successful execution, the OpenAPI Developer Portal automatically generates SDK code examples.

Configure access credentials using environment variables

  1. Notes:

    1. An AccessKey for an Alibaba Cloud account has access permissions for all APIs. This poses a high security threat. We strongly recommend that you create and use a Resource Access Management (RAM) user for API access or daily O&M. To create a RAM user, log on to the RAM console.

    2. We strongly recommend that you do not hardcode your AccessKey ID and AccessKey secret in your code because this can lead to security threats. This document describes how to store and access your credentials by configuring environment variables.

  2. Method for configuring Linux and macOS

    export NLP_AK_ENV=<ACCESS_KEY_ID>
    export NLP_SK_ENV=<ACCESS_KEY_SECRET>

    Replace <ACCESS_KEY_ID> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey for your account.

  3. Configuration method for a Windows system

    1. Create an environment variable file. Add the NLP_AK_ENV and NLP_SK_ENV environment variables. Then, set their values to your AccessKey ID and AccessKey secret.

    2. Restart the Windows operating system.

Java code example

/**
 * An AccessKey of an Alibaba Cloud account has access permissions for all APIs. This poses a high security threat. We strongly recommend that you create and use a RAM user for API access or daily O&M. To create a RAM user, log on to the RAM console.
 * This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
 * We strongly recommend that you do not hardcode your AccessKey ID and AccessKey secret in your code. This can lead to security threats.
 */
String accessKeyId = System.getenv("NLP_AK_ENV");
String accessKeySecret = System.getenv("NLP_SK_ENV");
DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
IAcsClient client = new DefaultAcsClient(defaultProfile);
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("query","Don't take your range hood apart to clean it. A small bar of soap can get it clean in minutes. It's simple and effective. Hi everyone, I'm a helper from Shenzhen Xiaogebang, and I share life hacks for your home. Follow for more! I've talked before about how professional cleaners tackle range hoods. Many of you commented that you don't have special tools and would rather learn some simple, everyday tips that don't require disassembly. So, I've been testing some ideas and can finally share a few practical tricks to quickly cut through grease. Let's get started! Soap and toilet paper. Surprised? You probably thought I was going to suggest baking soda or a special range hood cleaner, right? Nope! Like I said, this is an everyday method. Everyone has soap and toilet paper. I've tested this myself: crush a leftover piece of soap into a paste. Use an old toothbrush or just your hands to spread it on the range hood. Use warm water! After a few minutes, the grease will dissolve on its own. Then, just wipe it off with toilet paper. There's no need to take the range hood apart at all. It'll be sparkling clean! Dish soap and white vinegar. Many people have tried using dish soap to clean their range hoods, but maybe without much success. I suggest mixing dish soap with white vinegar to scrub the range hood's panel and grease trap. To clean the grease trap, remove it and soak it in warm, soapy water for about half an hour. Then, wipe it with a cloth, and you'll see a big difference! You can do both of these methods at home without buying a lot of special tools. To clean your range hood, give them a try!");
obj.put("top_k",5);
obj.put("type","keyword_extraction");
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NLP-textrank");
request.setPredictContent(JSON.toJSONString(obj));
RunPreTrainServiceResponse response = client.getAcsResponse(request);
System.out.println(response.getPredictResult());

Python code example

# Install dependencies
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-nlp-automl
# -*- coding: utf8 -*-
import json
import os

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest

# An AccessKey of an Alibaba Cloud account has access permissions for all APIs. This poses a high security threat. We strongly recommend that you create and use a RAM user for API access or daily O&M. To create a RAM user, log on to the RAM console.
# This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
# We strongly recommend that you do not hardcode your AccessKey ID and AccessKey secret in your code. This can lead to security threats.
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']

# Initialize an AcsClient instance
client = AcsClient(
  access_key_id,
  access_key_secret,
  "cn-hangzhou"
);
content = {
    "query": "Don't take your range hood apart to clean it. A small bar of soap can get it clean in minutes. It's simple and effective. Hi everyone, I'm a helper from Shenzhen Xiaogebang, and I share life hacks for your home. Follow for more! I've talked before about how professional cleaners tackle range hoods. Many of you commented that you don't have special tools and would rather learn some simple, everyday tips that don't require disassembly. So, I've been testing some ideas and can finally share a few practical tricks to quickly cut through grease. Let's get started! Soap and toilet paper. Surprised? You probably thought I was going to suggest baking soda or a special range hood cleaner, right? Nope! Like I said, this is an everyday method. Everyone has soap and toilet paper. I've tested this myself: crush a leftover piece of soap into a paste. Use an old toothbrush or just your hands to spread it on the range hood. Use warm water! After a few minutes, the grease will dissolve on its own. Then, just wipe it off with toilet paper. There's no need to take the range hood apart at all. It'll be sparkling clean! Dish soap and white vinegar. Many people have tried using dish soap to clean their range hoods, but maybe without much success. I suggest mixing dish soap with white vinegar to scrub the range hood's panel and grease trap. To clean the grease trap, remove it and soak it in warm, soapy water for about half an hour. Then, wipe it with a cloth, and you'll see a big difference! You can do both of these methods at home without buying a lot of special tools. To clean your range hood, give them a try!",
  "top_k": 5,
  "type": "keyword_extraction"
}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NLP-textrank')
request.set_PredictContent(json.dumps(content))
# Print the response
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
predict_result = json.loads(resp_obj['PredictResult'])
print(predict_result)

PredictContent example

{
  "query": "Don't take your range hood apart to clean it. A small bar of soap can get it clean in minutes. It's simple and effective. Hi everyone, I'm a helper from Shenzhen Xiaogebang, and I share life hacks for your home. Follow for more! I've talked before about how professional cleaners tackle range hoods. Many of you commented that you don't have special tools and would rather learn some simple, everyday tips that don't require disassembly. So, I've been testing some ideas and can finally share a few practical tricks to quickly cut through grease. Let's get started! Soap and toilet paper. Surprised? You probably thought I was going to suggest baking soda or a special range hood cleaner, right? Nope! Like I said, this is an everyday method. Everyone has soap and toilet paper. I've tested this myself: crush a leftover piece of soap into a paste. Use an old toothbrush or just your hands to spread it on the range hood. Use warm water! After a few minutes, the grease will dissolve on its own. Then, just wipe it off with toilet paper. There's no need to take the range hood apart at all. It'll be sparkling clean! Dish soap and white vinegar. Many people have tried using dish soap to clean their range hoods, but maybe without much success. I suggest mixing dish soap with white vinegar to scrub the range hood's panel and grease trap. To clean the grease trap, remove it and soak it in warm, soapy water for about half an hour. Then, wipe it with a cloth, and you'll see a big difference! You can do both of these methods at home without buying a lot of special tools. To clean your range hood, give them a try!",
  "top_k": 5,
  "type": "keyword_extraction"
}

PredictResult example

{
  "keywords_or_summaries": [
    "range hood",
    "helper",
    "clean",
    "soap",
    "share"
  ],
  "scores": [
    0.20365885857353325,
    0.2007197193667164,
    0.19899930843706187,
    0.19868134965414105,
    0.19794076396854748
  ]
}

Request parameters

Parameter

Description

query

Required. The content of the document from which to extract information.

top_k

Required. The number of keywords to extract when type is keyword_extraction, or the number of sentences to extract when type is text_summarization.

type

Optional. Specifies the extraction type. Set the value to keyword_extraction to extract keywords. Set the value to text_summarization to extract a summary. The default value is keyword_extraction.

Response parameters

Parameter

Description

keywords_or_summaries

A list of the extracted keywords or summary sentences, sorted by weight in descending order.

scores

The weight of each corresponding keyword or sentence.