Text Similarity (Basic Edition)

Introduction

This API is used to compute the similarity of texts.

For details about endpoints, see Endpoints.

Calling NLP APIs will incur fees. NLP packages are classified into the basic and domain-specific editions. When purchasing a package, view the APIs supported by the basic package and domain-specific packages in the Natural Language Processing Price Calculator.

URI

  • URI format
    POST /v1/{project_id}/nlp-fundamental/text-similarity
  • Parameter description
    Table 1 URI parameters

    Parameter

    Mandatory

    Description

    project_id

    Yes.

    Project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

Request

Table 2 describes the request parameters.

Table 2 Request parameters

Parameter

Type

Mandatory

Description

text1

String

Yes

Text 1 to be analyzed. The text is encoded using UTF-8. Chinese text contains 1 to 512 characters and English text contains 1 to 2,000 characters.

text2

String

Yes

Text 2 to be analyzed. The text is encoded using UTF-8. Chinese text contains 1 to 512 characters and English text contains 1 to 2,000 characters.

lang

String

No

Supported text language type. The default value is zh. Currently, Chinese and English are supported. The values are zh and en, respectively.

Response

Table 3 describes the response parameters.

Table 3 Response parameters

Parameter

Type

Description

similarity

Float

Similarity score. The value ranges from 0 to 1. By default, eight digits are reserved after the decimal point.

error_code

String

Error code when the API fails to be called. For details, see Error Code.

The parameter is not included when the API call succeeds.

error_msg

String

Error message returned when the API fails to be called.

The parameter is not included when the API call succeeds.

Example

  • Example request
    POST https://{endpoint}/v1/{project_id}/nlp-fundamental/text-similarity
    
    Request Header:
        Content-Type: application/json
        X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    
    Request Body:
        {
      "text1":"Text for analysis",
    "text2": "Text for analysis",
            "lang":"zh"
        }
  • Example response
    • Successful response example
      {
        "similarity":0.73369961
      }
    • Failed response example
      {
          "error_code": "NLP.0301",
          "error_msg": "argument valid error:text2.text2 for text similarity should be between 1 to 512 ;text2.must not be blank;"
      }

Status Code

For details about status codes, see Status Code.

Error Code

For details about error codes, see Error Code.