Pre-trained model tutorial

Updated at:

The NLP Self-learning Platform provides pre-trained model services for specific domains, such as product review parsing, news hierarchical classification, Chinese resume extraction, and English resume extraction.

Activate services and purchase resource plans

Before using pre-trained models, confirm that you have activated the Natural Language Processing and NLP Self-learning Platform services.

Natural Language Processing: Activation link

NLP Self-learning Platform: Activation link

Note

Pre-trained models include 500 free calls. If you exceed this limit, you must purchase a resource plan.

Purchase link

Use the NLP Self-learning Platform SDK

You can import the NLP Self-learning Platform software development kit (SDK). For more information, see the latest SDK version.

<dependency>    
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>3.5.0</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-nlp-automl</artifactId>
  <version>0.0.8</version>
</dependency>

API documentation: API Reference

API testing: API Test

Product review parsing service

Product review parsing service usage example

News hierarchical classification service

News hierarchical classification service usage example

Resume extraction (Chinese)

Resume extraction service usage example

Resume extraction (English)

Resume extraction (English) service usage example

Legal document extraction

Legal document extraction usage example

Conviction and sentencing prediction service

The following is a usage example for the conviction and sentencing prediction service. The current version only supports prediction for criminal cases.

Note

To call the criminal conviction and sentencing service, set the ServiceName parameter to convictionSentencingService.

Java code example

DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou","your-access-id-key","your-access-id-secret");
IAcsClient client = new DefaultAcsClient(defaultProfile);

Map<String, Object> input = new HashMap();
input.put("reqId","reqId");
input.put("text","Case description");

RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("convictionSentencingService");
request.setPredictContent(JSON.toJSONString(input));
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

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

# Initialize AcsClient instance
client = AcsClient(
  "<your-access-id-key>",
  "<your-access-id-secret>",
  "cn-hangzhou"
);

reqid = 'reqid'
text = 'Case description'
input_params = {
    "reqId": reqid,
    "text": text
}

# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('convictionSentencingService')
request.set_PredictContent(json.dumps(input_params))

# Print response
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
predict_result = json.loads(resp_obj['PredictResult'])
print(json.dumps(predict_result['data']))

PredictContent example

The PredictContent parameter is passed as a JSON string. The following example shows the content of the string.

{
  "reqId": "123445aad",
  "text": "The prosecution alleges that at approximately 15:00 on March 8, 2013, drug user Tang Mou contacted a man (handled in a separate case) by phone and arranged a drug transaction in Room 303 of the Hanming Hotel on Jinling North Road, Nansha District, Guangzhou City. At approximately 17:00 on the same day, defendant Yin Mou, instructed by the aforementioned man, went to the location and sold a small packet of transparent crystals to drug user Wen Moupeng for CNY 300. The defendant was then caught in the act by public security officers. The officers also seized two strips of tin foil from the defendant Yin Mou. After being arrested, defendant Yin Mou truthfully confessed his criminal acts to the public security authorities. Upon examination, the seized small packet of transparent crystals was found to have a net weight of 0.4 g and tested positive for methamphetamine. A test conducted by the public security authorities on defendant Yin Mou for methamphetamine was positive. The prosecution recommends that this court sentence defendant Yin Mou to a term of imprisonment ranging from XX to one year, and impose a fine."
}

Input parameters

Parameter

Type

Description

reqId

String

Request ID

text

String

Case description

PredictResult example

PredictResult is a JSON string. You can deserialize the string to parse it. The following example shows the structure.

{
  "reqId": "123445aad",
  "penalty": "7-month fixed-term imprisonment", 
  "articles": [{
    "lawText": "Article 347 [Crime of Smuggling, Trafficking, Transporting, or Manufacturing Drugs] Whoever smuggles, traffics in, transports, or manufactures drugs, regardless of the quantity, shall be investigated for criminal responsibility and punished. Whoever smuggles, traffics in, transports, or manufactures drugs and falls under any of the following circumstances shall be sentenced to a fixed-term imprisonment of 15 years, life imprisonment, or death, and shall also have their property confiscated: (1) Smuggling, trafficking, transporting, or manufacturing 1,000 grams or more of opium, 50 grams or more of heroin or methamphetamine, or large quantities of other drugs; (2) Being a ringleader of a drug smuggling, trafficking, transporting, or manufacturing group; (3) Using arms to cover the smuggling, trafficking, transporting, or manufacturing of drugs; (4) Resisting inspection, detention, or arrest by violence, where the circumstances are serious; (5) Participating in organized international drug trafficking activities. Whoever smuggles, traffics in, transports, or manufactures 200 grams or more but less than 1,000 grams of opium, 10 grams or more but less than 50 grams of heroin or methamphetamine, or relatively large quantities of other drugs shall be sentenced to a fixed-term imprisonment of not less than seven years and shall also be fined. Whoever smuggles, traffics in, transports, or manufactures less than 200 grams of opium, less than 10 grams of heroin or methamphetamine, or small quantities of other drugs shall be sentenced to a fixed-term imprisonment of not more than three years, criminal detention, or public surveillance, and shall also be fined; if the circumstances are serious, they shall be sentenced to a fixed-term imprisonment of not less than three years but not more than seven years and shall also be fined. Where a unit commits the crimes specified in the second, third, and fourth paragraphs, the unit shall be fined, and the persons in charge who are directly responsible and other directly responsible personnel shall be punished in accordance with the provisions of each of the said paragraphs. Whoever exploits or instigates a minor to smuggle, traffic in, transport, or manufacture drugs, or sells drugs to a minor, shall be given a heavier punishment. For multiple instances of smuggling, trafficking, transporting, or manufacturing drugs that have not been dealt with, the quantity of drugs shall be calculated cumulatively.",
    "provisionName": "Article 347 of the Criminal Law"
  }],  // Predicted legal articles output
  "accusation": ["Smuggling, trafficking, transporting, or manufacturing drugs"], 
  "features": [{
    "span": [207, 212],
    "content": "Methamphetamine",
    "type": "Conviction element",
    "name": "Drug type"
  }, {
    "span": [40, 122],
    "content": "contacted by phone and arranged a drug transaction in Room 303 of the Hanming Hotel on Jinling North Road, Nansha District, Guangzhou City. On the same day at approximately 17:00, defendant Yin Mou, instructed by the aforementioned man, went to the location and sold a small packet of transparent crystals for CNY 300 to",
    "type": "Conviction element",
    "name": "Trafficking drugs using information networks"
  }, {
    "span": [151, 212],
    "content": "two strips of tin foil were seized from his person. After being arrested, defendant Yin Mou truthfully confessed his criminal acts to the public security authorities. Upon examination, the seized small packet of transparent crystals was found to have a net weight of 0.4 g and tested positive for methamphetamine",
    "type": "Conviction element",
    "name": "Transporting drugs by carrying them"
  }, {
    "span": [151, 212],
    "content": "two strips of tin foil were seized from his person. After being arrested, defendant Yin Mou truthfully confessed his criminal acts to the public security authorities. Upon examination, the seized small packet of transparent crystals was found to have a net weight of 0.4 g and tested positive for methamphetamine",
    "type": "Conviction element",
    "name": "Drugs seized from the residence|vehicle of a drug trafficker"
  }, {
    "span": [207, 212],
    "content": "Methamphetamine",
    "type": "Sentencing element",
    "name": "Drug type"
  }, {
    "span": [198, 204],
    "content": "Net weight 0.4 g",
    "type": "Sentencing element",
    "name": "Drug weight in grams"
  }, {
    "span": [198, 204],
    "content": "Net weight 0.4 g",
    "type": "Sentencing element",
    "name": "Drug weight in grams"
  }, {
    "span": [199, 204],
    "content": "Weight 0.4 g",
    "type": "Sentencing element",
    "name": "Drug weight in grams"
  }]
}

Output parameters

Parameter

Type

Description

reqId

String

Request ID

penalty

String

Predicted sentence result

articles

List

Recommended legal articles

articles.lawText

String

Content of the legal article

articles.provisionName

String

Name of the legal article

accusation

List

Predicted charge result

features

List

Set of elements

features.span

[start int, end int]

Starting position of the element in the case description

features.content

String

The text content of the element from the case description

features.type

String

Element type

features.name

String

Element name